Hello I am trying to delete the /. I have problem looks like this , find all the files with the .sh extension in the current folder and delete the ./ before the name and the .sh after the name and this has to be within a file with the extension .sh which I named find_sh.sh. I have tried find . -name '*.sh' -print | sed 's/[.]//g' | sed 's/[/]//g' | sed 's/.sh//g' but this also deletes _ . What I am doing wrong?


I have done it like this and it works find . -name '*.sh' -print | sed 's/[.]//g' | sed 's/[/]//g' | sed 's/sh//' and it works.

| improve this answer | |
  • 1
    Note that you could simplify that to find . -name '*.sh' | sed 's#\./##; s#\.sh$##. – terdon Jul 6 '16 at 9:50

In your pattern . match any character, not only literal dot. You need to escape it with backslash to prevent that. Your other solution on the other hand removes all dots from the filenames, i.e. a.b.sh becomes ab. I propose the following alternative:

find . -name '*.sh' | sed 's,^./,,g' | sed 's/.sh$//'

Notice ^ which matches the beginning of the line and $ which matches the end.

But even better use only one sed command:

find . -name '*.sh' | sed 's,^./\(.*\).sh$,\1,'
| improve this answer | |
  • I have done it this way : find . -name '*.sh' -print | sed 's/.//' | sed 's/^[^_]*[/]//g' | sed 's/\.sh//' – JohnnyOnPc Jul 6 '16 at 10:20
  • @JohnnyOnPc create file: touch my.shell.script.sh and see the result of your command. – jimmij Jul 6 '16 at 10:55

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.