LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2011, 09:09 PM   #1
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Rep: Reputation: Disabled
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
 
Old 07-12-2011, 01:27 AM   #2
nooneknowme
Member
 
Registered: Feb 2008
Location: Bangalore, India
Posts: 66

Rep: Reputation: 4
A simple find command will do for one pattern. use the "-exec mv" option with find.
 
Old 07-12-2011, 01:40 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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.
 
1 members found this post helpful.
Old 07-12-2011, 06:01 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
find Dir1 -depth -exec rename AA ZZ '{}' ';'
 
Old 07-12-2011, 08:50 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
@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.
 
Old 07-12-2011, 08:58 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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)

Last edited by MTK358; 07-12-2011 at 09:02 AM.
 
Old 07-12-2011, 10:24 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
The ubuntu rename is a perl script.
 
Old 07-12-2011, 10:37 AM   #8
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
Attached Images
File Type: jpg error_msg.JPG (27.9 KB, 6 views)
 
Old 07-12-2011, 11:18 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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)
 
1 members found this post helpful.
Old 07-12-2011, 11:47 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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.
 
Old 07-12-2011, 12:01 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by grail View Post
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.
 
1 members found this post helpful.
Old 07-12-2011, 01:35 PM   #12
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
It may not be a good script, here is what I have, any comment?
Attached Images
File Type: jpg renameDirFile.JPG (32.6 KB, 6 views)
 
Old 07-12-2011, 01:49 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by mail4mz View Post
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?

Last edited by MTK358; 07-12-2011 at 01:51 PM.
 
Old 07-12-2011, 03:33 PM   #14
mail4mz
LQ Newbie
 
Registered: Jul 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
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?
 
Old 07-12-2011, 03:55 PM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by mail4mz View Post
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 View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to rename files recursively rs232 Linux - General 3 05-08-2011 02:05 PM
Rename recursively unihiekka Linux - Newbie 10 05-26-2010 01:55 AM
[SOLVED] how to rename files recursively, only keeping last x digits plus extension furryspider Programming 2 11-29-2009 12:55 PM
Unrar and Rename to Parent Folder Recursively Vasto Linux - Newbie 9 10-16-2008 06:36 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM


All times are GMT -5. The time now is 07:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration