Find and rename file recursively


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and rename file recursively
# 1  
Old 10-05-2015
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 :
Code:
find /home/application/test -name '*.txt'  -exec rename  's/txt/bak/'  {}  \;

seems to me like it is not the standard way of doing this, is there any other way using combination of find and sed??
# 2  
Old 10-05-2015
Are you sure about the command rename to me sounds like Tcl... why did you not use mv?
# 3  
Old 10-05-2015
Quote:
Originally Posted by vbe
Are you sure about the command rename to me sounds like Tcl... why did you not use mv?
Tried using mv, but no luck
Code:
find /home/arpu/test -name '*.txt'  -exec mv '*.txt' '*.bak'  {}  \;

# 4  
Old 10-05-2015
Quote:
mv '*.txt' '*.bak'
can't work as you can't have wildcards in the target.

There's a perl tool called rename that you might have access to. man rename:
Quote:
For example, to rename all files matching "*.bak" to strip the extension, you might say

rename 's/\.bak$//' *.bak
You wouldn't even need find for your task.
# 5  
Old 10-05-2015
Quote:
Originally Posted by mukulverma2408
Tried using mv, but no luck
Code:
find /home/arpu/test -name '*.txt'  -exec mv '*.txt' '*.bak'  {}  \;

Try
Code:
find /home/arpu/test -type f -name '*.txt' | while read f; do mv -v "$f" "${f%.*}.bak"; done

-type f is there so there is not chance of renaming a directory named something.txt, if exists

Last edited by Aia; 10-05-2015 at 02:58 PM.. Reason: grammar correction
This User Gave Thanks to Aia For This Post:
# 6  
Old 10-20-2015
Hi Aia

I would like to know the bit explanation of the piece of below command ...
Code:
find /home/arpu/test -type f -name '*.txt' | while read f; do mv -v "$f" "${f%.*}.bak"; done

Please explain below cmd in mv command..
Code:
"${f%.*}.bak

# 7  
Old 10-20-2015
man bash:
Quote:
Parameter Expansion
The `$' character introduces parameter expansion, command substitution, or arithmetic expansion.
.
.
.
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the
expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ``%'' case) or the
longest matching pattern (the ``%%'' case) deleted.
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find file type recursively and move

Hello, I supposed that it was working fine but now I see that it's not working as expected. I am running under ubuntu14.04, trusty. My plan was to search folderA and all subdirectories and move any txt file to destination folder, folderB : find /home/user/folderA/ -type f -iname "*.txt"... (0 Replies)
Discussion started by: baris35
0 Replies

2. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

3. 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

4. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

5. 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

6. Shell Programming and Scripting

Recursively rename directories

I have this directory tree under /apps/myapp/data: imageshack.us/photo/my-images/703/foldersc.png How to recursively rename ONLY directories with 5 digits (00000, 00100, 00200,..., 00007, 00107,...)? I want to add to their name two more zeros: Before: 00107 After: 0000107 Thanks in... (2 Replies)
Discussion started by: Susan_45
2 Replies

7. Shell Programming and Scripting

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:... (2 Replies)
Discussion started by: mail4mz
2 Replies

8. Shell Programming and Scripting

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: 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... (6 Replies)
Discussion started by: rbisconti97
6 Replies

9. Shell Programming and Scripting

Rename files recursively

hi I have files named 123_234_aaa.jpg 234_231_345.jpg and i wish to rename these files to aaa.jpg and 345.jpg. i.e inital number,_,next number should be removed form the file name. Please let me know how can i do it. (2 Replies)
Discussion started by: vasuarjula
2 Replies

10. UNIX for Dummies Questions & Answers

Batch rename recursively

I would like to replace multiple underscores with hyphens but I have 26,000 files to rename. They are all in one file structure and multiple sub-directories. It would be much simpler if I had a script to do it. Here are some samples of the file names: Example 1... (3 Replies)
Discussion started by: ..Chris..
3 Replies
Login or Register to Ask a Question