How to rename multiple files using find and rename
Solution 1
Replace:
find . -type f -exec rename .hs ' ' * \;
With:
find . -type f -exec rename .hs '' {} \;
In an -exec
command, the place where you want your file name to go should be marked with {}
.
Also, as I am guessing that you want .hs
removed, not replaced with a space, we have removed the space between the single-quotes.
Documentation
man find
includes an explanation of the -exec
option:
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of;
is encountered. The string{}
is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a\
) or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the-execdir
option instead. [Emphasis added.]
Solution 2
Using Shell (Ksh, Bash, ksh93, mksh, zsh) Pattern substitution expansion ${var/Pattern/Replacement}
(First match of Pattern, within var replaced with Replacement. Or ${var//Pattern/Replacement}
; Global replacement. All matches of Pattern, within var replaced with Replacement. If Replacement is omitted, then the match(es) of Pattern is replaced by nothing, that is, deleted.)
find . -type f -exec bash -c 'echo mv "$1" "${1/.sh/}"' _ {} \;
Ps, remove echo
to rename on file.
Related videos on Youtube
user277260
Updated on September 18, 2022Comments
-
user277260 almost 2 years
I want to take out .hs from all files. Why this command doesn`t work properly? It leaves the same name of files.
find . -type f -exec rename .hs ' ' * \;
-
John1024 over 6 yearsTry:
find . -type f -exec rename .hs '' {} \;
. If that doesn't work, tell us which of the various and incompatible versions of the commandrename
is the one that you are using.
-