Home / Alt manpages / dir(1)

  • dir(1)
  • User command
  • linux

Use dir to Inspect Files Without Missing Hidden Entries

You will finish with a reliable way to inspect a directory, expose hidden entries when needed, read the useful fields in a long listing, and choose sorting and output options deliberately. On this machine, dir is GNU coreutils 9.4. Allow about ten minutes. You need a shell and read access to the directory you want to inspect. The examples are ordinary commands and do not need elevated privileges.

dir is the GNU directory-listing command. It lists the current directory when you give it no path, or lists the files and directories named on the command line. It does not change the directory or its contents. This guide uses the installed command as the authority, because output details can differ between implementations.

1. Check the installed command

Confirm which executable your shell will run and record its version:

$ command -v dir
/usr/bin/dir
$ dir --version | head -n 1
dir (GNU coreutils) 9.4

The exact path can differ. The version matters when you are comparing output from another host, especially if a script relies on newer options. Checkpoint: if dir --version does not report GNU coreutils, read that implementation's manual before copying the options below.

2. List a named directory

Pass a directory path as the final argument. The default view hides names beginning with a dot and sorts the remaining entries alphabetically:

$ dir /path/to/project
README.md  build  src

Replace /path/to/project with a directory you can read. A name such as build is shown as an entry, not opened and listed recursively. To inspect the current directory, omit the path or use .:

$ dir .

The output layout depends on terminal width. With a wide terminal, entries are normally arranged in columns. For output that is easier to read line by line, use -1:

$ dir -1 /path/to/project
README.md
build
src

3. Include hidden entries when the question requires them

Use -a or --all when you need every entry, including the implied . and .. names:

$ dir -la /path/to/project
total 24
drwxr-xr-x 4 user group 4096 Sep 23 10:00 .
drwxr-xr-x 8 user group 4096 Sep 23 09:00 ..
-rw-r--r-- 1 user group  120 Sep 23 10:00 .env.example
-rw-r--r-- 1 user group  850 Sep 23 10:00 README.md
drwxr-xr-x 2 user group 4096 Sep 23 10:00 src

Your owner, group, dates and sizes will differ. If you want hidden files but not the implied directory entries, use -A or --almost-all. This distinction is easy to miss when checking whether a configuration file is present. A leading dot is only a naming convention: hidden entries can still contain credentials or other sensitive data.

To exclude backup-style names ending in ~ from the normal implied-entry matching, use -B. Do not treat this as a cleanup operation. dir only omits matching names from its display; it never deletes them.

4. Read a long listing

Use -l when you need permissions, ownership, size, timestamps and the entry name:

$ dir -l /path/to/project
total 12
-rw-r--r-- 1 user group  850 Sep 23 10:00 README.md
drwxr-xr-x 2 user group 4096 Sep 23 10:00 src
lrwxrwxrwx 1 user group    8 Sep 23 10:01 current -> releases

For a regular file, the first field describes the type and permission bits. The link-count, owner, group, byte size, timestamp and name follow it. A leading d identifies a directory and l identifies a symbolic link. The displayed timestamp is normally the modification time. It is not proof that a file was created then.

Use -h with -l for sizes such as 4K and 2G instead of raw byte counts:

$ dir -lh /path/to/project

These human-readable values are convenient for a person but less suitable for parsing. For stable machine processing, prefer an explicit format designed for that job and do not scrape aligned columns.

5. Make file types visible

Use -F or --classify to append a marker to each name. A directory receives /, a symbolic link receives @, and executable files can receive *:

$ dir -1F /path/to/project
README.md
build/
current@

If you only want the directory marker and other file-type indicators, use -p or --indicator-style=slash. The long option is clearer in scripts. These suffixes are display indicators, not part of the actual names, so do not pass the printed build/ back to another command without understanding the added character.

6. Change the ordering consciously

With no sorting option, dir sorts alphabetically by name. Use -S for largest first, -t for newest modification time first, or -r to reverse the selected order:

$ dir -lhS /path/to/project
$ dir -lt /path/to/project
$ dir -ltr /path/to/project

Use --sort=extension for extension order, --sort=version for natural ordering of numbers in names, and -U or --sort=none to leave entries in directory order. Directory order is filesystem-dependent and is not a promise of creation order. If the timestamp you need is not modification time, choose it explicitly with --time=access, --time=ctime or another value documented by the installed manual.

To put directories before files while retaining a useful sort, add --group-directories-first. Combining it with --sort=none disables that grouping according to the command's help text, so check the options together rather than assuming they compose.

7. Inspect the directory entry itself

There is a common trap when checking a directory's permissions. dir -l /path/to/project lists the directory's contents. Add -d or --directory to list the directory entry itself:

$ dir -ld /path/to/project
drwxr-xr-x 4 user group 4096 Sep 23 10:00 /path/to/project

This is the useful form for checking whether the directory itself is writable or owned by the expected account. It does not recursively inspect permissions below that point. If you need a complete permissions audit, use a purpose-built audit rather than assuming one dir line represents the whole tree.

8. Handle names and scripts safely

Names can contain spaces, tabs, newlines and characters that make a terminal display confusing. Use -b to print C-style escapes for non-graphic characters, or -q to show question marks instead. Use -Q when double-quoted names make a human review clearer. These options change presentation, not the underlying names.

Do not parse ordinary column or long-list output with a loop. Names can contain whitespace, and terminal quoting rules do not make the output a safe interchange format. For a shell task, use shell pathname expansion or a command that emits a NUL-delimited result. If you need NUL terminators from this GNU command, --zero ends each output line with NUL, but test the consumer as well as the producer before using it in a script.

Redirecting output is normally safe, but shell redirection can overwrite an existing file before dir runs. If you need a report, choose a new destination or use a checked temporary file and rename it only after the command succeeds. Do not use sudo merely to list a directory. If access is denied, first check the path and the account's permissions; elevated access can expose secrets and does not fix an incorrect path.

9. Check failures instead of trusting partial output

Check the exit status when the listing is part of a larger command:

$ dir -l /path/to/project
$ status=$?
$ printf 'dir exit status: %s\n' "$status"
dir exit status: 0

GNU dir returns status 0 when it is successful, status 1 for minor problems such as being unable to access a subdirectory, and status 2 for serious trouble such as an unusable command-line argument. A partial listing with a non-zero status is not a complete inventory. Fix the path or permission problem and run the check again.

If the command reports that a path does not exist, inspect the exact spelling without changing anything:

$ test -e /path/to/project && echo 'path exists' || echo 'path is missing'
$ dir -ld /path/to/project

Keep the path quoted when it comes from a variable or contains whitespace. A directory listing is a view of the current state, not a lock on it: files can be created, renamed or removed by another process immediately afterwards.

Done means

  • You confirmed that the installed command is GNU coreutils 9.4.
  • You know that the default view hides dotfiles and sorts by name.
  • You can choose between -a, -A and -d for the question being asked.
  • You can read a long listing without mistaking modification time for creation time.
  • You use explicit sorting and file-type indicators when they make the review clearer.
  • You check the exit status and do not parse human-oriented columns as safe file data.