LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Mass renaming directories (https://www.linuxquestions.org/questions/linux-newbie-8/mass-renaming-directories-4175573019/)

chris_crunch 02-23-2016 09:40 AM

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]

MensaWater 02-23-2016 10:05 AM

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.

suicidaleggroll 02-23-2016 10:10 AM

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.

TB0ne 02-23-2016 10:18 AM

Quote:

Originally Posted by chris_crunch (Post 5504834)
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.

chris_crunch 02-23-2016 10:19 AM

Brilliant, thank you

chris_crunch 02-23-2016 10:25 AM

Quote:

Originally Posted by TB0ne (Post 5504845)
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.

TB0ne 02-23-2016 10:31 AM

Quote:

Originally Posted by chris_crunch (Post 5504850)
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.

hydrurga 02-23-2016 10:44 AM

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.

grail 02-23-2016 10:56 AM

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)

MensaWater 02-23-2016 01:01 PM

Quote:

Originally Posted by suicidaleggroll (Post 5504841)
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


MensaWater 02-23-2016 01:07 PM

Quote:

Originally Posted by TB0ne (Post 5504845)
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.

TB0ne 02-23-2016 01:12 PM

Quote:

Originally Posted by MensaWater (Post 5504944)
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.

BW-userx 02-23-2016 03:03 PM

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


All times are GMT -5. The time now is 08:06 PM.