Renaming duplicate files in a loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Renaming duplicate files in a loop
# 1  
Old 01-17-2010
Renaming duplicate files in a loop

Hello,

I have a bunch of files whose names start with 'xx'
The first line of each file looks something like:

a|...|...|...|... , ...


In order to rename all of these files to whatever's between the 4th | and the comma (in the first line of that particular file) , I have been using:
Code:
for file in xx*; do mv "$file" `head -1 "$file" | cut -d '|' -f 5 | cut -d ',' -f 1`.txt; done

But my major problem is that several files have the same string between the 4th | and the comma. When this 'duplication' happens, I want to be able to rename the file to the same thing, but with something appended to the end, like a number or something.

I want to have the same number of files in the end as the number of files I started with, but right now I'm losing many many files since unix is writing overtop of files when the desired name has already been used.



Thank you in advance

Last edited by Yogesh Sawant; 01-19-2010 at 05:17 AM.. Reason: code tags, please
# 2  
Old 01-17-2010
Code:
ls xx* | awk -F"[|,]" ' { getline var < $0; file=$0; $0=var; print "mv " file " " $5arr[$5]++; close(file) } ' | ksh

# 3  
Old 01-20-2010
Thank you Anbu23,

Though your method worked perfectly for a simple test case that I made, it encountered a serious problem for the set of files I am working with.

For one of the files, the string destined to become the filename of that file was "too long" for some reason... and so the entire procedure stopped, and only 84 out of my 1883 files actually got renamed.

This was very strange though because when I was using my original code:

Code:
for file in xx*; do mv "$file" `head -1 "$file" | cut -d '|' -f 5 | cut -d ',' -f 1`.txt; done

none of the file names were "too long" ... the only problem was that some files were being overwritten due to duplicate naming.

So does anyone know how I could modify my code to avoid the duplicate naming ??

Or a better way of implementing Anbu23's code so as to not have any restriction on the size of a filename ??

I thought that there wasn't any restriction to the size a filename can be in unix.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

2. Shell Programming and Scripting

Renaming CERTAIN files

Hello I have a folder with around a 100 files. Some of these files have an extension of .DAT.csv and the others are simply .DAT I need to rename all those files ending in .DAT.csv to .DAT. I tried out the following script: cd /data/AED/DEV/IN/MENSUEL/B3C/FEERIE/BASCULE/TEST cnt=1 for fname... (3 Replies)
Discussion started by: S. BASU
3 Replies

3. Shell Programming and Scripting

Loop renaming files w/ a count problem

:wall: Hello there, basically in my program where im stuck at is when it comes to rename the files in a loop. - the program counts the number of files w a given name (works!) - and then if the number of files is greater or equal to the MAX_VERSIONS (numbers of files allowed w the... (1 Reply)
Discussion started by: thurft
1 Replies

4. UNIX for Dummies Questions & Answers

Renaming all files

Hi, Very silly question. I need to rename all my files located in one particular folder. The names are Results1.dis, Results2.dis, Results3.dis, etc. I need to change the names to Results001.dis, Results002.dis, Results003 so on and so forth. Now, Files with double digits such as Results17.dis... (3 Replies)
Discussion started by: Xterra
3 Replies

5. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

6. Shell Programming and Scripting

Renaming of files with different extensions on the same path to .found with the help of loop

hi , I have certain files at the same path with differeent extensions like .dat , .txt etc ...........i need to rename them with extension .found at the same path with the help of loop.... also the files names will be like this ; abc_2010_c1.dat abc_2010_c2.dat xyz_2010_c1.txt (2 Replies)
Discussion started by: amitpta
2 Replies

7. UNIX for Dummies Questions & Answers

Some questions - renaming duplicate names

I have a file that looks like this 2 4 10 500 tim9 5 8 14 700 tim9 3 5 15 432 john1 1 4 12 999 ellen2 So basically what i want to do is fine duplicate names on column 5 and rename it with an extention (i.e. tim9_1 and tim9_2). so the output file will look like this 2 4 10 500 tim9_1... (1 Reply)
Discussion started by: kylle345
1 Replies

8. Shell Programming and Scripting

Renaming Files

Hi Alll, I have a script that we use on the servers to change the name of files that have spaces in the name: #!/bin/tcsh set n = 0 foreach f ( * ) echo $f | grep " " if ( $? == 0 ) then mv "$f" `echo $f | sed -e "s/ /_/g"` @ n += 1 endif end echo $n changed I need to write a... (2 Replies)
Discussion started by: abch624
2 Replies

9. UNIX for Dummies Questions & Answers

Renaming files

Hello! I am not familiar with UNIX and I have this problem: I need to move files from a UNIX machine to a PC. UNIX file names contain ":" as special character which is not recognized in a PC. How can I change ":" for "_" in the name of a bunch of files in UNIX? Thanks for your help. (7 Replies)
Discussion started by: Tygoon
7 Replies
Login or Register to Ask a Question