specific file name renaming in all sub directories
Hello,
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 |
man find
man mv |
I tried that
Quote:
|
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) Let us know if you get stuck? |
grail, thanks for your advice, this is what I came up with:
find /path -name 'filename' | while read file ;do ; newfile=${filename%}".xml" ; mv $file $newfile ; done what am I doing wrong here ? |
What errors are you getting?
you could try something like (not the exact soln) Code:
set -xv NB: 1st post is html files, now you're talking xml ....? |
Yet another way to do it (isn't Linux wonderful?):
Code:
#Recursively rename some to other Code:
#Recursively rename some to other I didn't originate the script. I found it using www.google.com/linux and searching for find mv rename. I also found many others, but this one seemed the most elegant. |
Quote:
The loop now reads each line (remembering that find will not only return the filename but the full path from /path) into the variable file. Unfortunately you then in your code to set the newfile name reference what you are searching for and not what you have found: Code:
newfile=${filename%}".xml" Hence why the examples provided by others are a little more complex. |
Quote:
chrism01, bigrigdriver, grail, I have tried all of your commands in various ways but no luck: 1 example: Jack@opsu:/opt/lampp/htdocs/html/Ready/socal/> for FILE in $(find . -name oldfile.html) > do > NEW=$(echo $FILE | sed -e 's/oldfile.html/newfile.xml/') > mv "$FILE" "$NEW" > done Jack@opsu:/opt/lampp/htdocs/html/Ready/socal/> nothing happens... |
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. |
You do realise that executing an mv command does not have any output?
You can test it by the following: Code:
$ touch file1 I would first suggest you go and look for the file 'oldfile.html' to see if it still exists. Assuming it existed prior to running the script it should now obviously be 'newfile.xml' Should you wish to see some output you can use the '-v' switch with mv to get verbose output. |
Quote:
Quote:
If '$' is not a typo you can use command-not-found to lookup the package that contains it, like this: cnf $ BTW - Thanks for all your help people. |
$ - this is simply the prompt ... ie it is not something to be typed
Quote:
Quote:
|
Great, that did exactly what I needed :) thank you for all your help guys.
|
Well I am glad you got it working. Just thought I would show you what i meant based on your original example:
Code:
find /path -type f -name 'file123.html' -exec rename 's/123/456/' {} \; |
All times are GMT -5. The time now is 12:07 PM. |