[SOLVED] specific file name renaming in all sub directories
Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
specific file name renaming in all sub directories
I have a dir (pub_html) with 45 sub dirs, and in each there is a file with name (file123.html) what command can I use to rename all files with this name in all sub dirs to file456.html ?
I'm on opensuse 11.3
Well what have you tried? Remember the idea is we are here to assist and not necessarily do the work for you.
I will assume that after 'man mv' you are comfortable with how to rename a file using this command?
As far as find goes, did you check out the EXAMPLES (not shouting here ... this is what you can search for) section?
Essentially you need the following:
1. starting directory
2. check out -type to limit to files (ie so you don't rename a directory)
3. -name (or one of the many derivatives to suit your needs) will need to be passed the type of file you are looking for (eg. .txt, .mp3, .avi, etc)
4. redirect this into a loop (suggest while loop)
I also noted that you are using Ubuntu. This being the case you may also wish to 'man rename' which would then change step 4. to:
4. check out -exec to have rename alter file.
Within the rename man page i would also look at the '-n' option as a test scenario (ie to check results are as expected before making change)
NB: 1st post is html files, now you're talking xml ....?
chrism01, there are about 31k files in total in those folders, all of which are .html, Im trying to change a specific name out of those .html to .xml with a differend file name, so when the 'find' finds oldfile.html it will change it to newname.xml
chrism01, bigrigdriver, grail, I have tried all of your commands in various ways but no luck:
Jack@opsu:/opt/lampp/htdocs/html/Ready/socal/> for FILE in $(find . -name oldfile.html)
> NEW=$(echo $FILE | sed -e 's/oldfile.html/newfile.xml/')
> mv "$FILE" "$NEW"
The -printf command in find can be used to generate scripts to do what you want.
This example will find files named 1234.html and rename them to 5678.html
find ./ -name 1234.html -printf 'mv -v %p %h/5678.html\n' >renamescript
You can review the script before executing, to prevent any surprises. For example, if a directory name has spaces in it, maybe using -printf 'mv -v "%p" "%h/5678.html"\n' >renamescript
would be useful.