Visit The New, Modern Unix Linux Community


How to rename a file with a name from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to rename a file with a name from another file
# 1  
How to rename a file with a name from another file

Hello Experts...

I have a requirement to rename a file using the file name stored in another file. I browsed thru forums and found this for renaming multiple files.

Code:
 
awk '{f=$1;sub("-.*\\.","_"$2".",$1);system("mv "f" "$1)}' file-with-filenames

But, My requirement is to rename a single file and I dont know how to edit the available command without messing up Smilie

Case scenario
1)Filename.txt
File1_renamed

2)I need to read the file Filename.txt and rename the File1.txt to File1_renamed.txt

Can someone throw some light on how to do this? Your help will be much appreciated!! Thanks
# 2  
Code:
mv file1.txt $(xargs <Filename.txt)

Code:
mv file1.txt $(head -1 Filename.txt)

# 3  
one way:

Code:
#  mv File1.txt $(sed q Filename.txt)

# 4  
Thank you....

Thanks for the commands...

I got one more similar requirement to rename a file by appending a value from other file. I know it would be a simple modification to these commands but my unix knowledge did not help me....Can you please show me a way to do this....

Say

File_Date.txt contain a value 20110710

Now, I want to rename a file File1.txt to File1_20110710.txt

Your help is much appreciated!!!

---------- Post updated 06-07-11 at 12:19 AM ---------- Previous update was 05-07-11 at 07:32 AM ----------

Hi.. Many thanks for the commands...

I got slight change in the requirement to rename a file by appending a value from other file. I know it would be a simple modification to these commands but my unix knowledge did not help me....Can you please guide me to do this....

Scenario....

File_Date.txt contain a value 20110710

Now, I want to rename a file File1.txt to File1_20110710.txt

Your help is much appreciated!!!
# 5  
Does File_Date.txt contain the date value in its first line ?
If not, please give us an example of File_Date.txt content.
# 6  
File_Date.txt will have only one date value in the format YYYYMMDD

ex.,

20110706

I need to append this date to the name of other file already exists in the directory.

ex.,
File_Date.txt
File1.txt

I need to read the content(date value in yyyymmdd) from File_Date.txt and rename File1.txt by appending the date value, which should look like File1_20110706.txt

I managed to get it work with
Code:
mv File.txt $echo File1_$(head -1 File_Date.txt).txt

But, I would appreciate if you could suggest any better approach
# 7  
give a try with double quote
Code:
mv File.txt "File1_$(head -1 File_Date.txt).txt"

A better approach would have been to have stored the date into a variable instead of into a file

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #79
Difficulty: Easy
GNU/Linux is directly descended from BSD.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename file in directory using contents within each file

In the below there are two generic .vcf files (genome.S1.vcf and genome.S2.vcf) in a directory. There wont always be two genaric files but I am trying to use bash to rename each of these generic files with specfic text (unique identifier) within in each .vcf. The text will always be different, but... (11 Replies)
Discussion started by: cmccabe
11 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. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

5. UNIX for Dummies Questions & Answers

look for specific values in a file and rename file with value found

Hi, i have a file with some data ..look for some specific value in the file and if found that value rename the file with the value found in the file.. ex.. File.txt 1236 43715825601ANDERSSON, 1236 437158256031963040120060901200609010000000 1236 43715825604123 MCCL AVE UPPER 1236 ... (11 Replies)
Discussion started by: dssyadav
11 Replies

6. Shell Programming and Scripting

.sh file To rename existing file and copy new file

Hi All, I am very new to shell scripting . In my current task i want to create .sh file that will rename the existing file with appending _bu in it. And then copy new file . e.g if i have file linuxFirst.java then i want to rename it to linuxFirst_bu.java ..Then want replace with latest... (1 Reply)
Discussion started by: maheshkaranjkar
1 Replies

7. Shell Programming and Scripting

rename file by removing some part of the file name

I am special requirements to rename file. I have files with names like below: 1_firstname1_lastname1.html 2_firstname2_lastname2.html 3_fistname3_lastname2.html I would like these file to be renamed as below firstname1_lastname1.html firstname2_lastname2.html... (5 Replies)
Discussion started by: McLan
5 Replies

8. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

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

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies

Featured Tech Videos