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.
- 37,804
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 /$
- 640
Alternatively:
ls -lAh | grep -v '^d'
This method lists in
-lLong list format-ADisplays almost all (show hidden files but don't show.and..)-hHuman readable file sizes
while grep
-vDon't show matching recordsRegular expressionfilter^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.
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 "^."
}
- 31,035
- 161
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 *.?*
- 16,703
- 161
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
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
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