Quick script to rename files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick script to rename files
# 15  
Old 11-16-2014
Quote:
Originally Posted by jonesal2
Thanks for all the replies, I wasn't expecting it to be that complex.

I sort of thought the script might simply search the filename string for the first underscore and delete it plus all the characters before it, then search for (what would then be) the third underscore delete it plus the next 8 characters (since the time stamp is always YYYYMMDD)?

Or am I being too simplistic?
Not at all.
There are several examples here which do exactly that; excluding and/or deleting.
Could you post your version?
# 16  
Old 11-17-2014
So I have tried sed:
Code:
sed 's/^[^_]* - //'

Nothing really happened so I escaped that

Then I tried the perl script rename
Code:
 rename s/_\.// *.jpg

Which remove the _ and the first letter of the surname to look like
Code:
xurname_firstname_y_20141115_OS_(z).jpg

So then I tried:
Code:
 rename s/*_\.// *.jpg

but got
Code:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE _./ at (eval 1) line 1.

:-/
# 17  
Old 11-17-2014
Quote:
Originally Posted by Scrutinizer
Hi Don,

That pertains to the RHS of the pipeline, which can be executed in a subshell or in the foreground. The LHS is always executed in a subshell.

This can easily be checked:
Code:
for i in dash bash ksh zsh
do 
  shl=$i $i -c "{ u=1 ;} | { v=1;}; echo \"\$shl: \\\$u:\$u,\\\$v:\$v\""
done

Code:
dash: $u:,$v:
bash: $u:,$v:
ksh: $u:,$v:1
zsh: $u:,$v:1

This shows that ksh and zsh execute the RHS in the foreground. The RHS in the other shells and the LHS in all shells is executed in a subshell. In bash 4 there is a separate setting (not the default) that can be turned on so that the RHS is also executed in the foreground.

The process id within a subshell cannot be tested with $$. A subshell is a child process that inherits the environment of the parent shell, including the variable $$. Therefore in a subshell of the parent shell, $$ will represent the parent's $$.


Shell Command Language


The pid of a subshell can still be checked by starting a child process that is not a subshell and checking $PPID:
Code:
sh -c 'echo $PPID'

I used the following script to test:

Code:
for i in dash bash ksh zsh
do
  shl=$i $i -c "{ echo \"\$shl:LHS:\$(sh -c 'echo \$PPID')\">>tst.out;} | { echo \"\$shl:RHS:\$(sh -c 'echo \$PPID')\">>tst.out;}; echo \$shl:parent:\$\$ >> tst.out"
done

Code:
$ cat tst.out
dash:RHS:57607
dash:LHS:57606
dash:parent:57605
bash:LHS:57613
bash:RHS:57614
bash:parent:57610
ksh:LHS:57618
ksh:RHS:57617
ksh:parent:57617
zsh:LHS:57623
zsh:RHS:57621
zsh:parent:57621

Again this shows that the RHS of pipelines in ksh and zsh are executed in the foreground, but the rest are not..


Therefore using a pipeline in the file moving loop, earlier in the thread requires (2n+1) or (3n+1) processes including the ones for the mv command (depending on the shell that is used), while using a heredoc/string or parameter expansions leads to (n+1) processes.
Yes; you're absolutely correct.

Of course the standards allow every element of a pipeline to be executed in the current shell execution environment, but none of the current shells are implemented that way.
# 18  
Old 11-17-2014
Guys,

I have read through the posts as best I can, just a reminder that x, y & z represent variable number so can't be hard coded. One other thing which I should have mentioned is that OS could also be OD in some cases.

Thanks
# 19  
Old 11-17-2014
For a quick-and-save renaming, I would install "mmv"
and run
Code:
mmv "*_*_*_*_*_*" "#2_#3_#4_#6"

if the reference character is #, like in this man page.
If not available for your distro, download+compile mmv.c
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 20  
Old 11-17-2014
I think Scrutinizer's post#11 is a good starting point. Use a glob like ...*_O[SD]_*... and adapt / validate $first and $last against e.g. a date or so.
# 21  
Old 11-17-2014
Quote:
Originally Posted by jonesal2
So I have tried sed:
Code:
sed 's/^[^_]* - //'

Nothing really happened so I escaped that

Then I tried the perl script rename
Code:
 rename s/_\.// *.jpg

Which remove the _ and the first letter of the surname to look like
Code:
xurname_firstname_y_20141115_OS_(z).jpg

So then I tried:
Code:
 rename s/*_\.// *.jpg

but got
Code:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE _./ at (eval 1) line 1.

:-/
Gee, I can't possibly guess what might be wrong.

You told us you wanted to rename files whose names were of the form:
Code:
x_surname_firstname_y_20141115_OS_(z)

and we made suggestions based on that format. Note that the filenames in this format end with a ); not a ).jpg and that makes a HUGE difference in the attempts to limit the patterns and REs used to match just the files you said you want to process.

You could try this (which should work with your original file naming specification with or without a trailing filename extension):
Code:
#!/bin/ksh
for i in *_*_*_*_*_O?_\(*\)*
do	printf '%s__%s\n' "$i" "$i"
done | while IFS="_" read junk s f y junk o z empty old
do
	[ "$empty" = "" ] || continue # Protect against files with extra "_"s.
	echo mv "$old" "${s}_${f}_${y}_${o}_$z"
done

When run in a directory containing the files:
Code:
-rw-r--r--  1 dwc  staff    0 Nov 16 00:45 123_surname_firstname_y_20141115_OD_(456)
-rw-r--r--  1 dwc  staff    0 Nov 17 01:29 extra_x_surname_firstname_y_20141115_OS_(z)
-rw-r--r--  1 dwc  staff  642 Nov 15 12:36 problem
-rwxr-xr-x  1 dwc  staff  241 Nov 17 01:40 tester
-rw-r--r--  1 dwc  staff  147 Nov 16 00:43 tester2
-rwxr-xr-x  1 dwc  staff  146 Nov 16 00:47 tester3
-rw-r--r--  1 dwc  staff    0 Nov 15 12:36 x_surname_firstname_y_20141115_OS_(z)
-rw-r--r--  1 dwc  staff    0 Nov 17 01:29 x_surname_firstname_y_20141115_OS_(z).jpg

it produces the output:
Code:
mv 123_surname_firstname_y_20141115_OD_(456) surname_firstname_y_OD_(456)
mv x_surname_firstname_y_20141115_OS_(z) surname_firstname_y_OS_(z)
mv x_surname_firstname_y_20141115_OS_(z).jpg surname_firstname_y_OS_(z).jpg

If it displays the mv commands you want to run, remove the echo shown in red and run the script again to actually rename the files.

If you have other files in this directory that contain exactly six underscores and a pair of parentheses after the last underscore that you want to exclude, we can tighten up the pattern to avoid false matches (if you'll show us the format of the filenames we need to avoid). As written this string will let x, y, and z can be any strings of non-underscore characters.

(This script will use n+2 processes to rename n files.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script for rename many files

Hello friends! I have a problem with my script. I'm a italian boy. Sorry for my english ehehehehehhe. I've many files .jpg and I would like rename they in this mode: I have not files with progressive number e I would like rename with progressive number. Example: DSC_0012.JPG DSC_0582.JPG... (7 Replies)
Discussion started by: vegetablu
7 Replies

2. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

3. Shell Programming and Scripting

Rename files in the script

Hi All, I want to write a script to rename the file in to the incremental order for example Original file filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img filename=/nfs/n1/file1.img I want output shpuld be... (4 Replies)
Discussion started by: mangeshpardhi
4 Replies

4. Shell Programming and Scripting

Script to rename files

I have the following directories in my home directory, my scripts dbmig es ms_done my-home I want my output to look like the following MyScripts DbmigEs MsDone MyHome Basically, I want to get rid of spaces,special characters and convert the first letter of each word to uppercase and... (1 Reply)
Discussion started by: ramky79
1 Replies

5. Shell Programming and Scripting

Script Rename files

Hello, I have this problem. In a directory I have 4 csv files with this format: PHOENIX_KM_INTERAZIONI_YYYYMMDD.csv PHOENIX_KM_TRIPLETTE_YYYYMMDD.csv NEWCAB_KM_INTERAZIONI_YYYYMMDD.csv NEWCAB_KM_INTERAZIONI_YYYY_MM_DD.csv YYYYMMDD: format CURRENT date I wont rename all files in... (4 Replies)
Discussion started by: manichino74
4 Replies

6. UNIX for Dummies Questions & Answers

Script to Rename Files

I wrote a simple script that converts my windows text files to unix, so that I can compare them to different unix files purposes of my project. win2unix file1.txt file1Win.txt win2unix file2.txt file2Win.txt etc Is there a way to simplify this to: <while .txt in... (5 Replies)
Discussion started by: idano530
5 Replies

7. Shell Programming and Scripting

Shell Script to rename files

Hi, i need a bit of help writting a tcsh script which renames all ascii text files in the current directory by adding a number to their names before the extension so for example, a directory containing the files Hello.txt Hello.t Hello should have the following changes, Hello.txt... (2 Replies)
Discussion started by: yakuzaa
2 Replies

8. Shell Programming and Scripting

Script to rename files

Let me preface this by stating I have absolutely no idea what I'm doing in this arena, but I'm in need of a little help here. I need to take filenames like this: amwed_0402c-slug~1-cp.jpg And reduce them to slug~1.jpg That is, I need to remove the first 12 and last 3 characters. The... (3 Replies)
Discussion started by: cpreovol
3 Replies

9. UNIX for Dummies Questions & Answers

Script to rename files

Have files of the sort 3p1522015.dgn and need to have them renamed to 152201.dgn. Essentially dropping the 1st 2 characters and the last. I'm relatively new to UNIX and uncertain of where to start. I hope this provides enough detail. Thanks (5 Replies)
Discussion started by: Dinkster
5 Replies

10. OS X (Apple)

Rename Files with a script ?

Hi All !!! Is there any solution to get rid of / " * in old files names WITH A SCRIPT (About 100 Gb of old files) I know it can be done i just dont know how ! Hope that some one can help Best R. Yovel (1 Reply)
Discussion started by: yoveln
1 Replies
Login or Register to Ask a Question