Rename files based on a list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename files based on a list
# 1  
Old 04-28-2015
Rename files based on a list

Hi,
I have a directory with a lot of files like this:
Code:
a.bam
b.bam
c.bam

I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file:
Code:
a    x
b    y
c    z

The output should be like this:
Code:
x.bam
y.bam
z.bam

Thanks in advance
# 2  
Old 04-28-2015
Try:
Code:
while IFS='	' read from to
do
  [ -f "$from.bam" ] && mv -- "$from.bam" "$to.bam"
done < list

where the TAB Character can entered as CTRL-V TAB in vi..

Or
Code:
tab=$(printf "\t")
while IFS=$tab read from to
do
  [ -f "$from.bam" ] && mv -- "$from.bam" "$to.bam"
done < list

In bash of ksh93 you can use $'\t'

Last edited by Scrutinizer; 04-28-2015 at 06:26 PM..
# 3  
Old 04-28-2015
Quote:
Originally Posted by Scrutinizer
Try:
Code:
while IFS='	' read from to
do
  [ -f "$from.bam" ] && mv -- "$from.bam" "$to.bam"
done < list

where the TAB Character can entered as CTRL-V TAB in vi..

Or
Code:
tab=$(printf "\t")
while IFS=$tab read from to
do
  [ -f "$from.bam" ] && mv -- "$from.bam" "$to.bam"
done < list

In bash of ksh you can use $'\t'
Shouldn't the line marked in red above be:
Code:
while IFS="$tab" read from to

?
# 4  
Old 04-28-2015
Hi Don, it is a variable assignment and $tab is therefore exempt from field splitting after expansion...
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-28-2015
Thanks, it worked
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

2. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

3. Shell Programming and Scripting

Is better way copy list of multiple files, rename and gzip

Is better way to write the script loop through one by one "Old_File_1: & New_File_1:" to copy 100 files to /staging/test folder then re-name & gzip all those files? I wrote this code below and don't like that much. Thanks I have a control_file under /tmp/test folder like below 100 files and... (10 Replies)
Discussion started by: dotran
10 Replies

4. Shell Programming and Scripting

How do I rename list of files with dateformat

Hello, I have a list of files like -rw-rw-r-- 1 rails rails 8463005 Jul 27 04:02 find_or.log.3.gz -rw-rw-r-- 1 rails rails 33786339 Jul 27 04:02 pro.log.10.gz -rw-rw-r-- 1 rails rails 44815467 Aug 3 04:02 pro.log.9.gz -rw-rw-r-- 1 rails rails 81562896 Aug 4 04:02 pro.log.8.gz... (7 Replies)
Discussion started by: ashokvpp
7 Replies

5. Shell Programming and Scripting

How to Rename List of files in a directory

How can i rename list of files in a directory? (4 Replies)
Discussion started by: knip
4 Replies

6. UNIX for Dummies Questions & Answers

rename files based on their respective directory name

I have a number of files in directories labeled like this: /Data/tr_gray/tr_DTI/dti_FA.nii.gz (the brackets here represent a range of number that the files are labeled with) I need to rename each dti_FA.nii.gz file according to the name of the folder it resides in. For example, the file ... (3 Replies)
Discussion started by: tk0034
3 Replies

7. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

8. Shell Programming and Scripting

Need to move and rename a list of files

Hi, I need to do something easy but I can't seem to figure out how to do this. Let's say I have 6 files in the directory below: /ebsbeta_f/flash/EBSUATQB/onlinelog o1_mf_6_55klt7nr_.log o1_mf_3_55klskj4_.log o1_mf_4_55klsrl1_.log o1_mf_5_55klt09p_.log o1_mf_2_55klv1ts_.log... (10 Replies)
Discussion started by: exm
10 Replies

9. UNIX for Dummies Questions & Answers

copy and rename list of files

Hi all, I am a newbie in writng unix..I am using ksh shell..Does anyone know how to copy a list o files from directory A to directory B with differnt names? i.e in Dir A, I have RPT101.555.TXT RPT102.666.TXT and I want to copy those files to dir B with new naming convention.. in Dir B,... (7 Replies)
Discussion started by: kinmak
7 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question