LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Recursively rename some files (http://www.linuxquestions.org/questions/programming-9/recursively-rename-some-files-891191/)

mail4mz 07-11-2011 10:09 PM

Recursively rename some files
 
Hello,

I have one directory with 3 level sub-directories, and about houndard files under those directories. I need a shell script to rename all patern mateched directories and files.



For example: the patern is AA in the directory or file name.

Orignal directory:
Dir1-->Dir_AA-->file_AA.txt
Dir1-->Dir_BB-->file1_AA.txt
Dir1-->Dir_CC-->Dir_BB-->file2_BB.txt

Target directory:
Dir1-->Dir_ZZ-->file_ZZ.txt
Dir1-->Dir_BB-->file1_ZZ.txt
Dir1-->Dir_CC-->Dir_BB-->file2_BB.txt

Thanks in advance,

Mike

nooneknowme 07-12-2011 02:27 AM

A simple find command will do for one pattern. use the "-exec mv" option with find.

grail 07-12-2011 02:40 AM

I would caution you about how 'simple' this might be. looking at your input data if you change the name of the directory prior to trying to change the name of the file
underneath (first line of your example) it will fail. Just something to watch out for.

I would probably suggest outputting your find into a loop and sorting it so these occur in the correct order.

MTK358 07-12-2011 07:01 AM

Code:

find Dir1 -depth -exec rename AA ZZ '{}' ';'

grail 07-12-2011 09:50 AM

@MTK358 - didn't seem to work for me :(
Code:

$ find Dir1/ -depth -exec rename AA ZZ '{}' ';'
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.
Bareword "AA" not allowed while "strict subs" in use at (eval 1) line 1.


MTK358 07-12-2011 09:58 AM

I tried it, and it works perfectly for me. Maybe it has something to do with the version of rename or find. Here's mine:

Code:

$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)
$ rename --version
rename (util-linux 2.19.1)


grail 07-12-2011 11:24 AM

The ubuntu rename is a perl script.

mail4mz 07-12-2011 11:37 AM

1 Attachment(s)
Hi All,

It seems the script is able to change rename of the directories, but not the files under directories. I attached the error as followings.

Thanks again!

Mike

MTK358 07-12-2011 12:18 PM

It seems like rename is trying to rename the directory, even though it's not the last component in the path. I haven't before tested it with a directory that has to have its name changed, and it fails for me, too.

Maybe this will work:

Code:

while read file
do
    mv "$file" "$(dirname "$file")/$(basename "$file" | sed 's/AA/BB/g')"
done < <(find Dir1 -depth)


grail 07-12-2011 12:47 PM

The error you are seeing in post #8 is exactly what I was referring to in post #3.
The issue is not:
Quote:

It seems like rename is trying to rename the directory, even though it's not the last component in the path.
The issue is that rename works on the entire string, hence once it renames the directory it can no longer see the file.

MTK358 07-12-2011 01:01 PM

Quote:

Originally Posted by grail (Post 4412854)
The issue is not

Actually, it is. It tries to modify the whole string, therefore it modifies names that are not the last element of the path.

mail4mz 07-12-2011 02:35 PM

1 Attachment(s)
It may not be a good script, here is what I have, any comment?

MTK358 07-12-2011 02:49 PM

Quote:

Originally Posted by mail4mz (Post 4412929)
It may not be a good script

I guess it would work, but you told us that you have to modify the original directory tree, but now you're creating a new one. Why didn't you say that that's wahat you wanted in the first place? Second, use $(command) instead of backticks. It looks better and nests more easily. Finally, it's always good to double-quote all variables in case filenames have spaces.

Also, use text editors for code (not word processors) since they are designed for coding rather than writing documents. And have you ever heard of copy and paste?

mail4mz 07-12-2011 04:33 PM

Thanks MTK for the comments. I actually want to have rename the files under the same directory. However I am not able to find out how to do it.

Now I want to use SED to modify the content of each file. Unfortunatly, the following script does NOT work for me, :-(

sed -f ttt.sed < $sourcefile > $sourcefile

Any suggestion on using sed to change the content of multiple files?

MTK358 07-12-2011 04:55 PM

Quote:

Originally Posted by mail4mz (Post 4413043)
Thanks MTK for the comments. I actually want to have rename the files under the same directory. However I am not able to find out how to do it.

Did you look at my previous posts?

Quote:

Originally Posted by mail4mz (Post 4413043)
Now I want to use SED to modify the content of each file. Unfortunatly, the following script does NOT work for me, :-(

sed -f ttt.sed < $sourcefile > $sourcefile

Any suggestion on using sed to change the content of multiple files?

So you want to rename them and modify their contents?


All times are GMT -5. The time now is 08:10 AM.