Monday, February 18, 2019

A Beginner’s Guide to Using the Mac Terminal

guide-mac-terminal-

For most of your everyday Mac tasks, a soft and friendly GUI is both an asset and a comfort. Sometimes, though, Finder is a clunky middleman.

There are faster ways to find out where that pesky 5GB file is hiding, or the path of every file related to that app you thought you deleted. For these jobs and others, the command line is your new best friend.

What Is Terminal?

Terminal is a utility that allows you to interact with your Mac through the command line. Linux operating systems include similar tools, since both Linux and macOS are Unix-like OSes. The command line interface (CLI), or the language that you type into Terminal to interact with your Mac, is called bash. Everything we discuss below is a bash command.

Before you start using Terminal, you can customize it to your own personal preference. If you prefer, it’s even possible to download a third-party Terminal alternative for a customized look and feel.

General Mac Command Line Tips

First, let’s look at some basic Terminal facts you should know.

General Syntax

A bash command typically follows this pattern:

[Command] [Options] [Input or Path to File or Directory]

For example, in:

ls -la /Applications

ls is the command, -la is a compound of two individual options (-l and -a), and /Applications is the path to list.

The Path

Understanding paths will help you understand how macOS actually sees your files. Essentially, the path of a file is the Russian dolls’ nest of folders in which it’s contained, followed by the name of the file itself.

For example, on a Mac, the path of a file called My Secrets that lives on user John Doe’s Desktop is /Users/jdoe/Desktop/"My Secrets".

White Space

You must escape white space for the Terminal to process it properly. When bash sees a space, it interprets it as the end of a command. So if you have a folder with spaces in its name, like Path Test, and you try to list its contents with ls /Applications/Path Test, you’ll get this:

Invalid Path Causes Bash Command Failure

What’s going on here? Well, bash thinks that you called ls on /Applications/Path. When it couldn’t find that file, it stopped.

If you want bash to recognize the full name of your folder, you can either wrap the name in quotes or use a backslash, like so:

  • ls /Applications/"Path Test" or
  • ls /Applications/Path\ Test

Sudo

Many of the commands below require administrator-level access. If you’re not currently signed into administrator account, but you know the administrator’s password, you can place sudo (which stands for “single user do”) in front of the command to temporarily give it administrator-level privileges.

Terminal Commands to Improve Your Workflow

Now that you know the basics, let’s take a look at some extremely handy commands. Note that you can pull up full information on these commands, including all their options and examples, by typing man <command name> into the Terminal.

find

  • Replaces: Spotlight
  • Why it’s better: It’s faster and searches system folders that Spotlight excludes, or has trouble indexing.

Spotlight tends to skip macOS system files unless you tell it not to, and even then can have trouble indexing them. Conversely, the bash find command can search for anything, in any place, and will output the full path of what you’re looking for.

The syntax of find consists of four parts. In order, they are:

  1. find
  2. the path of the directory you want to search (/Applications below)
  3. options (the below example has -name, which means that find will search for files that match that name)
  4. the string to search (the below example has Google Chrome)

You should know that find uses regex (also called regular expressions). A full explanation of this topic is outside the scope of this article (or anything short of a textbook). However, the below example introduces a vital concept in regex, which is the asterisk (*), or wildcard character.

Putting it at the beginning and end of the search string means that find will output results that have characters before and after the search term. In this case, Google Chrome will bring up Google Chrome.app.

It all comes together to look like this:

An Example of the bash find Command

du

  • Replaces: Cmd + I to show info.
  • Why it’s better: It can show you multiple folders at once, and typically takes less time to load.

du stands for “disk usage,” and can quickly tell you the size of a file or folder, or even a list of files within a folder.

The best options for du are:

  • -d (depth): When followed by a number, tells find to limit its search to a -d level of depth in the directory where it runs.
    • For example, if you run du -d 1 /Applications, it will only show you the total size of the folders and files in your Applications folder, not the sizes of subfolders within those folders.
  • -h (human readable): This will show you the size of your files in K, M, or G, which stands for kilo, mega, or gigabytes.

Take a look at du in action:

Bash Command du in Action

mv

  • Replaces: Point-and-click moving of folders and files.
  • Why it’s better: It’s faster and requires no navigation.

You can quickly move a file or folder into another folder using mv. It works by simply changing the name of the path.

The syntax is mv <old file path> <new file path>.

For example, mv /Users/jdoe/Documents/file1 /Users/jdoe/Desktop/file1 will move file1 from jdoe’s Documents to his Desktop.

ls

  • Replaces: Cmd + i to show info.
  • Why it’s better: It’s faster, can show info on multiple files at once, and is highly customizable.

ls is an incredibly powerful command for showing you exactly what’s in your folders. It also reveals who’s allowed to see them, if you have any hidden files or folders, and much more.

The best options for ls are:

  • -l (long): Shows the permissions for each file in the folder, the most recent modification time, the file owner, and filename.
  • -a (all): Shows you all the files in a folder, including the hidden files (great for showing the user library in macOS, which is hidden by default).

Here’s what the output looks like:

ls -la In Action

mkdir

  • Replaces: Finder > File > New Folder
  • Why it’s better: It’s faster, and you can set the name right in the command instead of double-clicking the new folder.

Create new folders in an instant with this command.

Example: mkdir /Users/jdoe/Desktop/cool_stuff

rm

  • Replaces: Moving files to the Trash and emptying it.
  • Why it’s better: It’s faster, and good for deleting pesky files that the Trash won’t get rid of.

This command will delete, immediately and without prejudice, any file you put in its path. Obviously, use it with extreme caution. Unlike clicking Empty Trash, rm will not ask if you’re sure. It assumes you know what you’re doing.

One thing to note about rm is that by default, it will only delete files, not folders. To delete folders, you must use the -R option, which stands for recursive.

Example: rm -R /Users/jdoe/Desktop/cool_stuff

Master Your Mac With the Terminal

Now you know some essential Terminal commands and can start integrating them into your daily Mac workflow. Once you get comfortable using bash, you can go beyond simply replacing your everyday tasks and start exploring powers that only the command line can offer.

Start off by installing Homebrew, the best package manager for macOS. It allows you to install new programming languages, software repositories, and more.

Read the full article: A Beginner’s Guide to Using the Mac Terminal



from MakeUseOf http://bit.ly/2Ii2MjD

No comments:

Post a Comment