For Loop To Rename Multiple Files Finds One Non-existant File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers For Loop To Rename Multiple Files Finds One Non-existant File
# 1  
Old 11-16-2010
For Loop To Rename Multiple Files Finds One Non-existant File

Okay so here's something that's confusing me: I have a script that's designed to remove the words "new_" from the front of any file except two exceptions and it looks something like this...
Code:
for i in new_*
do
   if [[ `echo $i` != new_dat.bak ]] && [[ `echo $i | cut -c1-7` != new_ig_ ]]; then
          j=`echo "$i"|cut -c5-`
          mv $i $j
    fi
done

But what's happening is that it works perfectly on any directory except one that has no files starting with "new_". In that case it will still get in the if statement and report a single occurrence. It will report that it found a file called "new_*"

This I do not understand. Can someone please explain why it is seeing a non-existent file that just happens to match the description given in the for loop? Thanks.

Last edited by Scott; 11-16-2010 at 07:25 PM.. Reason: Code tags, please...
# 2  
Old 11-16-2010
When there are new_* files in a directory, * wildcard is being expanded and so file names are being passed in for loop as value of i.
When there is no file of type new_*, * wildcard is not expanded and so new_* is passed as it is in the for loop.
All is that, this for loop will execute atlest once, either for actual files ( if present) or for new_*
# 3  
Old 11-16-2010
Quote:
Originally Posted by anurag.singh
When there are new_* files in a directory, * wildcard is being expanded and so file names are being passed in for loop as value of i.
When there is no file of type new_*, * wildcard is not expanded and so new_* is passed as it is in the for loop.
All is that, this for loop will execute atlest once, either for actual files ( if present) or for new_*
Ah. Thanks for the clarification. I conceived a work around was to add a if test -f argument inside the loop to make sure it is a real file before trying to move it but I'd prefer to avoid the situation entirely. Is that possible?

---------- Post updated at 06:36 PM ---------- Previous update was at 06:34 PM ----------

Thanks for cleaning up my post Scott. Sorry it was a little sloppy. Smilie
# 4  
Old 11-16-2010
Code:
$ ls
new_a		new_b		new_c		new_dat.bak	new_ig_1	new_ig_2

Code:
ls new_* | while read FILE; do
  [[ "$FILE" = new_dat.bak ]] || [[ "$FILE" = new_ig* ]] && continue
  mv "$FILE" "${FILE#new_*}"
done

Code:
$ ls
a		b		c		new_dat.bak	new_ig_1	new_ig_2


Last edited by Scott; 11-16-2010 at 09:08 PM.. Reason: Quoting around "$FILE"
This User Gave Thanks to Scott For This Post:
# 5  
Old 11-16-2010
Code:
#!/bin/ksh
exec 2>/dev/null
ls new_* | while read i
do
    if [[ `echo $i` != new_dat.bak ]] && [[ `echo $i | cut -c1-7` != new_ig_ ]]; then
          j=`echo "$i"|cut -c5-`
          mv $i $j
    fi
done

This User Gave Thanks to anurag.singh For This Post:
# 6  
Old 11-16-2010
Excellent alternatives. Thank you both Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to rename files in bash loop

I am trying to use sed to rename all .txt files in /home/cmccabe/test. However, I am getting an error that I seems to be putting the files in a new directory s, instead of in the original. Thank you :). bash # rename classified cd /home/cmccabe/test pattern2_old="_classify"... (2 Replies)
Discussion started by: cmccabe
2 Replies

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

3. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

4. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

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

6. Shell Programming and Scripting

how to list on existant files

Hi I am using "ls" command to list files present on the system. "ls" lists all the files including those which are not present. Is there a way to filter the output so only the files which are actually present are shown. For exmple, the following command lists all the files: $ ls... (2 Replies)
Discussion started by: aoussenko
2 Replies

7. Linux

rename files using loop with different name

Hi, i need to write a shell script where i have to loop through all the file in a directory and rename them based on below condition. file1.dat file2.dat file3.dat the above files has to be moved to another directory like below file1_201001.dat file2_201002.dat file3_201003.dat... (3 Replies)
Discussion started by: feroz
3 Replies

8. UNIX for Dummies Questions & Answers

Rename files with sed in an until loop (double post)

I want to change the name of some of my files (mypics-0001, mypics-0002, mypics-0003.....mypics-0240) and I want to double check to see if this code is right: x=0 until do sed 's/mypics\-*/bday/g' done Would this change all of my file names to "bday0001....bday0240"? Please let me... (0 Replies)
Discussion started by: jvpike
0 Replies

9. UNIX Desktop Questions & Answers

Rename files without using for loop

Hi, Is it possible to rename files at a time in a directory without using the for loop. (ex: intial filename- abc.txt to abc.tmp or abc.txt.tmp) I need to rename the files in a remote directory to which I'm connecting thru tectia sftp. The commands 'mv', 'for', 'echo' do not work. I have... (3 Replies)
Discussion started by: Qwerty123
3 Replies

10. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies
Login or Register to Ask a Question