Listing files with ls
- Version
- unversioned
- Review
- Draft
Summary#
ls lists directory contents. Names that start with a dot are hidden unless you ask for them with -a or -A.
Syntax#
ls -lA path
ls -lhS path
ls -ld path
-l adds permissions, owner, size and time. -h prints readable sizes. -A includes hidden entries except . and .., which -a also shows.
Example#
(
set -eu
export LC_ALL=C
dir=$(mktemp -d)
trap 'rm -rf -- "$dir"' EXIT
cd "$dir"
mkdir logs
printf '12345678\n' > report.txt
printf 'hi\n' > notes.txt
: > .env
ls -1
echo '-- with hidden entries'
ls -1A
echo '-- largest first'
ls -1S -- *.txt
echo '-- the directory itself'
ls -d -- logs
)
Expected output#
logs
notes.txt
report.txt
-- with hidden entries
.env
logs
notes.txt
report.txt
-- largest first
report.txt
notes.txt
-- the directory itself
logs
Common mistakes#
Do not parse ls output in scripts, because file names can contain spaces and newlines. Loop over a glob such as ./*.txt, or pass find -print0 results to xargs -0. To see the permissions of a directory itself, use ls -ld; plain ls -l describes its contents.
Version applicability#
This entry describes GNU coreutils ls. BSD and macOS ls share the options used here but reject some GNU options, such as --time-style.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0