Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Can anyone provide some advice on a more complicated example to rename files...
I have files in the example format:
Where the front of the file name is always a constant number of characters (here its 11 'Babypicture'), the middle is a descriptor, and the end is a constant number of characters for the date (14 characters, _date=20070602), with no end descriptor (.jpg is not there)
How can I rename these, using perl, to
Where I am cutting out the middle descriptor and adding the .jpg at the end?
Of course. Mine was only a little guidance on how to build the new file name in BASH, based on your requirements. You have to embed it in a little script which retrives all the file names and rename them one at a time.
Note: at this point, since it looks like you are not experienced in shell programming, you have to do a backup of your pictures/files before trying to rename them!
A simple script could be
for file in Baby*
echo mv $file $newfile
This works by looping over the list of files whose name begin with "Baby" and assigns each file name to the variable "file", one at a time. Then build the new file name and finally perform the mv command to rename it. I intentionally put an echo in front of the mv command to let you verify what the script will do, before actually doing it: when you've verified that the mv commands are good, strip out the echo command and launch the script again.
This code works only 1) if the files are all in the working directory and 2) if file names don't contain blank spaces, otherwise you have to slightly modify the code to let it work properly.