Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Files

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
Hi Everyone

I have few directories and withing that files. I want to rename all the files in these directories.

Example

Directories

Larry Moe Joe John

Within that i have files i want to rename. The criteria for remaming is:

All files starting with jx should be change to jx.

Please help
 

man find



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
You want to rename the files from jxblahblah to jx.blahblah?

Here's a quick snippet to get you started:
Code:
for file in $(find . -type f -name "jx*")
do
    newfile=$(echo $file | perl -ne 's|^(.*/jx)([^/]*)$|$1.$2|g;print;')
    echo mv $file $newfile
done

Just go into the directory that contains Larry Moe Joe John and run the above command (or put it in a script).
You can replace the perl part with sed, awk - whatever you prefer. I prefer perl to do substitutions, but you can use whatever tool you wish. The main point was to show how you can use find to get a list of files in different subdirs. You can also replace the period right after find with a directory (like /home) so you don't even have to be in the directory when you run this code.

Hope that helps - just a start...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top