Sponsored Content
Top Forums Shell Programming and Scripting Need help for a Shell script to rename multiple files Post 302400633 by drewk on Wednesday 3rd of March 2010 06:35:30 PM
Old 03-03-2010
Quote:
Originally Posted by alister
Hello, all:

I see several solutions using the following snippet...
Code:
for fn in `ls file*`

... and just wanted to suggest not to do that. After the command substitution, filenames will undergo word splitting. If any of them include whitespace (assuming a default IFS value), the value of fn will not be set correctly for each filename (such filenames will be assigned to fn piecemeal over multiple loop iterations).

Code:
for fn in file*

... will accomplish the task without word splitting issues.

Illustrative example:
Code:
$ touch without_space with\ space

#Incorrect
$ for i in `ls w*`; do echo "$i"; done
with
space
without_space

#Correct
$ for i in w*; do echo "$i"; done
with space
without_space

Regards,
Alister
You are only partially correct. It is deceptive, but your second form is also incorrect in this case.

Try this:

Code:
for i in `ls`; do ls -l "$i"; done

that fixes the space problem, but breaks with file names with other characters, such as common ones of single or double quotes or mean ones like tab or CR.

The other problem is if you use more complex command substitution, such as this:

Code:
for i in "`ls | grep '^D'`"; do  echo "$i"; done

the return is:
Code:
Desktop
Documents
Downloads
Drive Util

which seems correct. However, now run:

Code:
for i in "`ls | grep '^D'`"; do wc -l "$i"; done

The return now is:

Code:
wc: Desktop\nDocuments\nDownloads\nDrive Util: open: No such file or directory

This is because BASH has passed all the file names with carriage returns to wc and wc thinks it is a single file name. Bad result especially with mv etc...


The only robust way I have found is with a ASCIZ termination and only while loops in bash support that...

---------- Post updated at 03:35 PM ---------- Previous update was at 03:30 PM ----------

Quote:
Originally Posted by alister
Try "Illustrator*" instead. UNIX is case sensitive. "illustrator*" is not the same as "Illustrator". If that was an issue, the following should print out a paged list of the filenames:

Code:
for f in Illustrator*; do echo "$f"; done | more

Alister
He is on a Mac, and the default is NOT case sensitive. Yours is better form however. On Mac BASH you can have case sensitive string comparisons tell you one thing and the file system do something disastrously different...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

rename multiple files

Hi, can anyone have a ksh script to rename multiple files (ie to remove .Z extension of the files) can someone correct this? for i in *.Z do var1 = substr($i, 1,at(".Z",$i)-1) mv $i $var1 done Thanks.. Antony (13 Replies)
Discussion started by: antointoronto
13 Replies

2. Shell Programming and Scripting

Shell script to rename files with .1,.2,.3 ....ext respectively

Hey Guys.... Just need some help as I am not proficient in Unix shell script... Doubt: --------------- Suppose there will be some of the following files inside a directory called OUT ... Path: - /appdb1/product/batch/rms/OUT files inside OUT directory:- POSU_75002_20090127_20090129035442... (4 Replies)
Discussion started by: satyajit007
4 Replies

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

4. Shell Programming and Scripting

Shell script to rename a group of files

Hello, I am having 1800 files in a directory with a specified format, like amms_850o_prod.000003uNy amms_850o_prod.000003u8x amms_850o_prod.000003taP amms_850o_prod.000003tKy amms_850o_prod.000003si4 amms_850o_prod.000003sTP amms_850o_prod.000003sBg amms_850o_prod.000003rvx... (12 Replies)
Discussion started by: atlantis
12 Replies

5. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

6. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

7. Shell Programming and Scripting

Rename multiple files

Hi, In my directory I have many files, for e.g. file_123 file_124 file_125 file_126 file_127 Instead of renaming these files one by one, I would like to rename them at a same time using same command... they should appear like 123 124 125 126 127 What command(awk or ls or... (3 Replies)
Discussion started by: juzz4fun
3 Replies

8. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

9. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

10. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies
All times are GMT -4. The time now is 07:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy