I figure there has to be a way of making ls only display non-directories, but the man page doesn't make it obvious
8 Answers
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.

- 36,774
-
1I 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... – 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 – Aug 19 '16 at 00: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
-
this is not a good solution, it does cut out files containing / in its path:
mkdir a;touch a/b; ls a/*| grep -v /
– a much better solution for your problem is to usefind
– rubo77 Apr 10 '20 at 07:45 -
You cannot have forward slashes in filenames in Linux. ext3/4 doesn't allow it. Some other underlying filesystems may theoretically support it but it wouldn't work properly in Linux; the system calls wouldn't support it and most software would be expected to have problems with it. Don't try putting forward slashes in filenames in Linux - unless they are encoded in some way in which case it won't be an issue here. – thomasrutter Apr 10 '20 at 10:06
-
-
-
grep -v /$
would filter out eny lines ending with a slash. Fixes rubo77's corner case. – VXDguy Oct 20 '21 at 20:53 -
@VXDguy I don't understand the issue you or rubo77 mentioned. As far as I understand the question we do want to filter out lines containing a slash because we want to filter out directories. File names contain no slash. – thomasrutter Oct 22 '21 at 04:10
-
@VXDguy upon re-reading, it looks like you're wanting something that works if you're running ls of another directory. In which case yes your workaround would solve that. – thomasrutter Oct 22 '21 at 04:16
-
1
-p
will not add the/
suffix for symbolic links that point to directories. Keep that in mind. – Martin Braun Oct 16 '22 at 22:56 -
1
-
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 /$

- 620
-
4
find . -maxdepth 1 -not -type d | xargs ls
- literally make ls show only the non-directory files. – Wil Nov 03 '20 at 23:55
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 recordsRegular expression
filter^d
- Those start with letter d (for directory) i.edrwxrwxr-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.
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.
@
means symbolic link (or that the file has extended attributes).*
means executable.=
means socket.|
means named pipe.>
means door./
means directory.
ls -F | grep -Ev '/|@|*|=|>|\|'
Above command displays only files.

- 166
-
Executables are also files. If you want loop files incl. executables, but no links, since they could be folders as well, use
for file in $(ls -1F "$path" | grep -Ev "\||/|@|=|>"); do
and access the full path using$path/${file//\*}
. – Martin Braun Oct 16 '22 at 23:15
If you want only files and don't want to perform any operation on them, then run:
ls -lA | grep -v '^d'
Or, if you want to iterate on each file, then for me this works:
ls *.?*

- 14,585

- 151
-
Using long listing with
-l
switch will be slow if you have many files. – EsmaeelE Jan 16 '23 at 19:48
If you only want to see only files, directories or both.
Or if you want to see hidden files, directories or not.
Use these bash functions:
showVisibleFilesOnly() {
ls -p | grep -v /
}
showVisibleFoldersOnly() {
ls -p | grep / | grep "^."
}
showOnlyFilesIncludingHidden() {
ls -Ap | grep -v / | grep "^."
}
showOnlyFoldersIncludingHidden() {
ls -Ap | grep / | grep "^."
}
showHiddenFoldersOnly() {
ls -Ap | grep / | grep "^." | grep "."
}
showHiddenFilesOnly() {
ls -Ap | grep -v / | grep "^." | grep "."
}
showAllFilesAndFoldersIncludingHidden() {
ls -Ap
}
showHiddenFilesAndFoldersOnly() {
ls -Ap | grep "^."
}

- 26,018

- 141
-
Now you can have exactly what you want when you want it with ease – jasonleonhard Mar 08 '21 at 23:02
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

- 11
-
1i 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! – Oct 30 '19 at 22:22
You might want to use du
instead of ls
. It will only output files. Then just awk '{print $2}'
to output only the file path.
You have to use the -d option with du to limit depth. http://linuxcommand.org/lc3_man_pages/du1.html
-
However, this would show the files even in subdirectories of subdirectories. – Kulfy Oct 31 '20 at 10:18
-
That is true. You can add the -d flag to limit the depth though. I have added an edit to my response to reflect that. – mixcocam Nov 02 '20 at 15:15
-
1Yeah. But that would still show directories, for example,
Desktop
when run from$HOME
and won't be an answer to this question since the questioner wants to list files only. – Kulfy Nov 03 '20 at 13:20
ls
, but you can easily do it with thefile
command:file /directory/containing/the/files -maxdepth 1 -not -type d
. – Henning Kockerbeck Aug 11 '16 at 22:03ls
throughgrep
, something likels -1F /directory/containing/the/files | grep -vE /$
. But be aware that parsing the output of ls can be tricky. – Henning Kockerbeck Aug 11 '16 at 22:08