You’ve just booted up your first Ubuntu server ever and you’re staring at the terminal screen. You’re excited yet nervous, your fingers ready to start dancing. But as your fingertips take up position on the keyboard… Nothing.
A mysterious emptiness envelopes your mind, and your hands go numb. Having heard all the tales of the legendary power of the Linux command line interface, you now feel useless, like a monkey behind the steering wheel of a Ferrari.
If this describes your first experience with an Ubuntu VPS (Virtual Private Server) or dedicated server, fear not — we’ve got you covered with a neat list of basic Ubuntu commands that will take your Linux CLI skills from “utter newb” to “I can do this”.
First off, let’s get some basic terminology straightened out. When you log in to your VPS (or simply, ‘server’), whether with PuTTy on Windows or via the native terminal on another Linux machine, you are connecting through a terminal (or ‘console’) screen. A terminal is the physical screen you would use to interact with a computer, although nowadays most terminals are virtual, meaning that you can run multiple different terminals on one computer at the same time.
Once you’re logged in to your server, you encounter a shell. The shell is an application whose main purpose is to run other applications by entering commands, and is often called a command-line shell because you execute commands line-by-line by pressing Enter. The default terminal shell for Ubuntu is called Bash, an acronym for “Bourne-Again SHell”.
So what are the commands, then? Let’s dive in!
Note: These Ubuntu commands are common to most UNIX-based distributions, so they will also work on Debian and similar flavors of Linux.
Command 1: cd
cd stands for “change directory” and you will use it to navigate your way around the files and folders on your filesystem.
To move to your filesystem’s root directory:
To move to your own user’s home directory, use the tilde (~):
To move to the directory
To move back to the previous directory you were in:
To move to the parent directory of your current directory:
Command 2: ls
ls stands for “list” and it lets you list all the files and folders in a given directory. If you don’t supply any options, it’ll simply show you all the files and folders in your current directory (but not hidden files!). Note that “folder” is used interchangeably for “directory” in this tutorial, and the same goes for “subfolder” and “subdirectory”.
To list everything including hidden files that start with “.” (e.g. “.htaccess”):
To list everything with useful details such as permissions, owner name, owner group, file size, and time of modification:
To list everything with all those useful details, while making the file sizes human-readable:
To list details for only the file called
file1, with useful details, while making the file size human-readable:
ls -lah file1
Command 3: cp
cp stands for “copy” and it lets you copy files and folders to anywhere on your filesystem. If you can see the file or folder that you want to copy when you type
ls then you don’t need to type in the full path, as seen in the examples below.
To copy the file called
file1 to the folder in your user’s home directory called
folder1 (note the tilde
~ standing in for your user’s home directory):
cp file1 ~/folder1
To copy the folder called
folder1 (and all its subfolders) recursively into
cp -r folder1 ~/folder2
To copy all files in your home directory that end with “.sh” into
~/folder1, use the asterisk as a wildcard:
cp ~/*.sh ~/folder1
Command 4: mv
mv stands for “move” and it lets you move files and folders to anywhere on your filesystem. This command is the same as using cut instead of copy in Windows.
To move the file called
file1 in your current directory to
mv file1 ~/folder1
To move the folder called
folder1 (and all its subfolders) recursively into
folder2 (note: you don’t need the
-r option as with the
mv folder1 folder2
To move all files in your home directory that end with “.sh” into
folder1, use the asterisk as a wildcard:
mv ~/*.sh folder1
Command 5: rm
rm stands for “remove” and it lets you delete files and folders.
To delete the file called
To delete the folder called
folder1 (and all its subfolders) recursively:
rm -r folder1
To delete all files in your home directory that end with “.sh”, use the asterisk as a wildcard:
Command 6: mkdir
mkdir stands for “make directory” and it lets you create a new empty directory.
To make a new directory called
To make a new directory,
newfolder1, nested inside another directory,
newparentfolder, that doesn’t exist yet:
mkdir -p newparentfolder/newfolder1
Command 7: nano
nano is a simple text editor that lets you edit files via the terminal.
To open the file called
file1 for editing:
Once you’ve opened a file with nano and made changes, you can save your changes with
o. To exit back to the terminal, press
Command 8: less
less is a basic screen reader and it lets you view the contents of a file in a scrollable format.
To exit back to the terminal, press
To view the contents of a file called
To view the results of a command (e.g.
dmesg) in less, use the pipe character “|” followed by
dmesg | less
Command 9: cat
cat is short for “concatenate” and it can be used in a variety of ways, including linking files together or simply viewing the contents of a file on screen.
To output the contents of a file called
file1 on the terminal screen:
To output the contents of multiple files on the terminal screen:
cat file1; cat file2; cat file3
To combine two files (
file2) into one file (
cat file1 file2 > file3
Command 10: find
find is a search tool and it lets you find files and folders matching a certain pattern under a given directory (and all subdirectories).
To find all files and folders matching “test1” in the
find ~/projects -name "test1"
To find all files and folders ending in “.log”, starting from the current directory:
find . -name "*.log"
To find files (not folders) in
~/projects that are older than 30 days, then delete them:
find ~/projects -type f -mtime +30 -delete\;
Command 11: grep
grep stands for “Global Regular Expression Print” and it lets you search for strings of text inside files. You can think of it as the Google for your filesystem, and it becomes extremely powerful when you combine it with regular expressions.
To search for the string “Hello” in the file called
greetings in the current directory:
grep "Hello" greetings
To recursively search for the string “Error” in all files and folders under the directory
grep -r "Error" ~/projects
To recursively (-r) search for strings under
~/projects with the word “error”, case insensitive (-i), and also show the line number where the string appears (-n):
grep -rin "error" ~/projects
Command 12: df
df stands for “display filesystem” and it shows a summary of disk space (total, used, and available) for each mounted filesystem.
To view disk space information in a human-readable format:
To view disk space information in a human-readable format, including the grand total (see bottom row of the output):
df -h --total
Command 13: du
du stands for “disk usage” and it displays the amount of disk space used by files or folders in a given directory.
To view disk usage for all files and folders in the current directory, in human-readable format:
To view disk usage for all files and folders in the current directory, in human-readable format, while opening the list of filesizes in the
less screen reader (useful for directories with lots of files and subfolders):
du -h | less
To view disk usage for all files and folders in the
~/projects directory, in human-readable format:
du -h ~/projects
Command 14: dmesg
dmesg stands for “diagnostic messages” (alternatively, “display messages”) and it displays diagnostic messages from the kernel ring buffer. In layman’s terms, it shows you useful information about your system (e.g. warnings and errors) starting from boot time up to the time you entered the command.
To view all diagnostic messages in scrollable format with the
dmesg | less
To view only those diagnostic messages that mention “usb”, use
grep (use the
-i option for case-insensitive search):
dmesg | grep -i "usb"
To view only the most recent 30 lines of diagnostic messages, use
dmesg | tail -30
Bonus tip: using BASH aliases
If you don’t like using a certain command name, you can change it to whatever you want by setting an ‘alias’. So if you prefer to type “remove” rather than “rm” (making it easier to remember, albeit longer to type), you can do so. But it gets even better… What if you want to use a string of commands, or a complex command, in one single command? This, too, can be done with an alias. You can turn the command “ls -la | less” into the alias “listless”, and when you type “listless”, you’ll instantly get a directory listing opened in the less reader (the same as typing “ls -la | less”).
Here’s an easy way to do this (replace
listless with your desired alias, and
ls -la | less with your desired command/s):
echo "alias listless='ls -la | less'" >> ~/.bash_aliases && source ~/.bash_aliases
Tom Davis is a technical contributor at TechWombat. He enjoys writing on IT, open source, electronics, and other geeky arcana.
One thought on “14 Basic Ubuntu Commands with Easy Examples”
I recently watched https://youtu.be/mhR7N9Hr1yw which is very helpful to talk about Common Linux Commands using Ubuntu