- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script to rename files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:30 AM
10-24-2001 05:30 AM
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 :-)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:34 AM
10-24-2001 05:34 AM
SolutionRather 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:40 AM
10-24-2001 05:40 AM
Re: Script to rename files
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:42 AM
10-24-2001 05:42 AM
Re: Script to rename files
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:44 AM
10-24-2001 05:44 AM
Re: Script to rename files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:47 AM
10-24-2001 05:47 AM
Re: Script to rename files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 05:55 AM
10-24-2001 05:55 AM
Re: Script to rename files
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
I hope this helps.
Does this help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2001 06:05 AM
10-24-2001 06:05 AM
Re: Script to rename files
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.