Need to rename file based on name in same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to rename file based on name in same file
# 1  
Old 01-13-2011
Need to rename file based on name in same file

Good day.

I need a one-liner (if possible) otherwise a longer script to do the following:

I have a list of files in a directory with the same filename extension, ie.

Code:
firstfile.cks, anotherfile.cks, somefile.cks, etc.

The filename and extension, however, are incorrect. The correct filename is contained on the only line inside each file:

Code:
# cat firstfile.cks
747339843       15691264        PS0001.pkg

# cat anotherfile.cks
2451811218     23460352       PS0002.pkg

# cat somefile.cks
172067168       15546368        PS0003.pkg

So, what I need is some script to search for all files *.cks, select the correct filename and adding a new filename extension ".cksum"; ie:

Code:
'firstfile.cks' becomes 'PS0001.pkg.cksum'.
'anotherfile.cks' becomes 'PS0002.pkg.cksum' and
'somefile.cks' becomes 'PS0003.pkg.cksum'

Thanks in advance,
Bjoern
# 2  
Old 01-13-2011
Try it first without the coloured part to make sure the commands are correct:
Code:
awk '{print "mv " FILENAME " " $NF ".cksum"}' *.cks | sh

# 3  
Old 01-13-2011
Code:
 find . -name "*.cks" -print | while read fname; do mv $fname "`tail -1 $fname | awk '{ print $3 }'`".cksum; done

# 4  
Old 01-13-2011
Code:
for i in *.cks
do
  read x x toname < "$i"
  mv "$i" "$toname.cksum"
done

Code:
grep . *.cks | 
while IFS="$IFS:" read from x x toname
do
  mv "$from" "$toname.cksum"
done


Last edited by Scrutinizer; 01-13-2011 at 04:39 PM..
# 5  
Old 01-13-2011
Quote:
Originally Posted by citaylor
Code:
 find . -name "*.cks" -print | while read fname; do mv $fname "`tail -1 $fname | awk '{ print $3 }'`".cksum; done

Thanks citaylor.

Your solution worked on some files and not others. I ran the commands in debug mode and substituted 'mv' for 'echo' to see what was happening.:
Code:
+ read fname
++ tail -1 ./14__002c.cks
++ nawk '{ print $3 }'
+ echo ./14__002c.cks 14.01.00.11_PS0013.pkg.cksum
./14__002c.cks 14.01.00.11_PS0013.pkg.cksum

This one worked fine, but the next one did something unexpected:
Code:
+ read fname
++ tail -1 ./fa__002e.cks
++ nawk '{ print $3 }'
+ echo ./fa__002e.cks $'fa_netra-10s_3.18.pkg.gz\r.cksum'
.cksum002e.cks fa_netra-10s_3.18.pkg.gz

How on earth did the '\r' get in there?

Regards,
Bjoern
# 6  
Old 01-13-2011
The files have been transferred from a Windows/DOS system and have carriage returns on the end of each line ?
# 7  
Old 01-14-2011
Quote:
Originally Posted by citaylor
The files have been transferred from a Windows/DOS system and have carriage returns on the end of each line ?
Bingo! Thanks a lot citaylor. I thought that all the ^M had already been removed.

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is it possible to rename fasta headers based on its position specified in another file?

I have 5 sequences in a fasta file namely gene1.fasta as follows, gene1.fasta >1256 ATGTAGC >GEP TAGAG >GTY578 ATGCATA >67_iga ATGCTGA >90_ld ATGCTG I need to rename the gene1.fasta file based on the sequence position specified in list.txt as follows, list.txt position1=org5... (5 Replies)
Discussion started by: dineshkumarsrk
5 Replies

2. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

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

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

5. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. Shell Programming and Scripting

Rename a file based on a specific separator

Hello, I am new to shell I have a folder which contains a list of files, all the files contain the separator : I need to replace this character for all the filenames (by batch) ex: hello:world should become hello-world please help Thanks (3 Replies)
Discussion started by: sikilaklak
3 Replies

7. Shell Programming and Scripting

Rename portion of file based on another file

Hello, I've been searching and reading, but I can't figure out how to solve this problem with my newbie skills. In my directory, I have a list of files (see dirlist.txt attachment) that I need to merge and rename. I have part of the code of the code figured out (see below). However, I... (3 Replies)
Discussion started by: anjulka
3 Replies

8. Shell Programming and Scripting

Rename folder based on containing XML file

Hi everyone. I'm in need of a solution where i need to rename a folder to a name that's inside an XML file in that folder. OS is Ubuntu 9.10 with Gnome. I've tried using grep, sed and xpath, but can't seem to find a solution. This is the simplified folder structure: FOLDER-NAME -... (4 Replies)
Discussion started by: CoolCow
4 Replies

9. Shell Programming and Scripting

Rename File Based on Created Date

I am trying to rename files based on the created/born date of the file. I Have a total of 4000 files that i am trying to do this with and would like it to be log_yyyymmddhh.gz right now the files are maillog.???.gz. Can anyone point me in the right direction of how to get this done via scipt? ... (4 Replies)
Discussion started by: Paulb
4 Replies

10. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies
Login or Register to Ask a Question