1832275 Members
1681 Online
110041 Solutions
New Discussion

Script to rename files

 
SOLVED
Go to solution
Preet Dhillon
Advisor

Script to rename files

Dear Colleagues,

I'm trying to rename files with an extension. The extension needs to be the name of the subdirectory the file is in. So, for example :

The file
/home/fred/myfile.txt
should be renamed to
/home/fred/myfile.txt.fred

The file
/home/fred/c-code/work/mycode.c
should be renamed to
/home/fred/c-code/work/mycode.c.work

The difficulty I'm having is that the path to the files can be several directories deep and I don't know how to identify the subdirectory in which the file lives. Can anyone help?

Many thanks in advance :-)
Nothing succeeds like excess
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Script to rename files

Hi:

Rather than giving you the answer (which in this case is easy); I'll do better by simply pointing you to two commands : basename and dirname. Man those commands and you will have just what you need and learn a little bit in the process.

Regards, Clay
If it ain't broke, I can fix that.
Robin Wakefield
Honored Contributor

Re: Script to rename files

Hi Preet,

Lots of ways to do this, e.g.

find /dirname -type f | awk -F/ '{print "mv",$0,$0"."$(NF-1)}'|sh

Leave out the |sh at then end to preview the action.

Rgds, Robin.
James R. Ferguson
Acclaimed Contributor

Re: Script to rename files

Hi Preet:

# cd
# find . -type f -name "*myfile" |xargs -i mv {} {}.old

This uses 'find' to descend subdirectories as necessary to find *files* matching the name "*myfile". For each instance found, the file will be renamed with the ".old" extension.

Regards!

...JRF...
Wodisch
Honored Contributor

Re: Script to rename files

Hello Preet,

in addition to the commands needed ("basename" and "dirname" and even "basename" processing the output of "dirname") you should be aware of the fact, that the *new* filename may already exit in the target directory!
Hence you will have to check BEFORE renaming wether the *new* name does already exist and NOT rename then...

Just my $0.02,
Wodisch
David Lodge
Trusted Contributor

Re: Script to rename files

As you haven't said where and how the script will run, here is my attempt, based one the script running in the current directory:

Pwd=$(pwd)
Sub=${Pwd##*/}
for i in *
do
[[ ${i} = "*" ]] && break
mv ${i} ${i}.${Sub}
done

If you have the full path for a file then you can do:

$ Filename="/var/tmp/dave/a.c"
$ Filename=${Filename%/*}
$ Filename=${Filename##*/}
$ print ${Filename}
dave

HTH

dave
Wim Rombauts
Honored Contributor

Re: Script to rename files

What directory do you need ?
If FILENAME is the variable that contains the name of the file including it's path, then
PATH=${FILENAME%/*} returns the full directory (/home/fred) in variable PATH
DIR=${PATH##*/} returns only the lowest directory entry (fred), even if the total path should be /home/group1/fred.
Above two commands are good to get only the directory name immediately above the file.

If you need the name of the directory immediately under /home, do the following :
UNDFERHOME=${FILENAME#*home/} returns everything after /home/ (=fred/file.txt)
DIR=${UNDERHOME%%/*} remvers ecerything from the first slash, leaving only "fred".

At last, just execute "mv $FILENAME $FILENAME.$DIR"

To get all file, use
for FILE in *.txt
do
<script> $FILE
done
If you need only to rename the files in a directory without checking subdirectories.
Else, use
find -name "*.txt" -exec <script> {} \;

I hope this helps.

Does this help.
Preet Dhillon
Advisor

Re: Script to rename files

Dear Colleagues,

Many thanks to you all for your prompt replies. I've assigned points for you all - Clay gets a couple extra for teaching me two new commands I hadn't come across before.

Thanks again, guys - you're the best.
Nothing succeeds like excess