I have a series of music folders. Some of the file names contain an underscore which I would like to get rid of.
With
find /Users/Chris/CDs -type f -name "*_*"
I find all of the files with underscores.
it appears that I can add -execdir mv {} to the command but do not know what to add from there.
I think {} provides the full path and file name as a string of the file with underscores but I do not know how to use something like sed 's/_//g' to remove the _ on the new file name.
Any help would be greatly appreciated.
Try:
How it works:
-execdir bash -c '...' Mover {} \;This starts up bash and tells it to run the command in the single quotes with
Moverassigned to$0and the file name assigned to$1.mv -i -- "$1" "${1//_/}"This renames file
$1. This uses bash's parameter expansion feature,${1//_/}, to create the target name from$1by removing all underlines.The option
-itellsmvto ask interactively before overwriting a file.The option
--tellsmvthat there are no more options. This is needed so that files whose names begin with-will be processed correctly.Example
Let's start with a directory with these files:
Next we run our command:
After the command completes, the files are:
The purpose of
$0Observe this command where we have added an error:
Note that
Moverappears at the beginning of the error message. This signals that the error comes from within thebash -ccommand.If we replace
Moverwith-, we would see:When running a single command in a terminal, the source of the error may still be obvious anyway. If this
findcommand were buried inside a long script, however, the use of a more descriptive$0, likeMoveror whatever, could be a big help.