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.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a very large number (~500) of files I need to rename. I suppose I could write a perl script to do it... but I'm looking for something quick and dirty.
I need to rename files that look like this:
EM76540.pdf
to this:
76540EM.pdf
Can I use bash with mv or am I doing to have to write some perl script?
I usually refer to this article for such tasks and can usually figure out how to come up with the right command through trial and error on some test subjects relatively quickly.
jschiwal that simple script did exactly what I needed. I can see why you're a guru. I really appreciate it. For me to do that with a perl script would have taken me hours I'm sure.
I haven't had a chance to visit that site you mentioned Junior Hacker - it stated it was too busy. But it looks like something I will definitely look at later.
Man... I need to learn more scripting. That's a handy tool.
Can anyone provide some advice on a more complicated example to rename files...
I have files in the example format:
Babypicture_playing_with_spoon_date=20070602
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
Babypicture_date=20070602.jpg
Where I am cutting out the middle descriptor and adding the .jpg at the end?
Will try it, but hopefully someone can guide me on some perl code for this too. Seem like bash fails for me when the list of files is to long - I get the "argument list to long" error
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
Code:
#!/bin/bash
for file in Baby*
do
newfile=${file:0:11}${file:$((${#file}-14))}.jpg
echo mv $file $newfile
done
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.
yes, I actually had done:
mv file="Baby*" newfile=${file:0:11}${file:$((${#file}-14))}.jpg
I strongly suggest to not try renaming/deleting/moving commands without knowing exactly what you're doing, that is without knowing the exact syntax! Unless you like living on the edge...
Occasionally, I do attempt living on the edge . But in this case, I copied some sample files to a test directory in order to perfect the renaming command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.