LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-23-2016, 10:40 AM   #1
chris_crunch
Member
 
Registered: Jan 2016
Location: Braintree, Essex
Distribution: Ubuntu 14.04
Posts: 107

Rep: Reputation: Disabled
Mass renaming directories


Hehe, hi it's me again...

So I have a load of directories which I've accidently put .tar on the end of them.

Code:
n01443537.tar  n01828970.tar  n02093859.tar  n02115913.tar  n02480495.tar  n02879718.tar  n03272562.tar  n03710637.tar  n03961711.tar  n04310018.tar  n04599235.tar
n01484850.tar  n01829413.tar
So the above are directories...

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]
 
Old 02-23-2016, 11:05 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
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.
 
2 members found this post helpful.
Old 02-23-2016, 11:10 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
Only thing I'd change in MensaWater's answer is the string replace. Echoing and piping to sed is a bit cumbersome when it can be done natively in bash
Code:
newdir=${olddir/.tar/}
Also some places in the script will break if there are any spaces in the path. If that's the case, it will need to be tweaked a bit.

Last edited by suicidaleggroll; 02-23-2016 at 11:12 AM.
 
1 members found this post helpful.
Old 02-23-2016, 11:18 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,280

Rep: Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527
Quote:
Originally Posted by chris_crunch View Post
Hehe, hi it's me again...
So I have a load of directories which I've accidently put .tar on the end of them.
Code:
n01443537.tar  n01828970.tar  n02093859.tar  n02115913.tar  n02480495.tar  n02879718.tar  n03272562.tar  n03710637.tar  n03961711.tar  n04310018.tar  n04599235.tar
n01484850.tar  n01829413.tar
So the above are directories...
So you have DIRECTORIES names as .tar FILES?
Quote:
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.
 
Old 02-23-2016, 11:19 AM   #5
chris_crunch
Member
 
Registered: Jan 2016
Location: Braintree, Essex
Distribution: Ubuntu 14.04
Posts: 107

Original Poster
Rep: Reputation: Disabled
Brilliant, thank you
 
Old 02-23-2016, 11:25 AM   #6
chris_crunch
Member
 
Registered: Jan 2016
Location: Braintree, Essex
Distribution: Ubuntu 14.04
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
So you have DIRECTORIES names as .tar FILES?

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.
 
Old 02-23-2016, 11:31 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,280

Rep: Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527
Quote:
Originally Posted by chris_crunch View Post
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.
 
Old 02-23-2016, 11:44 AM   #8
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 20 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909Reputation: 2909
If you ever fancy a GUI approach, I've found pyRenamer to be a useful wee utility.

To change directory names, remember to use View->Show Options and then select "Directories" in the options.

In this case, using Original file name pattern {X}.tar and Renamed file name pattern {1} gets the job done.
 
Old 02-23-2016, 11:56 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,799

Rep: Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065Reputation: 3065
Quote:
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.
Not sure just how hard you searched. The following on google :- bash remove extension
Yielded this as its first link :- http://stackoverflow.com/questions/9...ension-in-bash

And as far as I can tell, it tells you exactly how to remove the unwanted portion (just like suicidaleggroll's example)
 
1 members found this post helpful.
Old 02-23-2016, 02:01 PM   #10
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
Quote:
Originally Posted by suicidaleggroll View Post
Only thing I'd change ...
Code:
newdir=${olddir/.tar/}
Thanks - I haven't seen that usage before.
Code:
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 02:03 PM.
 
Old 02-23-2016, 02:07 PM   #11
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
Quote:
Originally Posted by TB0ne View Post
You don't say what/how you want to rename them TO
The OP's question seemed clear to me:
Quote:
would like to rename all of them to remove the .tar extension
He also made it clear he'd accidentally named them with the .tar extension.

There are two kinds of admins in the world. Those who have made mistakes and those who lie about having made mistakes.
 
1 members found this post helpful.
Old 02-23-2016, 02:12 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,280

Rep: Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527Reputation: 6527
Quote:
Originally Posted by MensaWater View Post
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.
Indeed.
 
Old 02-23-2016, 04:03 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147
I just want to hear the story about how he ended up with a slue of directories with the ends named "*.tar"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming directories in loop Sigma721 Linux - Software 3 08-13-2014 04:26 PM
[SOLVED] specific file name renaming in all sub directories Jack_R Linux - Newbie 15 04-07-2011 03:35 AM
[SOLVED] Renaming Directories Problem Crowey Programming 50 05-11-2010 08:04 PM
Renaming directories with Regex? Possible? smaddox Linux - General 9 11-09-2009 07:34 PM
Mass renaming? yanik Linux - General 6 02-16-2009 04:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration