Find and rename file recursively


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and rename file recursively
# 8  
Old 10-20-2015
Quote:
Originally Posted by krsnadasa
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

${f} is the same that $f, the variable, right?
There are two parts in shell expansion ${f%.*}, the `%' and the glob pattern `.*' (this is not a regular expression), in this case a dot followed by anything.
The `%' says remove starting from the end of the string as minimum as possible of that which matches the pattern.
If there were two `%%' it would be as much as it is able to match the pattern, starting at the utmost right of the string.

An example:
Code:
[aia@ludus krsnadasa]$ f="krsnadasa.original"
[aia@ludus krsnadasa]$ echo "$f"
krsnadasa.original
[aia@ludus krsnadasa]$ echo "${f%.*}"
krsnadasa
[aia@ludus krsnadasa]$ echo "${f%.*}.bak"
krsnadasa.bak

mv -v "$f" "${f%.*}.bak" expands to mv --verbose krsnadasa.original krsnadasa.bak

Last edited by Aia; 10-20-2015 at 02:20 PM..
This User Gave Thanks to Aia For This Post:
# 9  
Old 10-21-2015
Thanks RudiC and Aia

I just went through documentation of Bash Paramter expansion. Post that these things became quite clear to me. Thank you for your time..
 
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