Gabe Spound
Gabe Spound

Reputation: 590

Shell using sed to remove file extension from find results

I am trying to run

find . \ -type f -name "*.sh" -exec basename {} \; | sed "s/.sh/ "

to display all files in the currenty directoy, and subdirectories, that end in .sh. I use -exec basename {} to remove the location of the file, so I just get the filenames themselves. The find command is working fine, but when i pipe it into sed "s/.sh/ " I get an error message sed: 1: "s/.sh/ ": unterminated substitute in regular expression. I am trying to replace the .sh extension with nothing, so I just get filenames.

Upvotes: 0

Views: 4329

Answers (2)

chepner
chepner

Reputation: 531948

basename can remove the extension for you.

find . -type f -name '*.sh' -exec basename {} .sh \;

Note this will work for all valid file names, not just ones that don't contain a newline.

If your basename command supports it, you can use the -s option to minimize the number of calls to basename you need.

find . -type f -name '*.sh' -exec basename -s .sh {} +

This allows multiple file names to be passed to each call to basename.

Upvotes: 2

Jean Carlo Machado
Jean Carlo Machado

Reputation: 1612

You are only missing the close / on sed.

find . -type f -name "*.sh" -exec basename {} \;  | sed "s/\.sh//"

Upvotes: 2

Related Questions