Using find
command:
# Path to the source directory
dir="./"
while read file
do
output="$(basename "$file")"
output="$(dirname "$file")/"${output/#file/output}
echo "$file ==> $output"
done < <(find "$dir" \
-regextype 'posix-egrep' \
-regex '.*file\.[0-9]{3}\.txt\.gz$')
The same via pipe:
find "$dir" \
-regextype 'posix-egrep' \
-regex '.*file\.[0-9]{3}\.txt\.gz$' | \
while read file
do
output="$(basename "$file")"
output="$(dirname "$file")/"${output/#file/output}
echo "$file ==> $output"
done
Sample output
/home/ruslan/tmp/file.001.txt.gz ==> /home/ruslan/tmp/output.001.txt.gz
/home/ruslan/tmp/file.002.txt.gz ==> /home/ruslan/tmp/output.002.txt.gz
(for $dir=/home/ruslan/tmp/
).
Description
The scripts iterate the files in $dir
directory. The $file
variable is filled with the next line read from the find
command.
The find
command returns a list of paths corresponding to the regular expression '.*file\.[0-9]{3}\.txt\.gz$'
.
The $output
variable is built from two parts: basename (path without directories) and dirname (path to file's directory).
${output/#file/output}
expression replaces file with output at the front end of $output
variable (see Manipulating Strings)