File renaming from list of names contained in another file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File renaming from list of names contained in another file
# 1  
Old 06-27-2010
File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile).
The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be assigned to the file.

file-abcd.pdf #to be renamed [tab] 123456789 #the new filename
file-abce.pdf #to be renamed [tab] 809809231 #the new filename

Desired outcome:
file-abcd.pdf --> file.123456789.pdf
file-abce.pdf --> file.809809231.pdf

It's to say "for F in each actual file in files/ named like this on the left, re-name it to the name to the right and keep the old extension... But bash did not understand this phrase Smilie)
The names to be given are stored in a file but the files to name are actual files, so just no idea how to tackle this and where to even start.

So far all the scripting I could do and understand in bash was loops to rename just an extension to a file, or to add an extra suffix. Beginner... sigh... But there has to be a way to do it in Linux, there usually is.
Thanks in advance for your help!
# 2  
Old 06-27-2010
Is it literal contents of the file containing filenames?
file-abcd.pdf #to be renamed [tab] 123456789 #the new filename
file-abce.pdf #to be renamed [tab] 809809231 #the new filename

Or is it just:
file-abcd.pdf      123456789
file-abce.pdf      809809231

Anyway if your file looks like that second example, then this should work:
awk '{f=$1;sub("-.*\\.","."$2".",$1);system("mv "f" "$1)}' file-with-filenames

Last edited by bartus11; 06-27-2010 at 08:05 PM..
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-27-2010
Assuming the listfile and the pdf files are in the same directory and you being there:
$ awk '{print $1" "$5}' listfile | while read fn id; do
mv -v "$fn" "$(echo $fn|sed 's/pdf//')$id.pdf"
file-abcd.pdf -> file-abcd.123456789.pdf
file-abce.pdf -> file-abce.809809231.pdf

# 4  
Old 06-27-2010
for i in `cat listfile`
orgfilename=`echo $i | awk '{print $1}'`
newfilename=`echo $i | awk 'print $5}'`
mv $orgfilename $(echo $orgfilename | sed s/pdf/$newfilename\.pdf/g)

Last edited by 3junior; 06-28-2010 at 01:03 AM..
# 5  
Old 06-29-2010
Anyway if your file looks like that second example, then this should work:
awk '{f=$1;sub("-.*\\.","."$2".",$1);system("mv "f" "$1)}' file-with-filenames


1/ thanks!

2/ yes, the file looks like in the second example, it is a simple copy-paste of two columns from a spreadsheet to a text file.

3/ however, there is a small issue - caused by my attempt to keep the example simple; in fact the files have longer names with lots of dahses in them "-" like so:
acme-doc-may-00101.pdf 2336909
acme-doc-may-00102.pdf 2336910
acme-doc-may-00103.pdf 2336911

some are even longer
acme-doc-may-bclot-00104.pdf 2336912

Apologies, I wanted to keep it all lean in the example.

this causes a little problem, the renamed files have an extra dot in the filename after running your command:

In other operating systems it would be a problem to have two dots, so best to have an underscore instead.
I tried to edit your command but messed up results, of course Smilie

many thanks indeed!!!
# 6  
Old 06-29-2010
This should work:
awk '{f=$1;sub("-.*\\.","_"$2".",$1);system("mv "f" "$1)}' file-with-filenames

This User Gave Thanks to bartus11 For This Post:
# 7  
Old 06-29-2010
Originally Posted by bartus11
This should work:
awk '{f=$1;sub("-.*\\.","_"$2".",$1);system("mv "f" "$1)}' file-with-filenames

works like a charm!

very subtle! it replaces the dashes with underscore and renames the files with the number supplied in the right column.
many, many thanks!!!

may I ask you what the "system" command or call does? is it like $(command)?
anyway, thank you, very grateful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Renaming the file names in a directory

Hi, I have about 60 files in a directory and need to rename those files. For example the file names are i_can_phone_yymmdd.txt (where yymmdd is the date. i.e 170420 etc) i_usa_phone_1_yymmdd.txt i_eng_phone_4_yymmdd.txt The new file names should be phone.txt phone_1.txt phone_4.txt I am... (4 Replies)
Discussion started by: naveed
4 Replies

2. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

4. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

5. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

6. UNIX for Dummies Questions & Answers

Renaming files in one file from names in other

Hi Guys, I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like K2_34625-34675 K7_988963-988983 K12_773882-7734102 and the other set corresponding to the same is U_P_321_9_3_11.ab1 U_P_322_9_3_11.ab1 U_P_323_9_3_11.ab1 Now... (23 Replies)
Discussion started by: pawannoel
23 Replies

7. Shell Programming and Scripting

List of file names

I have the following list of file names stored in $fnames, so that if I do foreach f ($fnames) echo "$f" end I will get n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run1 n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run2 n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run3... (3 Replies)
Discussion started by: kristinu
3 Replies

8. Shell Programming and Scripting

Renaming file names

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (2 Replies)
Discussion started by: vpv0002
2 Replies

9. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies

10. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies
Login or Register to Ask a Question

Featured Tech Videos