Differences between revisions 6 and 7
Revision 6 as of 2008-06-30 14:14:16
Size: 4764
Editor: GreyCat
Comment: note GNU/BSD find requirement
Revision 7 as of 2008-09-25 17:10:06
Size: 5424
Editor: 137
Comment: Added -- to mv; added recursive rename example
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
for f in *.foo; do mv "$f" "${f%.foo}.bar"; done for f in *.foo; do mv -- "$f" "${f%.foo}.bar"; done
Line 13: Line 13:
The "--" is to protect from problematic filenames that begin with "-".
Line 17: Line 18:
for f in *\ *; do mv "$f" "${f// /_}"; done for f in *\ *; do mv -- "$f" "${f// /_}"; done
Line 28: Line 29:
  mv "$f" "${f%.foo}.bar"   mv -- "$f" "${f%.foo}.bar"
Line 41: Line 42:

Here's an example script that uses depth-first recursion (changes spaces to underscores, but you just need to change the ren() function to do anything you want) to rename both files and directories (again, it's easy to modify to make it act only on files or only on directories):

{{{
ren() {
  new=${1// /_}
  [ "$1" != "$new" ] && mv -- "$1" "$new"
}

traverse() {
  local i
  cd -- "$1" || exit 1
  for i in *; do
    [ -d "$i" ] && traverse "$i"
    ren "$i"
  done
  cd .. || exit 1
}

# main program
shopt -s nullglob
traverse /path/to/startdir
}}}
Line 60: Line 84:
    mv "$file" "$newname"     mv -- "$file" "$newname"
Line 92: Line 116:
    mv "$file" "$newname"     mv -- "$file" "$newname"
Line 108: Line 132:
   mv "$file" "$newname"    mv -- "$file" "$newname"
Line 112: Line 136:
It should be noted that all three of these examples contain a [:RaceCondition:race condition] -- an existing file could be overwritten if it is created in between the `[ -f "$newname" ...` and `mv "$file" ...` commands. Solving this issue is beyond the scope of this page, however. It should be noted that all these examples contain a [:RaceCondition:race condition] -- an existing file could be overwritten if it is created in between the `[ -f "$newname" ...` and `mv "$file" ...` commands. Solving this issue is beyond the scope of this page, however.

Anchor(faq30)

How can I rename all my *.foo files to *.bar, or convert spaces to underscores, or convert upper-case file names to lower case?

Some GNU/Linux distributions have a rename(1) command, which you can use for the former; however, the syntax differs from one distribution to the next, so it's not a portable answer. Consult your system's man pages if you want to learn how to use yours, if you have one at all. It's often perfectly good for one-shot interactive renames, just not in portable scripts. We don't include any rename(1) examples here because it's too confusing -- there are two common versions of it and they're totally incompatible with each other.

You can do mass renames with [:BashFAQ/073:Parameter Expansion], like this:

# POSIX
for f in *.foo; do mv -- "$f" "${f%.foo}.bar"; done

The "--" is to protect from problematic filenames that begin with "-". Here's a similar example, this time replacing spaces in filenames with underscores:

# Bash
for f in *\ *; do mv -- "$f" "${f// /_}"; done

This invokes the external command mv(1) once for each file, so it may not be as efficient as some of the rename implementations.

If you want to do it recursively, then it becomes much more challenging. This example renames *.foo to *.bar:

# Bash
# Also requires GNU or BSD find(1)
find . -name '*.foo' -print0 | while read -r -d $'\0' f; do
  mv -- "$f" "${f%.foo}.bar"
done

For more techniques on dealing with files with inconvenient characters in their names, see [:BashFAQ/020:FAQ #20].

The trickiest part of recursive renames is ensuring that you do not change the directory component of a pathname, because something like this is doomed to failure:

mv "./FOO/BAR/FILE.TXT" "./foo/bar/file.txt"

Therefore, any recursive renaming command should only change the filename component of each pathname. If you need to rename the directories as well, those should be done separately. Furthermore, recursive directory renaming should either be done depth-first (changing only the last component of the directory name in each instance), or in several passes. Depth-first works better in the general case.

Here's an example script that uses depth-first recursion (changes spaces to underscores, but you just need to change the ren() function to do anything you want) to rename both files and directories (again, it's easy to modify to make it act only on files or only on directories):

ren() {
  new=${1// /_}
  [ "$1" != "$new" ] && mv -- "$1" "$new"
}

traverse() {
  local i
  cd -- "$1" || exit 1
  for i in *; do
    [ -d "$i" ] && traverse "$i"
    ren "$i"
  done
  cd .. || exit 1
}

# main program
shopt -s nullglob
traverse /path/to/startdir

To convert filenames to lower case, if you have the utility mmv(1) on your machine, you could simply do:

# convert all filenames to lowercase
mmv "*" "#l1"

Otherwise, tr(1) may be helpful:

# tolower - convert file names to lower case
# POSIX
for file in "$@"
do
    [ -f "$file" ] || continue                # ignore non-existing names
    newname=$(echo "$file" | tr '[:upper:]' '[:lower:]')     # lower case
    [ "$file" = "$newname" ] && continue      # nothing to do
    [ -f "$newname" ] && continue             # don't overwrite existing files
    mv -- "$file" "$newname"
done

We use the fancy range notation, because tr can behave very strangely when using the A-Z range on some systems:

imadev:~$ echo Hello | tr A-Z a-z
hÉMMÓ

To make sure you aren't caught by surprise when using tr with ranges, either use the fancy range notations, or set your locale to C.

imadev:~$ echo Hello | LC_ALL=C tr A-Z a-z
hello
imadev:~$ echo Hello | tr '[:upper:]' '[:lower:]'
hello
# Either way is fine here.

This technique can also be used to replace all unwanted characters in a file name, e.g. with '_' (underscore). The script is the same as above, with only the "newname=..." line changed.

# renamefiles - rename files whose name contain unusual characters
# POSIX
for file in "$@"
do
    [ -f "$file" ] || continue            # ignore non-regular files, etc.
    newname=$(echo "$file" | sed 's/[^[:alnum:]_.]/_/g')
    [ "$file" = "$newname" ] && continue  # nothing to do
    [ -f "$newname" ] && continue         # do not overwrite existing files
    mv -- "$file" "$newname"
done

The character class in [] contains all the characters we want to keep (after the ^); modify it as needed. The [:alnum:] range stands for all the letters and digits of the current locale.

Here's an example that does the same thing, but this time using Parameter Expansion instead of sed:

# renamefiles (more efficient, less portable version)
# Bash
for file in "$@"; do
   [ -f "$file" ] || continue
   newname=${f//[^[:alnum:]_.]/_}
   [ "$file" = "$newname" ] && continue
   [ -f "$newname" ] && continue
   mv -- "$file" "$newname"
done

It should be noted that all these examples contain a [:RaceCondition:race condition] -- an existing file could be overwritten if it is created in between the [ -f "$newname" ... and mv "$file" ... commands. Solving this issue is beyond the scope of this page, however.

BashFAQ/030 (last edited 2020-06-10 14:10:22 by GreyCat)