Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename a file 1

Status
Not open for further replies.

7280

MIS
Apr 29, 2003
331
0
0
IT
Hi,
I'm trying to rename all the files with .zip extention that are in the current directory. I need to remove first 8 characters from the filename, so NEWFN=OLDFN minus first 8 characters.

I'm trying this:
ls *.zip | while read a b
do
OLDFN=$a
NEWFN=`awk '{ print ( substr ($a,8,35) ) }'`
echo "mv $OLDFN to $NEWFN"
done

what I see is only the new file name, so the awk output.
I know I'm missing something, but what?

Thanks in advance.
 
Hi

Sorry to say, but your code is pure guess. After correction and cleanup the closest form would be :
Code:
ls *.zip | \
while read a; do
  NEWFN=`awk -va="$a" 'BEGIN{print substr(a,8,35)}'`
  echo "mv $a to $NEWFN"
done
But I would prefer something simpler and faster :
Code:
ls *.zip | awk '{print "mv \""$1"\" \""substr($1,8,35)"\""}'
And if you like the output, then execute it as :
Code:
ls *.zip | awk '{print "mv \""$1"\" \""substr($1,8,35)"\""}' | sh

Feherke.
 
Was it so bad?

I was only missing the -va

Thanks however, the other command is simpler and suited perfectly.

Thanks again.
 
Why not just do:
Code:
rename 's/^........//g' *.zip
?
Works on my Linux-box, but maby not cross-platform?

:)
 
Hi

geirendre said:
Works on my Linux-box, but maby not cross-platform?
I saw [tt]rename[/tt] like that on a Debian. SuSE, Sorcerer and CygWin has slightly different one which does not use regular expression and can not be used in this case. :-( As far as I know Unix systems usually does have such tool.

Feherke.
 
Hi,
I tried on my linux (redhat). I have the command but it didn't work.

touch deleteme_file1.txt
touch deleteme_file2.txt
touch deleteme_file3.txt
touch deleteme_file4.txt

ls
deleteme_file1.txt deleteme_file2.txt deleteme_file3.txt deleteme_file4.txt deleteme_file5.txt

rename 's/^.........//g' *.txt

ls
deleteme_file1.txt deleteme_file2.txt deleteme_file3.txt deleteme_file4.txt deleteme_file5.txt

Thanks.
 
Try...
Code:
for i in *.txt; do mv $i $(expr $i : '.........\(.*\)'); done
 
Hi

Ygor said:
Code:
for i in *.txt; do mv $i $(expr $i : '.........\(.*\)'); done
                                      [red]12345678[b]9[/b][/red]
Beside that I try to use [tt]expr[/tt] as less as possible, because I find it very slow.
Code:
for i in *.txt; do echo mv $i ${i#????????}; done

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top