0

New to linux and bash, with ls command I can list file names in a directory. My directory contains text files. I want to pipe the listed file names to some other command that would remove the file extensions, How can I achieve that

ls
filename1.txt
filename2.txt

to

ls | somecommand
filename1
filename2

any help would be appreciated

Thanks

EMED
  • 11
  • 4

1 Answers1

0

Typically edit streams with Stream EDitor (sed). You could just remove everything from a comma till the end. With zero terminated strings, one could use -print0 and GNU sed version with -z.

find . -maxdepth 1 -type f -printf "%f\n" | sed 's/\..*//'

If all files are *.txt you could use basename, which allows to work with zero terminated strings with -print0 and xargs -0:

find . -maxdepth 1 -type f -name '*.txt' | xargs -I{} basename {} .txt

Do not use ls in scripts.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111