Basic Linux Shell Commands for Beginners

Linux and other Unix-like systems, including macOS, have a powerful command line. Simple commands can be joined together to do complex jobs quickly. This guide covers the basic commands you'll use every day.

Open a terminalOn Ubuntu press Ctrl + Alt + T. On macOS open the Terminal app.
Save typingPress Tab to complete file names, and ↑ to bring back earlier commands.
Stop a commandPress Ctrl + C to cancel a command that's still running.

Finding your way around

pwd shows where you are, ls lists what's there, and cd moves you somewhere else.

pwd
Prints the directory you're currently in.
ls
Lists the files in the current directory.
ls -l
Lists files in "long" format, with permissions, owner, size and date.
ls -lah
Lists all files, including hidden ones, with sizes that are easy to read (K, M, G).
cd /var/log
Moves to the /var/log directory.
cd ~
Moves to your home directory. Typing cd on its own does the same.
cd ..
Moves up one level.
cd -
Goes back to the directory you were in before.

Working with files and folders

mkdir files
Makes a new directory called files.
cp file.txt newfile.txt
Copies file.txt to a new file called newfile.txt.
cp -r /home/user/pics /home/user2/
Copies the pics directory and everything in it (-r means recursive) into /home/user2/.
mv file.txt /home/user/documents/
Moves file.txt into the documents directory.
mv oldfile.txt newfile.txt
Renames oldfile.txt to newfile.txt.
rmdir files
Deletes the directory called files, but only if it's empty.
rm file.txt
Deletes file.txt.
rm -r files
Deletes the directory called files and everything in it.

Take care with rm. There's no recycle bin on the command line, so deleted files are gone for good. Add -i (for example rm -i file.txt) to be asked before each file is deleted.

Reading files

cat file.txt
Prints the whole file to the screen. Best for short files.
less file.txt
Opens a file so you can scroll through it. Use the arrow keys to move, / to search and q to quit.
head file.txt
Shows the first 10 lines of a file. tail shows the last 10.
tail -f logfile.log
Shows the end of a log file and keeps showing new lines as they're added. Press Ctrl + C to stop.

Finding files

locate -i filename
Quickly searches for filename anywhere on the system, ignoring upper and lower case. It uses a database that is updated once a day, so very new files may not show up. Run sudo updatedb to refresh it. On some systems you'll need to install the plocate package first.
find . -name "*.txt"
Searches the current directory and everything below it for files ending in .txt. Slower than locate, but always up to date. See our guide to find for more examples.

Network and system

ping -c 4 8.8.8.8
Sends 4 test packets to 8.8.8.8 (Google's public DNS server) and shows how long each reply takes. Without -c 4 it keeps going until you press Ctrl + C.
w
Shows who is logged in, what they are doing, and how long the system has been running.

Getting help

man ls
Opens the manual page for a command, in this case ls. Press q to quit.
ls --help
Shows a shorter summary of a command's options. Most commands support --help.

Quick reference

CommandWhat it does
pwdShow the current directory
lsList files
cdChange directory
mkdirMake a directory
cpCopy files
mvMove or rename files
rm / rmdirDelete files / empty directories
cat / lessShow / scroll through a file
head / tailShow the start / end of a file
locate / findSearch for files
pingTest a network connection
wShow who is logged in
manRead a command's manual