11 useful commands for MacOS and Linux

More often than not, some Linux advocates will tell you that the terminal and the command line are the one and only trewe way to get things done with your computer. As a person who lives in 2017 rather than in 1987, I’m inclined to call bullshit on that. I do realize, however, that terminal commands can be useful for performing some (although not all) operations. Here’s a few that I find particularly useful.


Prints the working directory

The pwd command shows the current directory. Although new terminal window start out in the current user home folder, it’s easy to lose track of your whereabouts when working with the terminal.

Not anymore! With pwd you’ll always know where you are in the directory tree.

pwd supports two options: -L and -P, but chances are you’ll never have to use them. -L shows the current logical path, whereas -P shows the current physical path. The difference is that the first just tells you the current position as it appears to you, whereas the second resolves symbolic links.


Create an alias for a command

The alias command is one that I’ve been envying UNIX users for the longest time. What it does it it creates an alternative, user-defined way to call a command. You may have noticed that MacOS is missing the dir command. In order to enumerate a folder’s content, you’ll have to use ls instead.

However, you can still use alias to call ls by typing dir. All you need to do is type

alias dir=ls

and you’re ready to go! This, however, will only create a temporary alias that will be lost at the next reboot. In order to make the alias permanent, you’ll need to also type the following commands

echo alias dir=ls >> $HOME/.bash_profile
source .bash_profile

This will make your changes permanent for your current user.

Note: Be extra careful to use two greater than signs when using the echo command. Using only one will cause all of your previous aliases to be overwritten and there’s no coming back!


Search for files and folders

find is one of the oldest UNIX utilities used to search for files and folders. We actually already have an article on how to use it, and I strongly suggest you also read that. It may be a bit on the slow side (actually, even slower than Windows’ dir command!), but it works quite well.

Its basic syntax is as follows:
find PATH [options]
Where PATH is the folder in which you want to search and options are modifiers that you can use to change find’s behaviour. Find works recursively, which means that it will try and traverse all subfolders. Using / (the root directory) as the search path will mean that the command will search for files everywhere on your computer, even going as far as searching into network paths.

Here are a few useful options for find:

Option Behaviour
-name Filename Search for a file with a specific name.
-iname filename Same as name, but performs a case-insensitive search.
-xdev Do not search in network paths.
-maxdepth n Traverse n levels of the filesystem. Useful if you don’t want find to search everywhere.
-user username Search for files belonging to a specific user
-group groupname Search for files belonging to a specific group
-type x Search for files of a specific type. Valid types are:

b       block special

c       character special

d       directory

f       regular file

l       symbolic link

p       FIFO

s       socket

-size n Search for files of the n size. You should follow the number n with the unit type, such as M.


Quickly finds files and folders

locate, in a way, is an evolution of find. Instead of traversing the whole filesystem, locate will look up entries in a database. As one may expect, searches are much more quicker than those performed by find.

The basic syntax for locate is as follows:

locate file

The biggest drawback is that this database has to be kept up to date periodically and may not include the latest files added to the system.

However, it’s still possible to update it manually. On Linux, this can be achieved by typing

sudo updatedb

whereas on MacOS, the same effect can be obtained by running

sudo /usr/libexec/locate.updatedb

Of course, it’s still possible to create an alias for that command, so that it resembles that in Linux!


Create a directory

If you need to create a new directory, you can use mkdir. It’s a very simple command to use that will create the folder (or folders) you specified. In its basic form, mkdir has the following syntax:

mkdir foldername

Note: Mkdir interprets spaces as separators for different folders. If you want your folder to include a space, you should enclose its name in quotes.

It supports three options:

-p will create intermediate directories if required. For example, mkdir -p test/t4f will create a folder named test in the current path and one called t4f inside it.

-v will list folders as they’re created.

-m mode will assign the specified permissions to the newly-created folders.


Remove files and folders

Be very wary when using rm, for it won’t move files or folders to the recycle bin, but rather delete them completely, generally with no way to recover them at all! Its basic syntax is as follows:

rm filename

A few useful options include -R (or -r) which will remove a folder and all of its content, -i which will prompt the user for confirmation before removing something, and -P to delete files and overwrite them to ensure they’re not recoverable.


Synchronizes files and folders

rsync is a very powerful tool for copying files and folders. It allows you to specify a whole bunch of options that let you fine-tune how you want to copy stuff, but it can also be useful for simple copy operations.

Its basic syntax is as follows:

rsync -r source/ destination/

Notice the trailing slashes after the directory names: these are to ensure that rsync won’t try and recreate the whole directory structure inside the destination folder, but rather copy it as is.


Change directory

The cd command is used to change the current directory. This can be useful if you need to edit a configuration file but don’t want to type in the whole path to that specific file. The basic syntax of this command is

cd folder_path

where folder_path can either be relative (without a leading slash) or absolute. Paths are relative to the one shown by the pwd command. So, for example, if we’re in folder /home/tech4freelancers and we type

cd articles

the working directory will be changed to /home/tech4freelancers/articles


Change file and folder permissions

chmod is perhaps one of the most important commands in the UNIX world. It allows the user to change permissions (also known as modes) for files and folders. It’s admittedly a bit tricky to master and causes lots of headaches to first-time users.

In order to really understand how chmod works, we need to explain the traditional UNIX security model. Every file and folder in a UNIX system are attached to 9 special bits that are reserved for access control, that is, who can and cannot read from, write to, or execute a file. Six bits control access for the owner, the group the owner belongs to, and the rest of the users (generally known as the world), while the other three are generally used by the operating system when it needs to run a file. You generally won’t need to touch those three.

The two bits for the owner, the group and the world can have the following values or a sum thereof:

  1. execute permission has a value of 4
  2. write permission has a value of 2
  3. read permission has a value of 1
  4. no permission has a value of 0

You can add these values to combine their effect. For example, a file that can be read from and executed, but not written to by the group, will have the correspondent bits set to 4+1=5. Here’s a run down of the various values.

Value Description
0 No access
1 Read only
2 Write only (rarely used)
3 Read and write
4 Execute only
5 Execute and read
6 Write and execute
7 Read, write and execute

The basic syntax for chmod is as follows

chmod mode file

The chmod man page is very verbose and shows a lot more customization options, but even in its basic form, it’s a very useful tool.


Move and rename files and folders

mv is quite self-explanatory: it allows the user to move a specific file. What can be a bit counter-intuitive is that it also allows to rename files. Think about it for a second: if a file is stored in /home/tech4freelancers/a.txt and you move it to /home/tech4freelancers/b.txt, then you did nothing more than renaming it, even if it resides in the same directory.

mv basic syntax is similar to Windows’ MOVE command

mv source destination


Read the manual page for a command

man is short for manual. What this tool does is showing you a detailed explaination of what a command does. It goes without saying that manual pages (or man pages for short) do not deal with specific issues. However, many developers will insert a few examples if a specific command is complicated to use or otherwise tricky.

Its basic syntax is

man command

You can type man man to get information about the man program itself. Unfortunately, you cannot use man man man man man to get a description of a description of a description of a description of the man command. That would’ve been hilarious!

Options and advanced usage

The man command takes a few options. Some of them are:

Option Behaviour
-k (lowercase K) Make man behave like the apropos command (shows a one-line description for man pages that contain the keyword)
-f Make man behave like the whatis command (shows a one-line description for man pages matching the exact keyword)
-K (uppercase K) Search for a string in all man pages (it will take very long!)

Man pages are divided in eight sections, each dealing with a specific facet of the operating system. If you want to specify a particular section, you can do so before the page title.

For example,

man 4 tty

will display the man page for the tty driver, whereas
man tty
man 1 tty
will display the manual page for the tty command.

About Andrea Luciano Damico 137 Articles
Andrea Luciano Damico is a freelance translator from Italy. Among his interests are linguistics, technology, video games, and generally being a chill guy. He runs Let's Translate.it and Tech4Freelancers.net.