28

I figure there has to be a way of making ls only display non-directories, but the man page doesn't make it obvious

45
ls -p | grep -v /

Using ls -p tells ls to append a slash to entries which are a directory, and using grep -v / tells grep to return only lines not containing a slash.

  • I checked this one because it's my favorite answer (while i did upvote all of them), but now trying to find a way to put it in columns and reverse the order of output... – thinksinbinary Aug 12 '16 at 12:11
  • "You can use 1 switch for single column list" sorry, i did try to figure out what you meant by that, i would appreciate and example/explanation if you would, i only know what a switch is in regards to C programming – thinksinbinary Aug 19 '16 at 0:30
  • @sdkks You don't need the 1 switch when piping the output as it will default to single column in that situation. If that is what you were meaning. – thomasrutter Aug 21 '16 at 23:27
  • @thinksinbinary not sure how to make it multi column but you could search for or ask your own separate question and someone will know. – thomasrutter Aug 21 '16 at 23:28
9

You may try this:

find . -maxdepth 1 -not -type d

And map this to a special alias.

But if you're really keen on using the ls command, here:

ls -p | egrep -v /$

8

Alternatively:

ls -lAh | grep -v '^d'

This method lists in

  • -l Long list format
  • -A Displays almost all (show hidden files but don't show . and ..)
  • -h Human readable file sizes

while grep

  • -v Don't show matching records
  • Regular expression filter ^d - Those start with letter d (for directory) i.e drwxrwxr-x <some file details> <foldername>

If you don't want to type every time, you may make it into an alias for your bash/shell profile.

2

I saw in your( @thinksinbinary ) comment on the answer by @thomasrutter , that you wanted to be able to print them in reverse order and in columns. You probably have already figured it out or moved on, but here it is:

ls -pr | grep -v / | column
  • -p adds the forward slash ('/') to the directory names
  • -r reverses the order of output
  • -v lets grep do an inverse search to print everything except the directories (everything that doesn't have the '/' that -p put there)
  • "column puts it in columns" - Captain Obvious
  • 1
    i do think it's cool that on the ubuntu forum people still comment on and read your posts after a long time. I've been wanting to get back into linux in order to learn assembly and operating systems since doing so on windows is much more difficult. Thanks! – thinksinbinary Oct 30 '19 at 22:22
0
ls -F | grep -v /

Above command displays files, But it includes symlinks, pipes, etc. If you want to eliminate them too, you can use one of the flags mentioned below.

ls -F appends symbols to filenames. These symbols show useful information about files.

ls -F | grep -Ev '/|@|*|=|>|\|'

Above command displays only files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.