Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Hi all,
I've been hunting on google for this with no luck. It seems to crop up a lot for me so I thought there might be someone out there who could help me.
I often want to rename all files containing some string to something else, for example I might want to rename
for file in `find ./ -name "application_testing*"`; do file=${file#./*}; mv -v ${file} google${file#application}; done
You could also do:
for file in application_testing.*; do mv -v ${file} google${file#application}; done
If you want to rename files also in subdirectories, you need to decompose the directory and basename parts and change just the basename:
for file in `find ./ -name "application_testing*"`; do targetdir=`dirname $file`; bname=`basename $file`; mv $file ${targetdir}/google${bname#application}; done
Remember, that if its possible that the filenames contain whitespace or special shell characters, you need to put the variables inside double quotes: "${file}" or "${file#application}"
With the find command, if the pattern you are searching for uses wildcards, you need to add double quotes around the pattern, so that the shell doesn't expand the wild cards first.
Your original
find . -name application_testing*
would expand to: find. -name application_testing.cpp application_testing.mnt application_testing.mnt application_testing.sht
I've written a small script to do it now (and added some other useful stuff )
I have one other question though, I have to run the script a few times to make sure that it's renamed all the subdirectories and their subfiles - the reason being that it renames the top level directory first and then can't find the subdirectories any longer as the path has changed. Is there any way I can force it to begin renaming from the deepest level first?
First, of all, you could select just files by using the -type f option to the file command. Your description was about changing the names of files.
You could try the -mindepth options:
- Option: -mindepth levels
Do not apply any tests or actions at levels less than LEVELS (a
non-negative integer). `-mindepth 1' means process all files
except the command line arguments.
Also look at the -depth option.
The find man page would make a good read because it is such a powerful utility.
If you 'man' command has the option try
man -Tdvi find >find.dvi
or
man -Tps find >find.ps
to produce a dvi or post script file that is suitable for printing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.