12

For example, how can I replicate:

ls *.txt

when my text files are missing the .txt ending, and as far as extensions go appear the same as many other types of file?

Alternatively, is it possible to list specifically just the files which have NO file extension?

5 Answers 5

10

To match files that do not have an extension at all, you can use the command

ls | grep -v '\.'

To match files that do not have a .txt extension, you can use the command

ls | grep -v '\.txt'

This will pass the list of files in the current directory to grep, which will remove all file names that have a . (or .txt for the second command) in them.

3
  • Thankyou! This answered my question. Additionally, is it possible to use the same grep tool with other commands? For instance, with todos? (to convert line endings)
    – user82212
    Jan 22, 2013 at 20:31
  • 1
    Yes, grep can be used with any command. The | symbol is used to pass output from one command to another, and grep will match lines that have (or don't have) specific text. But if you want to modify the text (other than just printing matching lines), you should take a look at the sed command.
    – iBelieve
    Jan 22, 2013 at 20:38
  • To find only text files (excluding directories and binaries) use: ls --classify | grep -v '/'.'*'
    – chaskes
    Jan 22, 2013 at 20:53
6

With ls -ignore="PATTERN" you can exclude files from a ls result.

For example, ls --ignore="*.txt" to ignore txt files.

3

Simply negate your pattern (note: shopt -s extglob is required).

To display all files without extension:

ls !(*.*)

To show non-txt files, try:

ls !(*.txt)
0

The command ls -SX1|less will sort by extension in a single column and files without one will be moved to the top. Piping through less as in the example is suggested for directories with more than a few files to limit output to a screen full at a time.

0

I prefer avoiding the use of grep for simple listing operations.

In order to avoid using grep,

I recommend using ls !(*.*) to find files with no file extension, and

ls -I "*.gz" -I "*.tz" or ls --ignore={"*.jpg","*.png","*.bmp"}

to exclude specific file extensions

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .