Thursday, January 27, 2011

Renaming Files

To rename all files in a directory and add a new extension the xargs command can be used:
ls | xargs -t -i mv {} {}.old
xargs reads each item from the ls ouput and executes the mv command. The ‘-i’ option tells xargs to replace ‘{}’ with the name of each item. The ‘-t’ option instructs xargs to print the command before executing it.
To rename a subset of files, specify the file names with the ls command:
ls *.log | xargs -t -i mv {} {}.old
Or to add a current timestamp extension you may want to use the date command similar to this one:
ls *.log | xargs -t -i mv {} {}.`date +%F-%H:%M:%S`
The extension will look like “.2006-08-10-19:37:16″.
If you want to rename the extension of files, try the rename command:
rename .log .log_archive.`date +%F-%H:%M:%S` *
This command replaces the first occurrence of ‘.log’ in the name by .log_archive.`date +%F-%H:%M:%S`.
The following command replaces .htm extensions with .html for all files that start with “project*”:
rename .htm .html project*

No comments:

Post a Comment