List Files and Directories in Mac Terminal
In this tutorial, we’ll explore the commands necessary to view your files and directories, as well as more advanced options to filter, sort, and display your data in various formats.
Each example in this tutorial can be tried in your own Mac Terminal.
Introduction to the Terminal
The Terminal is a command-line interface (CLI) that allows you to interact with your Mac’s operating system using text-based commands. While you can perform most tasks using the Finder, the Terminal offers a faster and more flexible way to manage files and directories. The Terminal can be found by navigating to Applications > Utilities > Terminal.
Basic Commands for Listing Files
The most basic command for listing files and directories in the Mac Terminal is the ls
command. By default, ls
will list the contents of your current directory (also known as the working directory).
ls
When you run this command, it will display a list of all files and directories in your current location.
If you want to see hidden files (those that start with a dot), you can use the -a
option to reveal all files.
ls -a
This will display hidden files in the directory as well, which are typically used for system or configuration purposes.
Using Absolute and Relative Paths
In addition to listing the contents of your current directory, you can list the contents of other directories by specifying their path. Paths can be absolute (starting from the root of your file system) or relative (starting from your current directory).
For example, to list the contents of your Documents
folder, you would use the following command:
ls /absolute/path/to/directory
Or, if you are already in the /absolute/path/to/
directory, you can use a relative path:
ls Documents
Viewing Detailed Information
If you want to view more detailed information about the files and directories, such as their size, permissions, and the last modification date, you can use the -l
option. This will display a detailed list (also known as a long format listing).
ls -l
This output includes several columns of information, including:
- File permissions (e.g.,
-rw-r--r--
) - Number of links
- Owner of the file
- Group that owns the file
- Size of the file
- Date and time of the last modification
- File or directory name
Sorting Files by Time, Size, or Name
By default, the ls
command lists files and directories alphabetically. However, you can sort the output by different criteria. To sort by modification time, for instance, use the -t
option:
ls -lt
This will display the files and directories sorted by their modification time, with the most recently modified files at the top.
You can also sort by file size using the -S
option:
ls -lS
To reverse the order of the listing, simply add the -r
option to any of the above commands. For example, to list files in reverse order of modification time:
ls -ltr
Combining Options for a Custom View
You can combine different options with ls
to create a customized view. For example, if you want to view hidden files, sort by modification time, and display detailed information all at once, you can use:
ls -lat
This will give you a comprehensive listing of your files with plenty of detail, making it easier to navigate your directories.
Displaying Human-Readable File Sizes
When using ls -l
, the size of each file is displayed in bytes. For larger files, this can be hard to interpret at a glance. To make file sizes more readable, you can add the -h
(human-readable) option, which displays sizes in kilobytes (K), megabytes (M), gigabytes (G), etc.
ls -lh
This output will show file sizes in a format that is easier to comprehend:
This is particularly useful when dealing with a variety of file sizes, as it allows you to quickly identify large files that might be taking up significant disk space.
Using Color to Differentiate File Types
The ls
command can also use colors to distinguish between different types of files, such as directories, executables, and regular files. By default, colors may already be enabled in your Terminal, but if they aren’t, you can enable them using the --color
option:
ls --color
This will apply color coding to your listing, where directories may appear in blue, executable files in green, and symbolic links in cyan. This option makes it easier to visually scan through directories and spot different file types at a glance.