How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?
-
Possible duplicate of How to get the list of files in a directory in a shell script?, How to list files in directory using bash?, Shell script print out file names when given a folder, List files in current directory with full path using Bash, What expands to all files in current directory recursively?, Show files in current directory using Git Bash?, etc.jww– jww2018-05-30 01:09:00 +00:00Commented May 30, 2018 at 1:09
-
Possible duplicate of How to get the list of files in a directory in a shell script?codeforester– codeforester2018-05-30 01:43:31 +00:00Commented May 30, 2018 at 1:43
-
1None of the proposed duplicates seem to implement the requirement to omit directories.tripleee– tripleee2018-05-30 04:12:43 +00:00Commented May 30, 2018 at 4:12
15 Answers
Using find:
find . -maxdepth 1 -type f
Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.
6 Comments
find on mac, does not have neither -type, nor -maxdepth options.-type and -maxdepth aren't options in the normal sense; BSD find (as used on OS X) calls them primaries, and they must come after the filename operand(s) (., in this case; note that, unlike on Linux, the BSD version needs at least one explicit filename operand); the command in this answer definitely works on a Mac.find *.png -maxdepth 0 -type f to avoid the ./ prefix in the output filenames; also note the -maxdepth of 0, not 1), as long as all you need is the file names in alphabetical order. If you want what ls can otherwise do for you (different output format/ordering, inline control over whether hidden items are included or not), supplemented with [multi-]type filtering, the script from my answer can help.find * -maxdepth 0 -type f (an alternative derived from @AlexHall's comment) with find . -maxdepth 1 -type f from the answer: find . ... invariably includes hidden items, invariably prefixes output filenames with ./, and, with GNU find (Linux), typically outputs an unsorted list. find * ..., due to letting the shell perform globbing up front, by default excludes hidden items (can be changed with shopt -s dotglob), outputs mere filenames (no prefix), sorted alphabetically. Neither approach includes symlinks to files; use option -L to do so.ls -p | grep -v /
ls -p lets you show / after the folder name, which acts as a tag for you to remove.
4 Comments
ls in addition to find, since the latter returns relative paths and the former only filenames. Use the right tool for the job.ls -p | grep -v .// in them, so you did something other than what his answer doesls <=> grep)carlpett's
find-based answer (find . -maxdepth 1 -type f) works in principle, but is not quite the same as usingls: you get a potentially unsorted list of filenames all prefixed with./, and you lose the ability to applyls's many options;
alsofindinvariably finds hidden items too, whereasls' behavior depends on the presence or absence of the-aor-Aoptions.An improvement, suggested by Alex Hall in a comment on the question is to combine shell globbing with
find:find * -maxdepth 0 -type f # find -L * ... includes symlinks to files- However, while this addresses the prefix problem and gives you alphabetically sorted output, you still have neither (inline) control over inclusion of hidden items nor access to
ls's many other sorting / output-format options.
- However, while this addresses the prefix problem and gives you alphabetically sorted output, you still have neither (inline) control over inclusion of hidden items nor access to
Hans Roggeman's
ls+grepanswer is pragmatic, but locks you into using long (-l) output format.
To address these limitations I wrote the fls (filtering ls) utility,
- a utility that provides the output flexibility of
lswhile also providing type-filtering capability, - simply by placing type-filtering characters such as
ffor files,dfor directories, andlfor symlinks before a list oflsarguments (runfls --helporfls --manto learn more).
Examples:
fls f # list all files in current dir.
fls d -tA ~ # list dirs. in home dir., including hidden ones, most recent first
fls f^l /usr/local/bin/c* # List matches that are files, but not (^) symlinks (l)
Installation
Supported platforms
- When installing from the npm registry: Linux and macOS
- When installing manually: any Unix-like platform with Bash
From the npm registry
Note: Even if you don't use Node.js, its package manager, npm, works across platforms and is easy to install; try
curl -L https://git.io/n-install | bash
With Node.js installed, install as follows:
[sudo] npm install fls -g
Note:
Whether you need
sudodepends on how you installed Node.js / io.js and whether you've changed permissions later; if you get anEACCESerror, try again withsudo.The
-gensures global installation and is needed to putflsin your system's$PATH.
Manual installation
- Download this
bashscript asfls. - Make it executable with
chmod +x fls. - Move it or symlink it to a folder in your
$PATH, such as/usr/local/bin(macOS) or/usr/bin(Linux).
2 Comments
-1 (the number one); ls actually defaults to "bare" output, i.e., filenames only, but when outputting to a terminal uses a column-based layout (multiple filenames per line). To get one filename per output line (which is what dir /b does, if memory serves), use the -1 option, which is actually implicitly on when stdout is not connected to a terminal, such as when sending to a pipe or output file.Listing content of some directory, without subdirectories
I like using ls options, for sample:
-luse a long listing format-tsort by modification time, newest first-rreverse order while sorting-F,--classifyappend indicator (one of */=>@|) to entries-h,--human-readablewith -l and -s, print sizes like 1K 234M 2G etc...
Sometime --color and all others. (See ls --help)
Listing everything but folders
This will show files, symlinks, devices, pipe, sockets etc.
so
find /some/path -maxdepth 1 ! -type d
could be sorted by date easily:
find /some/path -maxdepth 1 ! -type d -exec ls -hltrF {} +
Listing files only:
or
find /some/path -maxdepth 1 -type f
sorted by size:
find /some/path -maxdepth 1 -type f -exec ls -lSF --color {} +
Prevent listing of hidden entries:
To not show hidden entries, where name begin by a dot, you could add ! -name '.*':
find /some/path -maxdepth 1 ! -type d ! -name '.*' -exec ls -hltrF {} +
Then
You could replace /some/path by . to list for current directory or .. for parent directory.
Comments
You can also use ls with grep or egrep and put it in your profile as an alias:
ls -l | egrep -v '^d'
ls -l | grep -v '^d'
3 Comments
find files: ls -l /home | grep "^-" | tr -s ' ' | cut -d ' ' -f 9
find directories: ls -l /home | grep "^d" | tr -s ' ' | cut -d ' ' -f 9
find links: ls -l /home | grep "^l" | tr -s ' ' | cut -d ' ' -f 9
tr -s ' ' turns the output into a space-delimited file the cut command says the delimiter is a space, and return the 9th field (always the filename/directory name/linkname).
I use this all the time!
2 Comments
You are welcome!
ls -l | grep '^-'
Looking just for the name, pipe to cut or awk.
ls -l | grep '^-' | awk '{print $9}'
ls -l | grep '^-' | cut -d " " -f 13
1 Comment
{ find . -maxdepth 1 -type f | xargs ls -1t | less; }
added xargs to make it works, and used -1 instead of -l to show only filenames without additional ls info
1 Comment
find -printf0 '...' | sort -zto list only file names (and directories at final point) without folders and anything else (properties, sizes etc) I used:
ls -aRp
- a=all entries (not ignore entries start with ".")
- R=recursive (in all subfolders)
- p=append "/" indicator to directories (if you like to clean them later) eg. /temp/list/a1/ will be listed as /a1 only
if you like to compare file existence in two different places then apply this too but it sort all files and directories independently of folder structure.
ls -aRp | sort --dictionary-order
Comments
# ls -l | grep -v '^d' | awk '{print $9}'
1 Comment
Just adding on to carlpett's answer. For a much useful view of the files, you could pipe the output to ls.
find . -maxdepth 1 -type f|ls -lt|less
Shows the most recently modified files in a list format, quite useful when you have downloaded a lot of files, and want to see a non-cluttered version of the recent ones.
3 Comments
ls doesn't read standard input, so it's useless to pipe anything to ls. Did you miss xargs?find command in this answer is completely ignored; the overall command is the same as ls -lt | less, which performs no type filtering.find you could do find . -maxdepth 1 -type f -ls. It also seems to work on Mac OS, though I believe -maxdepth and -ls are not properly portable options."find '-maxdepth' " does not work with my old version of bash, therefore I use:
for f in $(ls) ; do if [ -f $f ] ; then echo $f ; fi ; done
1 Comment
for f in $(ls) is doubly wrong. You could fix that with for f in *but then it would break because you are not quoting the argument to echo properly.