ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
$ 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.
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)
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)
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.