Trying to find and rename files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to find and rename files
# 1  
Old 03-24-2016
Trying to find and rename files

but it's not working.

Hello all.

I'm running the following command to find files with a specific name and rename them, but the command prompt returns a short 10 seconds after executing and doesn't find or rename anything.

What am I doing wrong here?

Code:
find . -type f | for file in *_D.pdf; do new=`echo $file | cut -c-20`; mv $file $new.pdf; done

I should mention there are A LOT of files in the directories.
# 2  
Old 03-24-2016
try something like:
Code:
find . -type f -name "*_D.pdf" | while read file ; do new=`echo $file | cut -c-20` ; mv "$file" "$new.pdf" ; done

# 3  
Old 03-24-2016
Depending on the shell that you are running, this will speed up the processing of A LOT of files:
Code:
find . -type f -name "*_D.pdf" | while read file ; do mv "$file" "${file:0:20}.pdf" ; done

Are you sure that 20 chars will contain the relevant part of the files?
# 4  
Old 03-24-2016
Quote:
Originally Posted by rdrtx1
try something like:
Code:
find . -type f -name "*_D.pdf" | while read file ; do new=`echo $file | cut -c-20` ; mv "$file" "$new.pdf" ; done


Tried that first but it gave the same error. So I removed the -name, thinking it would not look for the file twice.

---------- Post updated at 03:57 PM ---------- Previous update was at 03:54 PM ----------

Quote:
Originally Posted by RudiC
Depending on the shell that you are running, this will speed up the processing of A LOT of files:
Code:
find . -type f -name "*_D.pdf" | while read file ; do mv "$file" "${file:0:20}.pdf" ; done

Are you sure that 20 chars will contain the relevant part of the files?
This sure was faste, but it's not generating the correct new filename. I'm not familiar with ${file:0:20}. What does this do?

---------- Post updated at 03:59 PM ---------- Previous update was at 03:57 PM ----------

Quote:
Originally Posted by bbbngowc
Tried that first but it gave the same error. So I removed the -name, thinking it would not look for the file twice.

---------- Post updated at 03:57 PM ---------- Previous update was at 03:54 PM ----------



This sure was faste, but it's not generating the correct new filename. I'm not familiar with ${file:0:20}. What does this do?
Oh I see what it does. I just need to modify it to 26 and it works.

Many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename lots of files with find?

Can someone help me with this script. I have a bunch of files like this: "2209OS_02_Code" "2209OS_03_Code" "2209OS_04_Code" "2209OS_05_Code" "2209OS_06_Code" "2209OS_07_Code" "2209OS_08_Code" "2209OS_09_Code" "2209OS_10_Code" "2209OS_10_video" and I want to rename them to be like this: ... (2 Replies)
Discussion started by: siegfried
2 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. UNIX for Advanced & Expert Users

Find Gzip rename and mv

Hi all, what i'm trying to configure its to the following, find all files older then 1 min,gzip them ,rename/move with date and extension .gz (example tes.log_2012-07-26.gz) and trying to move them to another folder (gzipped),the command i'm typing its this, find /home/charli/Desktop/test/ -type... (4 Replies)
Discussion started by: charli1
4 Replies

4. Shell Programming and Scripting

Find directories with same name and rename them

Hello Im trying to make a script in bash shell programming to find subdirectories with the same name into the same directory and rename one of them!! Could you please help me? Thanks in advance (1 Reply)
Discussion started by: BTKBaaMMM
1 Replies

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

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

7. Shell Programming and Scripting

Find last updated files and rename

HI I have a requirement to find the last updated files from a directory whcih has subdirectories and inside them we have files with .txt,.doc,.xls .. extensions. i have to find those files which were updated in the last 1hr and rename the files with respective <sub-directory>_<filename> and copy... (3 Replies)
Discussion started by: ramse8pc
3 Replies

8. Shell Programming and Scripting

Find and rename

Hi, I was wondering if there is a way to find a particular file and then give it as an input to a program and then dump it into another file. Something like this: find ./ -name '*.txt' -exec ~/processText {} > mod.<current_file> \; I've been trying all sorts of weird things but not... (2 Replies)
Discussion started by: Legend986
2 Replies

9. Shell Programming and Scripting

Find, Append, Move & Rename Multiple Files

Using a bash script, I need to find all files in a folder "except" the newest file. Then I need to insert the contents of one text file into all the files found. This text needs to be placed at the beginning of each file and needs a blank line between it and the current contents of the file. Then I... (5 Replies)
Discussion started by: Trapper
5 Replies

10. UNIX for Dummies Questions & Answers

Find and rename all folders with name X.

Is there a command I can use to rename all directories with a certain name to a new name. For instance from my root directory I want to change all folders named '123' to '321' that are in the root directory or any subdirectory. Thanks in advance! (6 Replies)
Discussion started by: mkingrey
6 Replies
Login or Register to Ask a Question