Rename a file if I don't know its exact original name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename a file if I don't know its exact original name?
# 1  
Old 03-03-2006
Rename a file if I don't know its exact original name?

Hi everyone,

I will be downloading a file every day with the name in this format

SCA20060303_17514_IN.TXT

And I need to rename it to

SCA20060303_IN.TXT

Where "20060303" is the current date, and the "_17514" part will always be a 5-digit number that I will NOT know beforehand. I just need to take it out.

I've always used the mv filename1 filename2 command to rename files but how can I do this if I don't know the exact original name of the file?

Thanks,

Ryan
# 2  
Old 03-03-2006
Code:
echo 'SCA20060303_17514_IN.TXT' | sed 's#^\([^_][^_]*\)_[0-9][0-9]*\(.*\)#\1\2#'

# 3  
Old 03-03-2006
Thank you, but I won't know the file name beforehand. To get the file, I do

mget SCA*.TXT

How can I do this without knowing the original name?
# 4  
Old 03-03-2006
Quote:
Originally Posted by propel
Thank you, but I won't know the file name beforehand. To get the file, I do

mget SCA*.TXT

How can I do this without knowing the original name?
well.. 'mget' the files AND rename them all afterwards.

OR

look into ftp's [if that's the client you're using] "nmap" command if one exists for your ftp client:
Code:
ftp host
    nmap $1_$2_$3 $1_$3
    mget SCA*.TXT


Last edited by vgersh99; 03-03-2006 at 03:56 PM..
# 5  
Old 03-03-2006
Code:
for f in SCA????????_?????_IN.TXT;do
      echo mv $f ${f%%_*}_IN.TXT
done

ksh
obviously remove the echo
# 6  
Old 03-03-2006
Thank you all for your help. vgersh99, that nmap command is crazy!
# 7  
Old 03-03-2006
Quote:
Originally Posted by propel
Thank you all for your help. vgersh99, that nmap command is crazy!
everything is relative in this world!
do you understand what 'nmap' does? Smilie
did you consider the pros and cons of either of the methods?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recover the original file once removed

Hi All, Is there is any machanisim, once delete the file can we restore it. Thanks (8 Replies)
Discussion started by: bmk123
8 Replies

2. Shell Programming and Scripting

Find the original file size of encrypted file

Hi, I am trying to find out the original file size of an encrypted file in SunOS. The file was decrypted with gpg command. I want to know the size of the orginal file without decrypting it. I am using the below command, but it is not working for big files(more than 1 GB). gpg --passphrase... (4 Replies)
Discussion started by: vsachan
4 Replies

3. Shell Programming and Scripting

Merge different files into the original file

Hello Below is my requirement I have 3 files A1.txt , A2.txt and A3.txt . A2 is dynamically generating file I want the merge of A1,A2 and A3 in A2.txt Could you please help? (3 Replies)
Discussion started by: Pratik4891
3 Replies

4. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

5. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

6. UNIX for Dummies Questions & Answers

command to change original file

I have a script where I have the command sed "s/$search_string/$replace_string/g" $backup However I want a command to correct the original file $backup as well as creating another file with the changes which I have in my script already. In other words, I want to touch the orginal file also... (1 Reply)
Discussion started by: alis
1 Replies

7. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

8. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies
Login or Register to Ask a Question