I like using the following command to print file names but it prints them with extensions.
find . -type f -printf '%f\n'
In my directory, there are many files with different extensions. I tried adding --ignore='*.*'
both before and after -printf
, but it didn't work.
example; I have files myfile1.txt, myfile2.mp3, etc. I need it prints myfile1, myfile2, etc.
How would I do this?
Best Answer
If I understand you correctly (I didn't, see second part of the answer), you want to avoid listing filenames that contain a dot character.
This will do that:
The filename globbing pattern
*.*
would match any filename containing at least one dot. The preceding!
negates the sense of the match, which means that the pathnames that gets through to the end will be those of files that have no dots in their names. In really ancient shells, you may want to escape the!
as\!
(or update your Unix installation). The lone!
won't invokebash
's history expansion facility.To print only the filename component of the found pathname, with GNU
find
:With standard
find
(or GNUfind
for that matter):Before using this in a command substitution in a loop, see "Why is looping over find's output bad practice?".
To list all filenames, and at the same time remove everything after the last dot in the name ("remove the extension"), you may use
This would send all found pathnames of all files to a short shell loop. The loop would take each pathname and call
basename
on it to extract the filename component of the pathname, and then print the resulting string with everything after the last dot removed.The parameter expansion
${pathname%.*}
means "remove the shortest string matching.*
(a literal dot followed by arbitrary text) from the end of the value of$pathname
". It would have the effect of removing a filename suffix after the last dot in the filename.For more info about
find ... -exec ... {} +
, see e.g. "Understanding the -exec option of `find`".