Question: I have a directory "FolderName" with 10,000 new files every day. Almost a half of those files are named as follows:

```
filename_yyyy-mm-dd_hh:mm

```

while the other half are named:

```
filename_yyyy-mm-dd hh:mm

```

(with space instead of underscore)

The script I'd like to set up should do the following:

Rename only the files containing a space in their name, skipping files that need no processing.

I cannot find a way to make the script efficient, I need to really skip the good half, my script tries to mv any file, and it's quite long and inefficient. Any good idea for a better design?

Thanks everybody

Answer: 
Check out the [`rename`](http://search.cpan.org/~rmbarker/File-Rename-0.06/rename.PL) utility, a Perl script designed just for this, it's powerful and fast.

```
rename -n ' ' _ *\ *

```

Or:

```
find /path/to/dir -type f -name '* *' -exec rename -n ' ' _ {} \;

```

The `-n` flag is for dry run, to print what works happen without actually renaming anything. If the output looks good, remove the flag and rerun.
`for f in "$(find . -name '* *')"; do mv $f $(echo $f | sed 's/\ /_/'); done` will do it. There should be a better way using find's `-exec` option as well.

EDIT:
If the function is put in a script, then find can be used directly:

```
cat <<EOF > space_to_underscore
#!/usr/bin/env bash
mv "$1" "$(sed 's/\ /_/' <(echo "$1"))"
EOF
chmod +x space_to_underscore

find . -name '* *' -exec ./space_to_underscore {} \;

```

This will be faster than using a for loop.