[Leaplist] howto change the name of every file in a directory

Bryan J. Smith thebs413 at yahoo.com
Thu Oct 9 10:39:00 EDT 2008


From: Gray Frost <grayf327 at gmail.com>
> Richard,
> I like this method. Short and simple.

That's the power of bash.  If you can write it in a
for loop, a piped xarg list from find or something else,
etc..., it's a heck of a lot quicker than tracking down
a script.

Also remember that most distros still ship mc (Midnight
Commander), which is a terminal clone of Norton Commander.
It's very useful for many operations, including bulk
renaming that is very DOS like (e.g., rename * [something]*).

> Can you break it down for me?
> I think it is saying:
> For everything in this directory move the variable named
> file called $x to a
> new file name "dune_3 + $x" to give you dune_3$x.
> This actually places the "dune_3" in the front of every file.

for every "x",
  where "x" is set equal to every expansion of glob "*"
  (everything in directory)
  move (rename) "x" to "[prefix]x"

NOTE:  the brackets have to be "escaped" with a backslash"

> Is this a form of SED or AWK?

Nope, it's a "for" loop in POSIX Bourne shell (sh), of
which GNU Bourne Again shell (bash) is a superset of.

It uses shell globs to expand out to filenames and subdirectories.
You can pass _multiple_ globs in a bash for loop.

E.g., if I have files ...
  ann.mp3
  bill.avi
  bob.mp3
  duh.mp3
  readme.txt

If I run ...

  for i in *.avi *.mp3 ; do echo $i ; done

I will get output of:  
  bill.avi
  ann.mp3
  bob.mp3
  duh.mp3

Because the globs literally expand so the command is ...

  for i in bill.avi ann.mp3 bob.mp3 duh.mp3 ; do ...

Globulars (globs) are pretty straight-forward in bash.
For little programs like this, you can write them on
a single line, just adding a semi-colon (;) to separate
lines.

  for i in *.avi *.mp3
  do echo $i
  done

Or:  

  for i in *.avi *.mp3
  do
    echo $i
  done

Can be written with the semi-colons:

  for i in *.avi *.mp3 ; do echo $i ; done

In fact, in scripts, a lot of people (myself included)
actually merge the "do" line in with the "for" line with
that semi-colon (;).  E.g.,

  for i in *.avi *.mp3 ; do
    echo $i
  done

Makes the "blocking/indention" a little nicer, and uses
one less line anyway.

> Or is it just that simple where it just adds
> a "dune_3" to each file name in the for loop?

Yep.

Sed is a stream editor, it can be used in bash as well.
It typically takes regular expressions (regex).

Awk is another processor, works on delimeters.
It also takes regex.

Awk and Sed have different approaches to solving the
same, and different, problems.  Awk can excel at handling
delimited data (spaces, tabs, comma, etc...) easiliy.

In all honesty, you didn't need to dive into regex here.
You were just prepending, and that's simple to do in bash
itself.


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the Leaplist mailing list