Linux find command for filenames without extension for unknown extensions

filenamesfind

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:

find . -type f ! -name '*.*'

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 invoke bash's history expansion facility.

To print only the filename component of the found pathname, with GNU find:

find . -type f ! -name '*.*' -printf '%f\n'

With standard find (or GNU find for that matter):

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

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

find . -type f -exec sh -c '
    for pathname do
        pathname=$( basename "$pathname" )
        printf "%s\n" "${pathname%.*}"
    done' sh {} +

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`".

Related Question