Basic Terminal Commands: Navigating the macOS Directory Structure

Learning the under-the-hood operations of a Mac can intimidate many users, but it’s a pretty simple thing. After all, macOS is based on Unix; and if you’ve mastered the basics of Terminal operations, you can use them not only on Macs but also on Linux as well.

One of the basic operations you should learn first when using the Terminal is how to navigate around the folder and file structure to access files. That’s exactly what we’ll do in this article by using the cd, ls and pwd commands.

Using the Terminal

Accessing the Terminal — a built-in app in macOS that gives users access to the command line underpinnings of UNIX — can be done simply by performing these steps:

  1. Open the Finder and press Command + Shift + U to open the Utilities folder. Or, navigate to Applications | Utilities.
  2. Open the Terminal app.

That’s it. Once the Terminal app is open, you can begin typing commands. We’ll go over the commands you’ll need to use to navigate through the directory structure on macOS and UNIX-based systems below.

SEE: Users can also disable the creation of .DS_Store files from the Terminal app.

The cd command

The cd command (or change directory command) is used to navigate to the directory or folder the user wishes to access (Figure A). For example, if from the command prompt the operator wishes to change the working directory to the Applications folder, the user should enter the following command:

cd /Applications

Figure A: The cd command lets you easily move between directories in the Terminal. Image: Cory Bohon/TechRepublic

Entering cd followed by the absolute path to the file or directory in question enables operators to navigate to specific locations quickly when using the command line. Instead of navigating to the Applications folder, if the operator instead intended to change the working directory to the Applications folder’s Utilities subfolder, the user would enter:

cd /Applications/Utilities

The ls command

Once the correct file location is established, operators can begin manipulating files within the working directory. The ls command (or list command), simply typed ls into the Terminal, lists the contents of the working directory (Figure B).

The ls command allows users to easily list files and folders in the current directory they’re browsing in the Terminal.
Figure B: The ls command allows users to easily list files and folders in the current directory they’re browsing in the Terminal. Image: Cory Bohon/TechRepublic

Commands can be entered with options (or additional information) that make the commands exponentially more powerful. For example, the ls command, by default, hides within its results certain files, such as those with file names that possess a period (or dot) as the first character. Entering ls -a instead of just ls generates instructions dictating all files within a directory be revealed within the command’s results.

Numerous additional ls options are available. Whereas the -a option instructs the command to reveal all files, the -l option specifies that file attributes — including permissions, file size and file date — be listed within the displayed results as well.

The pwd command

When working within different directories and, later, when additional Terminal skills are obtained and operators begin performing additional move, copy and even file attribute or permissions changes, it becomes easier to forget in which directory one is currently working. The pwd (or print working directory) command is a quick shortcut command an operator can enter to reveal the current working directory (Figure C).

The pwd command will print the current directory into the Terminal.
Figure C: Lost and don’t know what directory you’re browsing? The pwd command will print the current directory into the Terminal. Image: Cory Bohon/TechRepublic

Understanding the macOS directory structure

Using the Terminal can be a great way to navigate files stored on a Mac but not without first understanding the macOS directory structure. The macOS file system stores files within folders, also known as directories. The top, uppermost folder is known as the root directory. Folders located within or beneath the root directory are known as subfolders or subdirectories, two different ways of saying the same thing. That may seem obvious to more advanced users and administrators, but even some more seasoned users may not be aware an absolute directory path exists for each file.

SEE: Learn the ins and outs of searching for files on your Mac.

To navigate to a specific file’s path, which can prove critical when attempting to administer a Mac using the Terminal, first, the user must understand the directory structure. The root directory is often referred to as /. Beneath the root, on most Macs by default, are several additional folders, including Applications, which stores programs, and Users, which stores home folder information for each user possessing an account on the machine.

The cd, ls and pwd offer a straightforward way to navigate these folders, allowing users to manage files quickly and easily.

Source link

Leave a Comment