vidir is a Perl script from moreutils.

I didn’t know what this script could do at first, I thought it’s just numbered files and directories until I read its manpage:

When editing a directory, each item in the directory will appear on its own numbered line. These numbers are how vidir keeps track of what items are changed. Delete lines to remove files from the directory, or edit filenames to rename files. You can also switch pairs of numbers to swap filenames.

Bang! I was shocked. I had never thought of this way to rename files, to delete files, or to switch file names.

At first, I couldn’t think of any good example, then I recalled a question on Gentoo Forums, Recursively rename files and folders in Bash, which I had contributed a solution:

_rren() {
[[ "$1" == "" ]] && return 1
for fn in "$1"/*; do
   _fn="${fn// /_}"
   [[ "$fn" != "$_fn" ]] && mv "$fn" "$_fn" && fn="$_fn"
   [[ -d "$fn" ]] && ( _rren "$fn" )
done
true
}

_rren .

The OP wants to rename all files and directories, replace spaces with underlines. It has to be able to do it recursively.

With vidir:

$ find | sort | tail -n +2
./dir a b
./dir a b/e f
./dirc
./dirc/e f
./file a b
./file c d

$ find | sort | tail -n +2 | vidir -

In vi :%s/ /_/g, then save and quit. The filenames become:

$ find | sort | tail -n +2
./dir_a_b
./dir_a_b/e_f
./dirc
./dirc/e_f
./file_a_b
./file_c_d

Simple and safe because you can review the result. For another example, if you want to delete all *.png:

  1. find -type f.
  2. You search those files first: /.*\.png$.
  3. Check all highlights if they all are needed to be deleted; if not, do remove manually.
  4. Remove contents of those lines: :%s/.*\.png$//.

Simply brilliant!