LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-09-2004, 06:04 AM   #1
dhbiker
Member
 
Registered: Dec 2003
Location: Bristol
Distribution: Slackware 10.2
Posts: 196

Rep: Reputation: 30
mv files


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

application_testing.cpp
application_testing.sht
application_testing.shm
application_testing.mnt

to:

google_testing.cpp
google_testing.sht
google_testing.shm
google_testing.mnt

how can I do it? I thought it'd be something along the lines of:

find . -name application_testing* -exec mv {} google_testing*;

but that doesn't work. any thoughts?
 
Old 11-09-2004, 06:40 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This will work for files in the same directory.

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

Last edited by jschiwal; 11-09-2004 at 06:44 AM.
 
Old 11-09-2004, 07:08 AM   #3
dhbiker
Member
 
Registered: Dec 2003
Location: Bristol
Distribution: Slackware 10.2
Posts: 196

Original Poster
Rep: Reputation: 30
works a treat, thanks!
 
Old 11-09-2004, 08:32 AM   #4
dhbiker
Member
 
Registered: Dec 2003
Location: Bristol
Distribution: Slackware 10.2
Posts: 196

Original Poster
Rep: Reputation: 30
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?
 
Old 11-09-2004, 12:38 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Able to write files to MP3 player but no files written into actual device? olnex Linux - Hardware 0 11-11-2005 06:32 AM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 11:42 AM
How can konqueror view html files in .gz & .bz2 files directly? ailinzhe Linux - Software 5 05-24-2004 08:36 AM
xine plays only (a part of) the sound of a wmv, some divx files and crushes mp4 files bezoomny Linux - Software 1 03-10-2004 07:33 PM
How to filter files in files and files which are in a sub-directory with "grep"? Piero Linux - Newbie 9 08-29-2003 02:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration