In Windows and Unix-based systems, the options we can use when listing directory contents, and the variety of uses for such a listing, are simply too numerous for us to document here. (In any event, running dir /?
on Windows or ls --help
on Unix-based systems will give you a taste.) Fortunately, what we’re interested in when getting a directory listing usually doesn’t change much: We simply want a list of files, directories, or both, optionally filtered to include only those matching some wildcard pattern, optionally including/excluding details such as size, modification dates, etc., and with a few different sorting options So we’ll focus on just a basic set of options here.
Commands
Windows Command Prompt
Syntax
dir [path][filename] [options]
- If
path
is not specified, the current working directory is assumed. - If
path
is specified, it is interpreted in accordance with “Specifying file and directory paths”. filename
can contain one or more of the wildcard characters,?
and*
. The former is used as a wildcard substitute for a single character, while the latter is a wildcard substitute for any number of characters.
Common options
/AH
,/A-H
- Display only hidden files and directories, or exclude hidden files and directories, respectively. (The default behavior is to display all entries, hidden or otherwise.)
/AD
,/A-D
- Display only directories or only files, respectively. (The default behavior is to display both directories and files.)
/B
- Include file or directory names only, without additional details.
/S
- Generate a recursive listing of subdirectory contents. (When
/B
and/S
are used together, the resulting names include the full paths.) /OG
,/O-G
- Group directories at the start or end of the listing, respectively. (By default, files and directories are listed together.)
/ON
,/O-N
- Order by name in ascending (default) or descending order, respectively.
/OE
,O-E
- Order by file extension in ascending or descending order, respectively.
/OD
,O-D
- Order by date in ascending or descending order, respectively.
Examples
dir
List the contents of the current working directory.
dir ..\*.java /O-D
List the files with names ending in .java
, located in the parent directory of the current working directory, ordered by modification date, with the most recently modified listed first.
Bash shell (OS X & Ubuntu Terminal, Git Bash, etc.)
Syntax
ls [options] [path][filename]
- If
path
is not specified, the current working directory is assumed. - If
path
is specified, it is interpreted in accordance with “Specifying file and directory paths”. filename
can contain one or more of the wildcard characters,?
and*
. The former is used as a wildcard substitute for a single character, while the latter is a wildcard substitute for any number of characters. (However, neither wildcard will match an initial period in a file or directory name.)
Common options
Note that single-letter options can be combined, e.g. -al
is equivalent to -a -l
.
-a
- Include files and directories that are normally hidden (i.e. with names beginning with a period).
-l
- Use the long listing format (one line per file or directory), with file details.
-1
- Use a long listing format (one line per file or directory), without file details.
-d
- List directories matching wildcard pattern, but not their contents.
-R
- List directories recursively.
--group-directories-first
- Group directories before files in the list.
-X
- Sort by entry extension.
-r
- Reverse sort order.
Examples
ls -al
List the contents of the current working directory, including hidden files and directories, using long list format with details.
ls -ald ~/bootcamp/docs/*
List all subdirectories in the docs
subdirectory of the bootcamp
subdirectory of the current user’s home directory, using the long list format.
Tasks
-
Use the
cd
command to change to your home directory. -
List the directory contents with all files and directories (including hidden entries).
-
List the directory contents, excluding hidden entries. Note any differences between the two listings.