View previous topic :: View next topic |
Author |
Message |
pilgrim n00b
Joined: 17 Jun 2003 Posts: 57 Location: Canada
|
Posted: Sat Jul 05, 2003 12:46 am Post subject: Shell scripting |
|
|
Hi,
Can someone tell me if it's possible to tell the shell this:
rename foo.bar so that four character are deleted from the end of the file name
Thus the name becomes foo without the .bar extention.
Thanks in advance,
ZiM |
|
Back to top |
|
|
ctford0 l33t
Joined: 25 Oct 2002 Posts: 774 Location: Lexington, KY,USA
|
|
Back to top |
|
|
otulp n00b
Joined: 22 Apr 2002 Posts: 31 Location: Norway
|
Posted: Sat Jul 05, 2003 3:37 am Post subject: |
|
|
Perhaps not exactly what you wanted, but:
Code: |
for n in *; do mv $n `echo $n | cut -d'.' -f1`; done
|
Looks at all files in the current directory and cuts away all parts of the file name after the first dot. Messes up if a file name contains spaces or premature dots. All in all just an ugly quickie.
If you like, you can change '*' to '*.mpg' or some other matching criteria. |
|
Back to top |
|
|
Naan Yaar Bodhisattva
Joined: 27 Jun 2002 Posts: 1549
|
Posted: Sat Jul 05, 2003 3:48 am Post subject: |
|
|
Many ways to do this. One way:
Code: |
for x in *.???; do mv "$x" "${x%.???}"; done
|
The expression in the loop strips out the trailing 4 characters including the ".". |
|
Back to top |
|
|
mlang Tux's lil' helper
Joined: 24 Jan 2003 Posts: 82 Location: Near Pittsburgh, PA
|
Posted: Sat Jul 05, 2003 4:18 am Post subject: |
|
|
Felt like twiddling...
A script:
Code: |
#!/bin/bash
ls -A1 | grep '.\+\..*' | sort | while read OLDFILE
do
NEWFILE=`echo ${OLDFILE} | sed 's/\.[^\.]*$//g'`
mv -f "${OLDFILE}" "${NEWFILE}"
done |
The result:
Code: | mlang@bigbox test $ ls -a
. .. .test4.test .test5 test1.test test2.test.test test3.t test6.tst
mlang@bigbox test $ rm-ext
mlang@bigbox test $ ls -a
. .. .test4 .test5 test1 test2.test test3 test6 |
This is probably 100x more complicated than it needs to be...I'm a regexp n00b. |
|
Back to top |
|
|
|