How to list only files and not directories of a directory Bash_ - Stack Overflow
How to list only files and not directories of a directory Bash_ - Stack Overflow
Asked 12 years, 7 months ago Modified 5 months ago Viewed 235k times
178 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?
bash file-listing
Share Improve this question Follow edited Dec 9, 2018 at 18:32 asked May 13, 2012 at 20:04
Ciro Santilli Rodrigo
OurBigBook.com 12.7k 23 70 101
380k 116 1.3k 1.1k
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 May 30, 2018 at 1:09
Possible duplicate of How to get the list of files in a directory in a shell script? – codeforester May 30, 2018 at 1:43
1 None of the proposed duplicates seem to implement the requirement to omit directories. – tripleee May 30, 2018 at 4:12
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.
Share Improve this answer Follow answered May 13, 2012 at 20:06
carlpett
12.5k 5 50 82
After my comment to mklement0's answer, I realized that "find ./*.png -maxdepth 1 -type f > pngs.txt" would probably accomplish the same. It
does. Without installing a script. – Alex Hall Sep 26, 2015 at 5:29
2 find on mac, does not have neither -type , nor -maxdepth options. – Timofey Jul 3, 2016 at 17:36
3 @Tim: -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. – mklement0 Jul 7, 2016 at 16:42
2 @AlexHall: That's a clever solution (though I suggest 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. – mklement0 Jul 7, 2016 at 17:09
3 To contrast 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. – mklement0 Jul 7, 2016 at 17:22
42 ls -p | grep -v /
ls -p lets you show / after the folder name, which acts as a tag for you to remove.
3 It's nice to see an example using ls in addition to find , since the latter returns relative paths and the former only filenames. Use the right
Share Improve this answer Follow answered Sep 9, 2017 at 22:03
tool for the job. – Ben Amos Mar 21, 2018 at 19:05
Code42
hi! this doesn't work for me. did you mean ls -p | grep -v ./ – user8395964 Jan 29 at 7:07 2,472 1 20 23
@user8395964 no, file names can't have a / in them, so you did something other than what his answer does – Ed Swangren Sep 16 at 5:44
what this answer misses is color coding ( ls <=> grep ) – Ed Swangren Sep 16 at 5:46
10 • carlpett's find -based answer ( find . -maxdepth 1 -type f ) works in principle, but is not quite the same as using ls :
you get a potentially unsorted list of filenames all prefixed with ./ , and you lose the ability to apply ls 's many
options;
also find invariably finds hidden items too, whereas ls ' behavior depends on the presence or absence of the -a or -
A options.
• An improvement, suggested by Alex Hall in a comment on the question is to combine shell globbing with find :
• 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.
• Hans Roggeman's ls + grep answer is pragmatic, but locks you into using long ( -l ) output format.
• a utility that provides the output flexibility of ls while also providing type-filtering capability,
• simply by placing type-filtering characters such as f for files, d for directories, and l for symlinks before a list of ls
arguments (run fls --help or fls --man to learn more).
Examples:
Installation
Supported platforms
Note:
• Whether you need sudo depends on how you installed Node.js / io.js and whether you've changed permissions later; if you
get an EACCES error, try again with sudo .
• The -g ensures global installation and is needed to put fls in your system's $PATH .
Manual installation
• Download this bash script as fls .
• Move it or symlink it to a folder in your $PATH , such as /usr/local/bin (macOS) or /usr/bin (Linux).
1 Brilliant. Even if it involves creating another script. I copied and pasted from the plain text source you link to, into a text file, and ran it in Cygwin
Share Improve this answer Follow edited Apr 19, 2023 at 8:28 answered Jun 3, 2015 at 11:35
with this command: fls.sh f *.png > pngs.txt And bingo: a list of .png files without paths. But really, ls doesn't have an option comparable to
mklement0
DOS' "dir /b" switch? I looked through the contents of ls' --help output, and nothing of the sort is there, at least. – Alex Hall Sep 26, 2015 at 5:25
434k 68 697 902
1 @AlexHall: Thanks; tl;dr: try -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.
– mklement0 Sep 26, 2015 at 13:23
8 Listing content of some directory, without subdirectories
I like using ls options, for sample:
so
sorted by size:
Then
You could replace /some/path by . to list for current directory or .. for parent directory.
Share Improve this answer Follow edited Apr 22, 2020 at 6:35 answered Apr 22, 2020 at 6:21
F. Hauri - Give Up
GitHub
70k 18 128 146
3 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'
Share Improve this answer Follow answered Jan 21, 2014 at 13:10
crogg01
2,516 17 35
I wouldn't put this in a script for the reason given above, but another quick-and-dirty solution: ls -F | grep -v '/$' – mooie Jan 25, 2019 at 11:33
@mooie, what does the '/$' mean or why does it remove the directories from the output of ls? – Classified Apr 7, 2023 at 0:24
1 @Classified "ls -F" displays a slash ("/") immediately after each pathname that is a directory. "grep" uses the dollar sign ("$") to indicate an end
of a line. So "grep '/$'" would match all lines outputted by "ls -F" that end in a slash, i.e. directories. "grep -v" prints all non-matching lines, i.e.
everything but directories, which will be file names (and symbolic links). – mooie Apr 8, 2023 at 10:30
2 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).
Share Improve this answer Follow answered Mar 27, 2020 at 22:49
Richard
47 2
Wow! What if filenames do contain spaces? – F. Hauri - Give Up GitHub Apr 22, 2020 at 6:27
use "9-" to get everything from the 9th field on. Links also are affected by this since they'll be after the 9th field. – Richard Nov 16, 2020 at 16:11
ls -l | grep '^-'
Share Improve this answer Follow answered Aug 10, 2021 at 12:38
Anthony Rutledge
7,534 2 40 46
added xargs to make it works, and used -1 instead of -l to show only filenames without additional ls info
Share Improve this answer Follow edited May 19, 2017 at 18:10 answered May 19, 2017 at 10:52
arghtype Anabioz
4,534 11 49 61 11
This has some merit, in that it enforces sorting by time, though there are arguably less clumsy ways to do that with find -printf0 '...' |
sort -z – tripleee May 30, 2018 at 4:11
Share Improve this answer Follow answered Jan 13, 2022 at 15:02
Abdullah
249 2 18
Share Improve this answer Follow answered Nov 21, 2022 at 5:49
mug896
2,015 1 23 18
0 to list only file names (and directories at final point) without folders and anything else (properties, sizes etc) I used:
ls -aRp
• 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.
Share Improve this answer Follow answered Jun 16, 2023 at 12:32
Estatistics
946 12 26
0 # ls -l | grep -v '^d' | awk '{print $9}'
Share Improve this answer Follow answered Oct 23, 2023 at 15:06
Alex
983 9 9
-2 Just adding on to carlpett's answer. For a much useful view of the files, you could pipe the output to ls.
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 This probably doesn't behave as you expect! ls doesn't read standard input, so it's useless to pipe anything to ls . Did you miss xargs ?
– gniourf_gniourf Mar 4, 2015 at 7:12
To put it differently: the find command in this answer is completely ignored; the overall command is the same as ls -lt | less , which
performs no type filtering. – mklement0 Jul 7, 2016 at 17:34
1 Though with GNU 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. – tripleee May 30, 2018 at 4:08
-2 "find '-maxdepth' " does not work with my old version of bash, therefore I use:
Share Improve this answer Follow answered Oct 12, 2016 at 13:40
Gill Davison
1
The use of 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. – tripleee May 30, 2018 at 4:06