Alan Dipert

Tuesday September 2, 2008
Category  

Suppose you have a directory of files, and all of the files have a common prefix that is a certain number of characters long, and you want to take the prefix off. Your directory looks like this:

[alan@shitfit testdir]$ ls
000-lol.zip  001-rofl.txt  002-roflmfao.jpg

So to get rid of the numbers and dashes in all of the files, you can do something like this:

for F in *; do mv $F ${F:4}; done

The ${F:4} bit tells Bash to print the string $F starting at the 4th character. It turns out Bash does all kinds of things with strings, as you can see here.

So, your result will look like this:

[alan@shitfit testdir]$ ls
lol.txt  rofl.txt  roflmfao.jpg

To tune your renaming to a certain file extension, you might use:

for F in *.txt; do mv $F ${F:4}; done

You have a number of options available, and all of them involve a loop, some kind of formatting approach, and the mv command. Micha suggested using find and piping the results to while, read, sed, and mv. This guy does something like that to match the beginnings of filenames to a pattern.

Presto.

Comment

  1. Thanks for the tip, made my day!

    Florian Brucker · Jan 7, 03:28 PM · #

  2. order propecia [url=“http://www.bmbvideo.com/propecia.html”]order propecia[/url] http://www.bmbvideo.com/propecia.html order propecia buy ambien [url=“http://www.bmbvideo.com/ambien.html”]buy ambien[/url] http://www.bmbvideo.com/ambien.html buy ambien tramadol [url=“http://www.bowlinginlubbock.com/Tramadol.htm”]tramadol[/url] http://www.bowlinginlubbock.com/Tramadol.htm tramadol

    ahtis_ausstellung · Dec 9, 06:16 AM · #

  3. Hi! kWkRbJUg http://isamyy.com/

    viagra · Jan 21, 05:08 PM · #

  4. YGpZxEL

    OFysxFXH · Feb 2, 06:30 PM · #

  5. U57TDQ sefedpidgfpw, [url=http://jdfmofsvwsvc.com/]jdfmofsvwsvc[/url], [link=http://ueoikjiebzwx.com/]ueoikjiebzwx[/link], http://keoyimmazamw.com/

    folejo · Feb 6, 05:34 AM · #

  6. 2VKLFk wuwqruxnvatw, [url=http://bzjvavqnnjey.com/]bzjvavqnnjey[/url], [link=http://fuhqmnurbwmj.com/]fuhqmnurbwmj[/link], http://kdfcbilumngi.com/

    lrhbkeecjyh · Feb 12, 05:39 PM · #

  7. TtVEjS

    lMGjBbKy · Mar 9, 12:10 PM · #