How to use Terminal to be productive (for mac users)

Even if you’re not a programmer, if you use a Mac (or Linux), you’ll save so much time with Terminal when performing repetitive tasks. (If you’re on a PC, check out cmd or PowerShell.) Spending just 30 minutes on how to use it will be one of the best productivity investments you’ll ever make if you manage lots of files or if you perform tasks related to web development.

If you’re already familiar with Terminal, skip to the last section where I include cool and useful commands I wasn’t aware of until recently.

What is Terminal?

Terminal is a built-in app that allows you to perform tasks on your Mac through the command line. Bash refers to the shell that processes the commands.

It can seem daunting at first, but once you get used to just a few basic commands, you’ll get the hang of it. Commands carried out via Terminal (which is a command line interface or CLI) are processed faster than those executed from a Graphical User Interface (GUI), the point, click, and drag interface most of us are used to on our Macs. 

How to open Terminal

I use Spotlight search on my Mac, and type “terminal.” To open it from a folder, go to Applications and then Utilities.

Basic Terminal commands

To get started, let’s start with learning how to navigate through your folders (= directories). Once you open Terminal, type the following:

pwd

(= print working directory) Use this command to confirm your exact location (current directory/folder). When you first open Terminal, you should be in the home directory, Users/[your username]). To navigate to the Documents folder, type

cd Documents

(cd = change directory) You should now see an indication that you are in the Documents folder. Note that typing just Documents after cd takes you to that folder if it exists within the current folder. The Documents folder is located within the home directory, so it’ll work. We’ll return to this concept in just a second. To view all files in the current folder, type

ls

(= list files) Choose a folder that exists within your Documents folder that you want to navigate to. In my case, I want to open up the Images folder. Can you guess how?

cd Images

Now if you want to move back up to Documents, and type cd Documents, it won’t work (unless you have another folder called Documents within the Images folder). This is because cd followed by the folder name takes you to that directory within your current location. You have a few options to access the Documents folder from here.

cd ~/Documents

This is the full path (= address) for the Documents directory (~ takes you to the home directory). If you already have the Documents folder handy in your GUI, you can drag it over to get the full path as well.

Instead, if you want to move up one directory, type

cd ..

You should now be back in the Documents folder.

Now let’s create a new folder (= make directory) within the Documents folder called yolo.

mkdir yolo

Easy! Let’s create a .txt file called lmao by using the touch command.

touch lmao.txt

To confirm that the file was created, view all files from the yolo folder. Do you remember how?

ls

Let’s move this file from the Documents directory to the yolo directory by using the mv command (= move):

mv lmao.txt yolo

Note that typing just the file (lmao.txt) and the folder (yolo) works because both exist within the current location (Documents folder). If that weren’t the case, you’d have to include the full path like so

mv ~/Documents/yolo/lmao.txt ~/Documents/lmao.txt

To copy a file, use the cp command and the same syntax

cp [source] [destination directory]

Finally, to remove files, use the rm command:

rm lmao.txt

This command does not put the files into the trash bin nor does it ask you if you’re sure about deleting the files. It deletes them forever. Be 1000% certain you know what you’re doing when deleting from Terminal. To remove folders, use -r (recursive):

rm -r yolo

Again, working with Terminal is so much faster than using point and click GUI commands. You’ll notice the difference when dealing with many or large files, such as those from a back up drive.

Other handy bash commands

To perform any task that requires admin access, start the command with sudo (= single user do) followed by single space, then the rest of the command. Terminal may ask for the password you use to log into your system.

sudo [command]

Repeat the last command

!!

To cancel the previous command, use

CTRL C

To autocomplete, hit the tab key. For example, if you want to move to the Documents folder, type do and then hit tab to see all the folders that start with those two letters.

To hide a file or folder

chflags hidden [file path]

View all files including hidden ones

ls -a

To compress files into a zip folder

zip

To extract files from a zip folder

unzip

Cool and useful Terminal commands

Keep your computer from sleeping or dimming

caffeinate

Cancel caffeinate mode

CTRL C

Shut down your computer right away

sudo shutdown -h now

Shut down your computer in 30 minutes

sudo shutdown -h +30

To find a file, use the find command, which is more powerful, quicker, and customizable than Spotlight search. This command can find files that Spotlight cannot, whether it’s simply due to the limitations of its functionality or its exclusions of certain files (e.g., systems folders).

find [path of scope] [options] [search string]

To find any file within your user home folder that has the string “img” in the file name:

find ~/ -name img

To find any png file within the Documents folder:

find ~/Documents -iname "*.png"

Check to see if you can connect to a website, and get details of your connectivity.

ping [URL]

Get the whois information for a website

whois [URL]

Change the format of your screenshots from PNG to JPG to save storage space

defaults write com.apple.screencapture type jpg

Launch a local server for a simple website. First move to the directory that has the website files (use cd), then type

python -m SimpleHTTPServer

Terminal will tell you the port number for your server. Mine says Serving HTTP on 0.0.0.0 port 8000. You can then open up a web browser, and type localhost:8000 in the address bar to access the website. To stop running the server: CTRL C.

Sometimes, when downloading files from the Internet, the browser gets in the way due to restrictions or because it tries to open up the files right away. To download files to the current folder, type

curl -O [URL where the file resides]

To download multiple files

curl -O [URL where file 1 resides] -O [URL where file 2 resides]

In my previous post, Apps for Productivity – how to automate and create in batches, I showed how to get Automator to read text from files. A much quicker way would be to use Terminal:

say -f [file path]

If you want it to read something you type into Terminal right away:

say "aji is the most beautiful pug in the whole wide world"

[Advanced] Automate scripts by scheduling them (especially useful for scheduling backups)

crontab -e

[minute] [hour] [day of month] [month] [day of week] [script path]

Minute refers to the minute of the time you want to run the script, number between 0-59

Hour refers to the hour in 24 hour format, number between 0-23

Day of month, 1-31

Month, 1-12

Day of the week, 0-6 (Sunday is 0, Saturday is 6)

A couple of examples:

Run backup.sh every weekday at 2 AM

0 2 * * 1-5 ~/mybackupfolder/backup.sh

Run backup.sh everyday at 2:30 PM

30 14 * * * ~/mybackupfolder/backup.sh

To save the job, press CTRL O

To view contents from a file via Terminal, perhaps because it’s corrupt or giving you some other error when double clicking on it, type

cat [file path]

View what cookies your computer downloads from a website by saving them to a .txt file, then use cat to view the file:

curl --cookie-jar [file name].txt [URL of webpage] -O

cat [path of file you just saved]

Finally, last but not least, there are hidden games available for when you need to take a break from work! Type

Emacs

Then follow instructions to activate menubar (fn key + F10). Go to tools (hit t key), and then games (g key). From there, choose a game you want to play. I tried Adventure, a text-based game where I type in instructions for my character based on clues given by the narrator. I didn’t get very far. Try it out for yourself.

If you liked this article, I think you’ll also like this one here: Apps for productivity- how to automate tasks