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.
cd to the top level directory you want (e.g. /, /root, /usr)
Run:
Code:
for dir in $(find . -type d -name "*.tar")
do parentdir=$(dirname $dir)
olddir=$(basename $dir)
newdir=$(echo $olddir |sed -e s/.tar//)
echo "Renaming $olddir to $newdir in $parentdir"
mv ${parentdir}/$olddir ${parentdir}/$newdir
done
Note that the sed would remove any occurrence of .tar in the directory name so if you had a directory like ralph.tar.billybon.tar it would change that to ralphbillybob. One assumes you don't have names like that but if you do you'd have to modify the sed to only do the one at end of line.
The above only changes directories and not valid files with .tar extensions due to the initial find command.
I would like to rename all of them to remove the .tar extension. Has anyone got any ideas of how to do this? [there's a LOT of them]
Solutions have been provided...you may also want to look at the "rename" command. You don't say what/how you want to rename them TO, or give any details. Read the "Question Guidelines" link in my posting signature. Doing basic research first, before posting a question, is always a good thing...this is a recurring theme in many of your posts. Putting "linux rename many directories" into Google pulls up MANY examples.
Solutions have been provided...you may also want to look at the "rename" command. You don't say what/how you want to rename them TO, or give any details. Read the "Question Guidelines" link in my posting signature. Doing basic research first, before posting a question, is always a good thing...this is a recurring theme in many of your posts. Putting "linux rename many directories" into Google pulls up MANY examples.
Of course I did Google this first. But it was tricky to remove the .tar extension from the files- that was the bit I didn't understand. There are no examples of removing the end of the directory name, as far as I could understand.
And the provided solutions worked wonders. I didn't believe any other details were necessary.
Of course I did Google this first. But it was tricky to remove the .tar extension from the files- that was the bit I didn't understand. There are no examples of removing the end of the directory name, as far as I could understand.
Removing the .tar extension would be part of the renaming process...since that would, obviously, involve changing the name. And again, putting the previously mentioned search-terms into Google pulled up many solutions to mass-rename things.
Quote:
And the provided solutions worked wonders. I didn't believe any other details were necessary.
Details are ALWAYS necessary...as a mathematician that should be self-evident.
Of course I did Google this first. But it was tricky to remove the .tar extension from the files- that was the bit I didn't understand. There are no examples of removing the end of the directory name, as far as I could understand.
for dir in $(find . -type d -name "*.tar")
do parentdir=$(dirname $dir)
olddir=$(basename $dir)
#newdir=$(echo $olddir |sed -e s/.tar//)
newdir=${olddir/.tar/}
echo "Renaming $olddir to $newdir in $parentdir"
# mv ${parentdir}/$olddir ${parentdir}/$newdir
done
Last edited by MensaWater; 02-23-2016 at 01:03 PM.
The OP's question seemed clear to me:
He also made it clear he'd accidentally named them with the .tar extension.
Very true, and I said as much. But I am always hesitant to ASSUME that's all, since the files seemed to follow some sort of naming convention, which (to me), said further renaming (such as n01828970.tar moving to N-01828970) may have been in order, which is why I asked.
Quote:
There are two kinds of admins in the world. Those who have made mistakes and those who lie about having made mistakes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.