Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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've been using Linux/Unix operating systems for several years now, just as a general-purpose OS and for my research in computational biochemistry. That said, I have little coding experience and have only recently begun trying to write simple shell scripts to automate some processes.
I have run into a bit of a hang-up in a script I'm trying to write. I would like to modify the extensions of a (large) series of files. I have read a number of threads about changing extensions (JPG --> jpg is a common one), but my problem is a bit different.
I have a series of files:
file.pdb.1
file.pdb.2
....
file.pdb.x
A .pdb file is a simple text file with atomic coordinates, so essentially it is just a text file. I would like to take these files (automatically output by another program, and I have no control over the output nomenclature) to names like:
file_1.pdb
file_2.pdb
(etc)
Does anyone know a simple way to do this (or even a complicated one)? I'm at a loss! Since the number after .pdb keeps changing, I haven't been able to come up with a simple search string to do an iterative find and replace.
I will precede the "mv" command with echo just to test it out. If you make a typo you might rename all of the files to the same thing, loosing all but one file.
Code:
for file in demo.pdb.*; do
echo mv -v "$file" "${file%.pdb.*}_${file##*.}.pdb"
done
For this version, the files are named demo.pdb.1, demo.pdb.2, etc. It uses variable expansion instead of an external program. Note the double quotes around the filenames. If you have variable whose value contains white space, you need to surround the variable with double quotes to prevent shell taking the value as multiple arguments.
After running the test you can hit the up arrow to bring back the command and delete the echo command.
My habit is calling everything "file" because I've used something similar to the above so many times.
Then I'll start with "for dir" but my finger memory will type "file" instead latter on instead of "dir". I think using "echo" was the more important point when working interactively. Imagine accidentally adding a space to an argument and having it expand to "rm ~/$olddocs /*". Or having a series of move commands expanded as: "mv file1.JPG file.JPG" "mv file2.JPG file.JPG". Especially if you are using a complicated sed command to convert the filename.
Yes, this is a case where I have learned the hard way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.