Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Shell scripting
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
pilgrim
n00b
n00b


Joined: 17 Jun 2003
Posts: 57
Location: Canada

PostPosted: Sat Jul 05, 2003 12:46 am    Post subject: Shell scripting Reply with quote

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
View user's profile Send private message
ctford0
l33t
l33t


Joined: 25 Oct 2002
Posts: 774
Location: Lexington, KY,USA

PostPosted: Sat Jul 05, 2003 2:59 am    Post subject: Reply with quote

Yes it can be done, but i'm not that familiar with bash programming. I did a quick google search so check this out....

http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

Chris
Back to top
View user's profile Send private message
otulp
n00b
n00b


Joined: 22 Apr 2002
Posts: 31
Location: Norway

PostPosted: Sat Jul 05, 2003 3:37 am    Post subject: Reply with quote

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
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Sat Jul 05, 2003 3:48 am    Post subject: Reply with quote

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
View user's profile Send private message
mlang
Tux's lil' helper
Tux's lil' helper


Joined: 24 Jan 2003
Posts: 82
Location: Near Pittsburgh, PA

PostPosted: Sat Jul 05, 2003 4:18 am    Post subject: Reply with quote

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. :D
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum