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

echo 'SCA20060303_17514_IN.TXT' | sed 's#^\([^_][^_]*\)_[0-9][0-9]*\(.*\)#\1\2#'

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:

ftp host
    nmap $1_$2_$3 $1_$3
    mget SCA*.TXT
for f in SCA????????_?????_IN.TXT;do
      echo mv $f ${f%%_*}_IN.TXT
done

ksh
obviously remove the echo

Thank you all for your help. vgersh99, that nmap command is crazy!

everything is relative in this world!
do you understand what 'nmap' does? :confused:
did you consider the pros and cons of either of the methods?

vgersh99, it seems to me that with nmap you are assigning values to variables ($1 and $3 in your example) and using those variables to re-format the string.

However, I did just realize that I also have to rename the _IN.TXT to lowercase. Does this mean the 2nd option is better?

Thanks again

you can try:

ftp host
    nmap $1_$2_$3 $1_$3
    case
    mget SCA*.TXT

to map all CAPs to low case....
You can play with either 'nmap' and/or 'macdef' to get the desired result OR go the 'post processing' route, but it might interesting to figure out how to do it natively within ftp.

Althought this is more complicated ,u can try:

for EXT_FILE in $(ftp -n ${HOST} < files.ftp | grep -vE "^Passive|^total" |grep -v active|awk -F\SCA '{print $2}')
do
echo ${EXT_FILE} >>files_ext-list.tmp
done

cat files_ext-list.tmp|while read EXT_FILE
do
EXT_FILE2="$(echo ${EXT_FILE}|awk -F\_ '{print $1}')_in.txt"
echo "define(EXT_FILE,${EXT_FILE}) define(EXT_FILE2,${EXT_FILE2})" > out.macros
cat out.macros download_files.ftp | m4 -B64556 > temp.ftp
ftp -n ${HOST} < temp.ftp
done

Where:

cat files.ftp

user username password
passive
dir
bye

cat download_files.ftp

user username password
passive
get "SCA""EXT_FILE" "SCA""EXT_FILE2"
bye

Cheers