linux find command for filenames without extension for unknown extensions
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`".
In Linux, there is no such thing as a file extension. A .
in a file name has no significance whatsoever (notwithstanding that a .
as the first character in a filename identifies it as a hidden file).
Also, checking the manual page for find
on my system shows no --ignore
option.
That said, if you want to ignore any files with a .
in their name, you can use find
's -not
operator:
find -type f -not -name '*.*' -print
As other's have pointed out, "extensions" don't really mean anything to Unix systems. But we can remove them from a listing with a simple sed
command.
e.g.
find * -type f -print | sed 's/\.[^.]*$//'
If there are directories and you don't want them to be shown in the listing then
find * -type f -printf "%f\n" | sed 's/\.[^.]*$//'
For a single directory we can just use ls
instead of find
ls | sed 's/\.[^.]*$//'