0

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 ' ' * \;
  • 1
    Try: find . -type f -exec rename .hs '' {} \;. If that doesn't work, tell us which of the various and incompatible versions of the command rename is the one that you are using. – John1024 Feb 22 '18 at 5:24
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.]

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.