Rename file based on first 3 characters of data in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename file based on first 3 characters of data in file
# 1  
Old 10-01-2008
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 positions are "UNB" then rename the file FileA_UNB.

I believe I might be able to use the GREP command to identify the data but not sure how to rename the file being processed.

Appreciate any feedback.
# 2  
Old 10-01-2008
Assuming you want to rename all the files in the current directory:

Code:
#!/bin/sh

for file in *; do
  ext=`cut -c 1-3 ${file}`
  mv ${file} ${file}_${ext}
done

Regards
# 3  
Old 10-01-2008
Rather than using the cut utility, the shell builtin read could be used i.e.
Code:
for file in *; do
    read -r -n 3 ext < ${file}
    mv ${file} ${file}_${ext}
done

This User Gave Thanks to fpmurphy For This Post:
# 4  
Old 10-01-2008
Which shell's built-in is that ?

Not working in zsh though
 
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 File Name with Special Characters

I am trying to rename files with spaces and other characters and not able to be successful. FileNames: UPLOAD REFERENCE.xls UPLOAD MASS REFERENCE.XLS find /UPLOAD REFERENCE/ -depth -type f -name "* *" -exec rename " " "_" "{}" ";" The above one is successful to replace spaces... (1 Reply)
Discussion started by: eskay
1 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 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

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

5. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 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

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. firstfile.cks, anotherfile.cks, somefile.cks, etc.The filename and extension, however, are incorrect. The correct filename... (7 Replies)
Discussion started by: BRH
7 Replies

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

10. 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
Login or Register to Ask a Question