Perl Oneliners: rename files
Image you have some files in the directory. You heed to rename them added some prefix. This is a simple Perl code can do it:
find . | Perl -ne 'chomp; next unless -e; $oldname = $_; s#\./(.+)#prefix_$1#; next if -e; rename $oldname, $_;'
Let’s see what we do. First of all we get a list of files using command find and pass it to our oneline Perl script. The option-e is used to specify Perl expressions to be run and -n is used to create loop over input without printing anything.
Inside our Perl expression we get a file name, remove end of line from it, then check if the file with this name is not exists get next one. Then we store the file name in the temporary variable, add prefix to the file name, check if file with new name is exists (get the next file if yes) and remane inputed file.
It’s pretty easy to add more complicated processing of file names and additional checkin (file type, for example).