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.
pwdPrints the directory you're currently in.
lsLists the files in the current directory.
ls -lLists files in "long" format, with permissions, owner, size and date.
ls -lahLists all files, including hidden ones, with sizes that are easy to read (K, M, G).
cd /var/logMoves 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 filesMakes a new directory called files.
cp file.txt newfile.txtCopies 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.txtRenames oldfile.txt to newfile.txt.
rmdir filesDeletes the directory called files, but only if it's empty.
rm file.txtDeletes file.txt.
rm -r filesDeletes 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.txtPrints the whole file to the screen. Best for short files.
less file.txtOpens a file so you can scroll through it. Use the arrow keys to move, / to search and q to quit.
head file.txtShows the first 10 lines of a file. tail shows the last 10.
tail -f logfile.logShows the end of a log file and keeps showing new lines as they're added. Press Ctrl + C to stop.
Finding files
locate -i filenameQuickly 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.8Sends 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.
wShows who is logged in, what they are doing, and how long the system has been running.
Getting help
man lsOpens the manual page for a command, in this case ls. Press q to quit.
ls --helpShows a shorter summary of a command's options. Most commands support --help.
Quick reference
| Command | What it does |
|---|---|
pwd | Show the current directory |
ls | List files |
cd | Change directory |
mkdir | Make a directory |
cp | Copy files |
mv | Move or rename files |
rm / rmdir | Delete files / empty directories |
cat / less | Show / scroll through a file |
head / tail | Show the start / end of a file |
locate / find | Search for files |
ping | Test a network connection |
w | Show who is logged in |
man | Read a command's manual |