Press "Enter" to skip to content

Wildcard replace in unix

Wildacrd replace in Unix

Way back when the limited wildcard replace in DOS was as simple as using a ? to represent a single character for renaming, moving, copying, or whatever you wanted to do to a file..

For example;
copy 117691???.txt test117691???.txt

would search the current directory and match any filename with 117691(any 3 chatacters).txt and copy them to test117691(same 3 characters).txt.

On Unix or Linux, it just doesn’t work, but alas the key is a simple for loop, tested in bash and ksh shells;

for file in 117691????.job; do cp $file test${file%?????}; done
This command gives the same results as the old DOS method, a little more typing but with a little thought it becomes a very powerful command.