
How to Use the Command Line for Apple macOS and Linux
You might be like me – I used computers for twenty years without ever touching a command prompt. I didn’t know anything about it, and it seemed scary and overwhelming. I thought it was something only really advanced users knew anything about. When I inevitably encountered a situation where I had to gain some basic command line knowledge, I discovered how useful and easy it is.
Learning to use the command line will open up endless possibilities for you – it is undoubtedly essential in web development and programming, but even regular users doing everyday tasks will benefit. Follow along in this tutorial and you’ll see how simple it is to use, and how powerful it can be.
If you’ve never used the command line, this article will be extremely helpful to you. If you have a basic or intermediate knowledge of the command line, you may learn some new tips and tricks.
Apple macOS and most Linux servers use almost all the exact same commands, so this tutorial applies to both. Even if you use a Windows PC, this will be useful to learn as your websites are likely hosted on a Linux server.
Prerequisites
- There are no prerequisites to this article – as long as you have basic computer literacy you’ll be able to follow along
Goals
- Learn what the command line interface is and how it relates to your computer
- Learn some basic terminology related to the command line
- Learn the most common, useful commands
What we’ll learn
- Show current directory and contents of directory
- Moving between directories
- Creating files and directories
- Write text to a file
- View contents of a file
- Deleting files and directories
- Copying and pasting files and directories
- Moving/cutting files and directories
- Running multiple commands
- Changing permissions
- Run as administrator
- Connecting to another computer or server
- A few more useful commands
- Using a basic text editor
What is the command line?
I promise this is the most important thing to understand, and it completely blew my mind when I finally understood it.
Windows, macOS – whatever operating system you’re using – is simply a visual representation of your computer. This is known as a Graphical User Interface (GUI). Take away the file explorer, the desktop, the icons, and all the other graphics, and you’re left with the command line. Instead of dragging and dropping, pointing and clicking, you’re typing. That’s the only difference.
I really want to reiterate this. Your entire computer can be accessed through the command line. You can do everything through the command line. It’s the same computer you already know and love.
On a Mac, if I open Finder, this is my home folder. It’s called taniarascia
.
I can access the same exact files through a web browser. If I open Google Chrome and I type in /Users/taniarascia
, I’ll be in the same place.
The same is true of the command line interface. I’m going to open Terminal.app by opening Spotlight search (command + spacebar) and type in Terminal.
My terminal background is dark. Yours might be white or blue or different depending on what you’re using. This is simply a personal preference, which we can learn to change later.
Just as I was “in” the taniarascia
folder in Google Chrome and Finder, I’m currently “in” the same folder via the command prompt/terminal. I’m going to prove this by typing pwd
into the terminal, then pressing enter. pwd
stands for Print Working Directory, and will show me exactly “where I am” at any point.
Here’s what I type.
pwd
And here’s what is written (printed) to the screen.
NolBook:~ taniarascia$ pwd /Users/taniarascia
It wrote /Users/taniarascia
as my current working directory, where I “am”. But how do I know what’s there? How do I interact with any of those files and directories? I’m going to use the ls
command, which stands for List Directory Contents
ls
NolBook:~ taniarascia$ ls Desktop Documents Library Music Private Sites Dev Downloads Movies Pictures Public Songs
And now I see exactly what I see in Finder and Chrome/a web browser!
Now you should understand that you’re accessing the same files and folders from the command line as you would from any program on the computer. If it doesn’t quite make sense yet, just follow along and I promise it will very soon. If you think that’s incredibly simple and I spent way too much time explaining it, then you’re probably a little brighter than I am.
In programming,
Understanding the syntax
When I open terminal, I see this.
NolBook:~ taniarascia$
You’ll probably see something else, but the ~
and $
will remain. Here’s what’s going on, which you can look back on for reference:
Computer Name:Directory UsernameREADY
- Computer Name (NolBook) – That’s just the name I gave my computer.
- Directory (
~
) – Directly next to the computer name is the current directory you’re working in.~
stands for home directory, which is mytaniarascia
folder. - Username (taniarascia) – This might be slightly confusing because my home folder AND username are both taniarascia, but this is specifically referring to the computer user.
- READY (
$
) – A dollar sign signifies that the prompt is ready to accept your command. You do not type the$
, it’s just there. On a Windows computer, this is represented by a>
symbol.
A terminal or command prompt is a program (command line interface) that runs a shell, which interprets the commands.
Commands
We’re going to learn how to do a lot of the regular things you do on a computer with a mouse or keyboard shortcuts. We’re going to move between directories, create files and folders, delete them, move them, copy and paste them, and edit files. You can also press clear
at any point to wipe all the history and have a clean screen.
So far, we’ve learned three things.
Command | Meaning | Description |
---|---|---|
pwd |
Print Working Directory | find out where you are |
ls |
List Directory Contents | see what files and directories are in your current location |
clear |
Clear | clear the terminal screen |
Always remember to type pwd
before writing any commands to make sure you know where you are.
By default, if you quit Terminal, you will end up back in your home directory.
Moving between directories
Right now, I’m in my home folder. If I want to move somewhere else, I will use the cd
command – Change Directory. I’m going to move to the Music folder, then check my location. Type these commands, and press enter after each one.
cd music
pwd
ls
Here is the output.
NolBook:~ taniarascia$ cd music NolBook:music taniarascia$ pwd /Users/taniarascia/music NolBook:music taniarascia$ ls Audio Music Apps GarageBand iTunesFirst, I moved to the Music folder. The terminal will understand a directory regardless of case, so I can write music or Music. As you can see, it says
NolBook:music
instead ofNolBook:~
, so I know I’m in a different directory now. I printed out my current location to make sure, then listed the contents.That’s great, but I don’t really want to do anything in the Music folder. How do I go back? In the terminal, one dot (
.
) represents the current directory, and two dots (..
) represents one directory backwards, or closer to the root.I want to go back one directory, back into my home folder.
cd ..
NolBook:~ taniarascia$By typing
cd ..
I’ve told the shell to take me back one directory, and now I’m back in home/~
. Right now would be a good time to practice moving between directories.../..
will take you back two directories, and so on.Spaces in directory and file names
If you try to move into a directory that has a space, you may encounter an issue. For example, in my Music folder, there was a directory called Audio Music Apps. However, if I try to simply type that..
cd Audio Music Apps
-bash: cd: Audio: No such file or directoryThe shell thinks I’m trying to move into Audio instead of Audio Music Apps because it does not recognize the space. There are two ways to remedy this.
Using Quotations
Wrap any file in double quotes to preserve the spaces.
cd "Audio Music Apps"
Escaping
Type a backslash
\
character before each space. If you press tab, the Terminal will do this for you! Simply typecd A
and press tab and the shell will automatically assume what you want to type.cd Audio\ Music\ Apps/
Command Meaning Description cd
Change Directory move between directories You can end a command at any point by pressing control + C
Creating files and directories
You can create files and folders from the command line.
Create directory
Let’s create a folder for practicing named Shell with the
mkdir
(Make Directory) command.mkdir Shell
Congratulations, you created a directory! If I type
ls
, I’ll see my newly created directory in the home folder. I can also see this through Finder.
Now you can move into the Shell directory by inputting
cd shell
.NolBook:~ taniarascia$ cd shell NolBook:shell taniarascia$ pwd /Users/taniarascia/shellCreate file
Now let’s make a file. You can do this with the
touch
command. I imagine it as Merlin tapping a wand and creating something out of thin air.touch test.html
I’m not very unique with my example names, so I just called it test.html. When you input this code, it won’t output anything to signify that the command was successful. You can
ls
to see it, or check in Finder that you have created a valid .html file.You can create any sort of file, but it likely only makes sense to create text based files through terminal.
You can also create multiple files at the same time.
touch one.txt two.txt three.txt
Write text to a file
We used
touch
to create an empty file, but we can even create a file on the fly with some content usingecho
.echo "Hello World" > hello.txt
Now I have a plain text file called hello.txt that contains the contents
Hello World
.View contents of a file
I can check this by opening it from Finder, but I can also see the contents through Terminal with the
cat
command.cat hello.txt
NolBook:shell taniarascia$ cat hello.txt Hello WorldAt this point, I would recommend creating some more files and directories and moving between them to get more familiarized with the commands.
These commands –
touch
,cat
, andecho
– can do much more than what I’ve shown in these quick examples.
Command Meaning Description mkdir
Make Directory create a new directory touch
Touch create a new file cat
Concatenate view the contents of a file echo "x" >
Echo quickly print text to a file Deleting files and directories
Now hopefully you’ve make a big mess of files and directories in your testing folder, so we can start cleaning it up.
Delete a file
Use the
rm
(Remove) command to remove a file.rm hello.txt
Note that this will permanently delete the file – it won’t send to the Trash/Recycling bin.
The asterisk (
*
) is known as a wildcard in programming. I can choose to delete all the files of a certain filetype with a wildcard. For example, if I saved many .png files as .jpg, I could runrm *.png
to batch delete the whole set of .png files.Delete a directory
Now, let’s say you create a new directory called goodbye with
mkdir goodbye
, and you try to delete it withrm goodbye
. You’ll get this error.NolBook:shell taniarascia$ rm goodbye rm: goodbye: is a directoryNo problem, we’ll just delete it with
rmdir
(Remove Directory).rmdir goodbye
And now it’s gone. If you want to remove a folder that has files in it, you’ll have to run a slightly different command.
rm -r goodbye
Just like with
touch
, we can remove multiple files or folders at the same time.rm one.txt two.txt three.txt
Command Meaning Description rm
Remove remove directory entries rmdir
Remove Directory remove directories Copying files and directories
We can also copy and paste files through the command line with the
cp
(Copy) command. Simply typecp
followed by the source (file you want to copy) and destination (place you want to copy it to).cp source destination
I’m in my Shell folder. Let’s say I make a new directory called websites with
mkdir websites
. Now I can copy mytest.html
from/Users/taniarascia/shell
to/Users/taniarascia/shell/websites
.cp test.html websites
This is the same as copying and pasting (command + C).
To copy an entire directory, use the
-R
option. I can copy the websites directory and all of it’s contents to a new directory.cp -R websites websites2
Duplicating a file
You can also duplicate a file in the same folder.
cp test.html test2.html
Moving files and directories
You can move files just as you copy them with the
mv
(Move) command.mv source destination
This is similar to cutting and pasting (command + X).
Command Meaning Description cp
Copy copy files cp -R
Copy Recursively copy a directory and all its contents mv
Move move (cut and paste) files and directories Running multiple commands
We can run multiple commands with the double ampersand (
&&
) operator. As long as the first command is successful, the subsequent one will run.touch newfile.txt && mv newfile.txt websites
I just created a new file and moved it to a different directory with one command.
Changing permissions
File permissions aren’t often taken into consideration when you’re a casual Windows or Mac user, but if you’ve ever worked on a web server you likely have experience with permissions. You can change permissions with the
chmod
(Change Mode) command.chmod 644 test.html
I’ve given
644
(read and write by owner) permissions to test.html, a common permission for files.Run as administrator
The term
sudo
stands for Super User Do. You might encounter a situation in which the current user you’re logged in as does not have sufficient permissions to perform a task. You can precede a command withsudo
to run the command as administrator, as long as you have the admin password. When you type the password, it will not show any asterisks to indicate that you’ve typed anything.sudo chmod 775 directory
You can also switch to the administrator user. This is not something you should do often, but it’s useful to know. At times you will need to be logged in as admin to move between restricted directories.
sudo su
Now my terminal looks different – it has a
#
instead of a$
to indicate that I’m logged in as the super user.sh-3.2#Since I don’t really want to be logged in as admin, I’m going to
exit
, which will return me to my regular user account.exit
Connecting to another computer or server
If you’re a web developer or designer, you’ll be familiar with connecting to a web server via FTP or SFTP. You can achieve a secure connection via the command line with
ssh
(Secure SHell).You connect to the server with the same information you’d use to connect via a GUI like FileZilla or Transmit.
ssh [email protected]
Once you’ve entered your password, you are now “inside” the other server. Your console will most likely look something like this:
[[email protected] ~]$All the commands we’ve already learned will work on your web host as well. You can exit the server and return to your own computer with the
exit
command.A few useful commands
Sometimes, when I’m having an existential crisis, I turn to my computer to bring me back to reality.
whoami
NolBook:~ taniarascia$ whoami taniarasciaI often need to check the IP address of a given domain when I’m migrating a website. I can do this with the
dig
(DNS Lookup) command.dig google.com
;; QUESTION SECTION: ;google.com. IN A ;; ANSWER SECTION: google.com. 279 IN A 216.58.192.238If I forget something, I can also check the help guide for the command line with
man
(Manual).man touch
NAME touch -- change file access and modification timesTo exit the manual pages, simply press q.
Using a basic text editor
You may have heard of programs such as Vim or Emacs. These are Terminal-based text editors. Both of these programs have a bit of a learning curve. Most (if not all) Macs and Linux-based computers come with a program installed called Nano, which is a very simple text editor.
I can use the
nano
command to open a file with Nano.First, I’ll create a new file.
touch index.html
Then I’ll edit it with
nano
.nano index.html
Now my Terminal screen will look something like this.
At the top, I can see what file I’m editing (index.html) and at the bottom are the various commands I can do.
^
stands for control. You won’t be able to use your mouse to move around or do anything except copy the contents of the file.So I’ll just type something unique.
Now to save my file, I’ll press control + O (the letter), and enter to confirm. I can exit Nano at this point by typing control + X, and I’ll be back where I started.
Now I can check the contents of index.html with
cat
to make sure it all worked properly.NolBook:shell taniarascia$ cat index.html <html> <head> <title>Hello, World!</title> </head> <body> <h1>Hello, World!</h1> </body> </html>Recap
Here’s a recap of all the commands I went over today.
Command Meaning Description pwd
Print Working Directory find out where you are ls
List Directory Contents see what files and directories are in your current location clear
Clear clear the terminal screen cd
Change Directory move between directories mkdir
Make Directory create a new directory touch
Touch create a new file cat
Concatenate view the contents of a file echo "x" >
Echo quickly print text to a file rm
Remove remove directory entries rmdir
Remove Directory remove directories cp
Copy copy files cp -R
Copy Recursively copy a directory and all its contents mv
Move move (cut and paste) files and directories &&
And run multiple commands whoami
Who Am I display current user id dig
Dig DNS lookup man
Manual open manual (help) pages nano
Nano’s ANOther editor free text editing program Conclusion
Now that you know how to use the command line, you can do a few things like…
- Use Git from the command line
- Run Sass from the command line
- Set up a workflow with Gulp
- Or Grunt
- Set up a Jekyll site
- Download all your programs with a single command
- Set up a Linux server (AWS)
- Set up a cron job
and plenty more. There are no more limits! Have fun.
I might consider making a Windows tutorial, which would be almost the same just with a few commands and screenshots changed, and a basic Bash script tutorial.
Newsletter
Get updated when I create new content.
Unsubscribe whenever. Never any spam.Write a response
Discussion
Can you write a book on the Linux/Unix on tips, command, and everything Linux?
The way you explain it is much more easier than the one I have.
I appreciate the help!
what about ditto command?
I want to be able to copy and move select files from one directory to another. Is there a way to have bash read files names from a list
Thanks, Tania. The post was very informative and helped me understand the Terminal better.
Very helpful. Just wish you would say how to enter multiple command lines before pressing “Return” or “Enter” key. It’s clear the computer executes or “bashes” the command when you press the “enter” key.
I’m just trying to copy a list of commands from a source that’s supposed to enable me to stop Mail from crashing each time I select a particular unsolicited perhaps malicious email.
Many thanks for wonderful article all the same.
Really great article, Tania! Thanks a lot for making the "scary" simple.
Thank you Thania! this is a awesome explanation. 🙂
So helpful!
This is brilliant, thank you so much! I am an experienced UI user (20+ years, server tools, client tools, etc. etc.) but I started a new job a few weeks ago that requires terminal knowledge and this has been a big change for me. Thanks for explaining things clearly and logically. 🙂
You're welcome!
I like the design and content on your website.
Thanks.
Hi Tania pretty nice website, and the post is perfect for newbies I'll share it with my newbies friends 🙂
~ sudo rm * -f (bad newbies) XD
best regards from barcelona
It's good for revision, just I was missing one step.
Thanks,
Very neat and concise tutorial for newbies!
Hi, I’m looking for a command that resets icon folders?
Just perfect for a “newbie” that only touched a Laptop/PC in the last 30 years, (most of us with less than 40 years).
I am one of those dinosaurs that studied computer science in the 80’s and experienced a Vax with Hollerith cards, then a UNIX course with only a command line interface, then came the PC (IBM) with MS-DOS and a black screen. In my case the command line was the “normal way to access a computer” but this was forgotten in the last three decades (for the final user).
Thank you for the clear and easy introduction to this “underground and marginal World” without prejudices to all human beings.
Most computer users (without technical knowledge) just don’t know most of today’s automation, Cisco routers, Oracle databases, etc, are accessed and administered only by a command line interface. Be welcome all of you !
Thanks soooooo much.
Simple, to the point, and step by step.
You’re saving this newbie.
Very nice tutorial. Easy way to learn Linux .Good Job, thanks!!
A minor technicality, you don’t need “$sudo su”, just “$su”.
I believe invoking “$sudo command” invokes the equivalent of
$su
$command
$exit.
This is why sometimes previous invocations with sudo don’t show up in the CLI history when you press the up arrow. They’ve been stored in the root user’s history files.
This is probably the best intro to command line post I’ve seen, thanks! I’ve been learning bash scripting and one thing I found is rmtrash, which can be installed with `brew install rmtrash`: https://github.com/PhrozenByte/rmtrash. It sends removed files and directories to the trash.
Really nice explanations. I was able to add something new to my previous knowledge. Thank You!
Hi Tania – great post, very helpful and well explained.
I think people would not face borring reading your tutorial.
Very nice tutorial.
That was very helpful 🙂
Glad you think so!
IMO:
– There should be a mention of vi/vim somewhere in this article. If someone plans to learn CLI then having a basic working vi/vim is very useful. Also it is a cult!
– Commands like `ps` and `grep`.
– Piping.
– Finally, a small remark that there everything depends on the shell. While bash is probably a default choice, a beginner can also consider `zsh`…
Direct and clean, no complications, you are a good teacher, sincerely congratulations.
Thank you very much , so when hr is writing a demand like ” knows shell scripts” ,thats what they mean i guess…looking forward for windows tut,cheers
I’d like to write an article on shell scripting as well, though I’ll have to learn a bit more about that myself first!
Three things you might want to try: iTerm, ZSH, and TMUX
I’ve heard good things, but so far I’ve stuck with the built in Terminal program.
Your posts are very well explained. Thanks !! Amazing job Tania
Thank you, João. 🙂 Great to hear from you!
Very nice article! Btw, the terminal does not have to be boring black&white. I highly recommend iTerm2 (Terminal.app alternative) with some nice color themes 🙂
https://www.iterm2.com/
http://iterm2colorschemes.com/
No need for iTerm the Terminal App has setting you can change to make it all pretty if that is important. Everything from changing the default size of the box the bg color, the text size and font, syntax highlighting colors, the blinking curser, what it says in the nav bar… everything… It is all in the prefences and you can even save your own.
thank you a lot,you dont know how much you are changing lives
I like it! Good job, the only thing I really wish you would have expanded on two things:
1) you talk about redirecting echo to a file using >. I would like to see a brief explanation that > and >> are not limited to echo. Redirects are such a powerful tool that I wish you’d explain more.
2) while the link for file permissions is good from a programmers standpoint, the binary in layman terms is confusing. It’s easier to explain that read has a value of 4, write a value of 2, execute a value of 1, and that to deny there is a 0 value possible. Add the permissions you want to get the total number. I’d also like to see and explanation that there are three columns (user, user group, and everyone else). All three can have 0-7 values. Explaining this way, the link is unnecessary.
Thanks for the input. I believe those to be beyond the scope of an introduction to command line, but I would like to expand in a possible follow-up article.
hey there!
a friend and i have a v-server for a few years now. i’m doing frontend stuff and he is doing all the backend stuff. whenever i saw him doing his part i was just sitting there not knowing wtf he was doing but since i didn’t need any of this i haven’t bothered learning it. i know how to start a gulp-process in my local environment – thats more than enough. but now that i know the true power of the terminal, oh baby, i’ll never use a ‘normal’ interface.
thanks a lot for another awesome article! you’re doing a great job with these posts and i’m always checking the site if there is something new. keep it up! (:
sincerly
~
I remember when I first started, watching a software engineer friend of mine rapidly doing terminal commands. I had no idea what was going on! Now that I’ve learned, I see it wasn’t technowizardry after all.
Amazing.
Glad you liked it!