Find and Rename File using Terminal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and Rename File using Terminal
# 1  
Old 11-06-2010
Find and Rename File using Terminal

I need help finding a file through terminal and then renaming it automatically.

Here is what I have so far to find the file:
Code:
cd /User/Applications
find . */SourceM.app/banner.png | while read line; do mv "$line" banner-.png; done

I want the script to rename the file "banner.png" to "banner-.png"

The issue I have with the above script is that it stops partially through searching the directory.

Any help would be appreciated.

Thank you
# 2  
Old 11-06-2010
There's no particular reason for it to be stopping, though maybe you should be grateful it did: That'd systematically destroy every banner-*.png it finds, by moving it into the current directory, overwriting the last one.

---------- Post updated at 01:33 PM ---------- Previous update was at 01:28 PM ----------

Actually, because you told it to find . and never narrowed it down any more, it's going to rename each and every file and directory it finds into ./banner-.png. It may be freezing after the first move, unable to find anything else.

---------- Post updated at 01:36 PM ---------- Previous update was at 01:33 PM ----------

If there's anything left to operate on, you could try this:

Code:
# by default, find finds everything in the listed dirs.  -name 'banner.png' narrows it down to only that filename
find /path/to/search/in -name 'banner.png' |
while read FILENAME
do
        # replace /banner.png with /banner-.png, keeping full path
        NEWNAME="${FILENAME/\/banner.png/\/banner-.png}"
        mv -i "${FILENAME}" "${NEWNAME}"
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-06-2010
Thank you, but I got this error:
Code:
mv: cannot move `/User/Applications/3BE2961D-A38F-46D4-A061-8AAE31F77EF3/SourceM.app/banner.png' to `/User/Applications/3BE2961D-A38F-46D4-A061-8AAE31F77EF3/SourceM.app\\/banner-.png': No such file or directory

---------- Post updated at 03:23 PM ---------- Previous update was at 03:04 PM ----------

I got it figured out, this is what i did:
Code:
find /User/Applications/ -wholename "*/SourceM.app/banner.png" | while read FILENAME; do NEWNAME="${FILENAME/\/banner.png//banner-.png}"; mv -i "${FILENAME}" "${NEWNAME}"; done

# 4  
Old 11-07-2010
Code:
find /User/Applications/*/SourceM.app -type f -name "banner.png" | while read FILENAME
do
  NEWNAME=${FILENAME%.*}-.png
  mv ${FILENAME} ${NEWNAME}
done

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 11-07-2010
^ Thank you, that is a much better way! Here is my final code:
Code:
# SourceM
if [ -f /User/Applications/*/SourceM.app/banner.png ]
	then
find /User/Applications/*/SourceM.app -type f -name "banner.png" | while read FILENAME
do 
NEWNAME=${FILENAME%.*}-.png
mv ${FILENAME} ${NEWNAME}
done
fi;

Not sure if the "if statement" is really needed. I guess it would come in handy for directories I am replacing multiple banners in.
# 6  
Old 11-28-2010
Why does this not work?
Code:
find /User/Applications/*/Quick\ News\ UK\ Free.app/DownloadRSSFlashgForOtherApp.png | while read FILENAME; 
do NEWNAME=${FILENAME%.*}.bak; mv ${FILENAME} ${NEWNAME}; 
done;

The Directory has spaces, and when I printf $FILENAME, it only prints up to the first 'space'. So therefore it does not rename the file.
# 7  
Old 11-28-2010
Since your filename includes space, you have to quota it.

Code:
find /User/Applications/*/Quick\ News\ UK\ Free.app/DownloadRSSFlashgForOtherApp.png | while read FILENAME; 
do NEWNAME=${FILENAME%.*}.bak; mv "${FILENAME}" "${NEWNAME}"; 
done;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and rename file recursively

Hi, I have a directory which contains multiple files with .txt extension, i want to rename all these file to .bak extension using find command, this is what i've tried, please help me to correct this : find /home/application/test -name '*.txt' -exec rename 's/txt/bak/' {} \; seems to... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Find and rename part of a file

hi, Need your help. I need to write a script for below.. i have two files in directory /home/abc as below: Watch_20140203_abc.dat Watchnow_20140203_abc.dat I have to copy this file from /home/abc to /home01/home02 after that i have to rename the date part in above two files... (1 Reply)
Discussion started by: Vivekit82
1 Replies

3. Shell Programming and Scripting

How to find and rename of particular pattern in file.?

Hi Guys, I have folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below. Article_????_?????????.txt Below is the one which I tried but it doesn't works can you please help us.... (4 Replies)
Discussion started by: Vinoth Kumar G
4 Replies

4. Shell Programming and Scripting

Problem with Find and rename file

I want to find a file say IIFT and check its size is zero or not. If its zero then I have to rename anothe file say WWFT , which is in another folder to WWFT$Todaysdate. I tried below command: cd dir2 (*File WWFT is in dir2) find dir/ -type f -name 'IIFT*' -size 0 -exec mv WWFT... (3 Replies)
Discussion started by: ammbhhar
3 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace then rename file

Hi, This is probably quite simple for an expert, but I keep getting confused about the best approach, grep, awk, sed. What I have is a range of files numbered 1 to 100. They go file1.txt file2.txt and so on In each file I need to find and replace a couple of items and rename add a... (5 Replies)
Discussion started by: chickenhouse
5 Replies

6. UNIX for Dummies Questions & Answers

Split and Rename files using Terminal and bin/bash

I have a file named Me_thread_spell.txt that I want to split into smaller files. I want it to be split in each place there is a ;;;. For example, blah blah blah ;;; blah bhlah hlabl awasnceuir asenduhfoijhacseiodnbfxasd;;; oabwcuhaweoir;;; This full file would be three separate files... (7 Replies)
Discussion started by: mschpers
7 Replies

7. Shell Programming and Scripting

Find and Rename files using (find mv and sed)

In response to a closed thread for degraff63 at https://www.unix.com/shell-programming-scripting/108882-using-mv-find-exec.html the following command might do it as some shells spit it without the "exec bash -c " part: Find . -name "*.model" -exec bash -c "mv {} \`echo {} | sed -e 's//_/g'\`"... (0 Replies)
Discussion started by: rupert160
0 Replies

8. Shell Programming and Scripting

Find and rename long file names (html)

Hi Guys, I need a help. I have 1130 zip files. Each one of them has files including 1 html file with long file name (includes special charactors, Alphabetic and numbers). I have copied all 1130 zip files to my linux system and extracted using below command. Find . -name "*.zip" -exec... (7 Replies)
Discussion started by: Rajmani
7 Replies

9. UNIX for Dummies Questions & Answers

chm and pdf help in terminal (rename)

i have a cd of pdf and chm documents that i transfered from my windows xp box. i transfered the files to my linux box (ubuntu hardy). unfortunately, i used to be a windows GUI only user so the use of spaces and "( )" and underscores. now that i use linux, ive been using CLI for using and... (3 Replies)
Discussion started by: Texasone
3 Replies

10. UNIX for Dummies Questions & Answers

How to rename Xterm terminal in VNC session

Hi All, We are using VNC viewer to login to remote server. Remote server is running with Red Hat linux 3.0. Our problem is, we are unable to rename xterm terminal so is there any way we can rename? Thanks a lot in advance for your valuable inputs. Regards, Bachegowda (0 Replies)
Discussion started by: bache_gowda
0 Replies
Login or Register to Ask a Question