14 Basic Ubuntu Commands with Easy Examples

Basic Ubuntu Commands with Examples

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.

Examples

To move to your filesystem’s root directory:

cd /

To move to your own user’s home directory, use the tilde (~):

cd ~

To move to the directory ~/projects:

cd ~/projects

To move back to the previous directory you were in:

cd -

To move to the parent directory of your current directory:

cd ..

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”.

Examples

To list everything including hidden files that start with “.” (e.g. “.htaccess”):

ls -a

To list everything with useful details such as permissions, owner name, owner group, file size, and time of modification:

ls -la

To list everything with all those useful details, while making the file sizes human-readable:

ls -lah

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.

Examples

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 ~/folder2:

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.

Examples

To move the file called file1 in your current directory to ~/folder1:

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 cp command):

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.

Examples

To delete the file called file1:

rm file1

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:

rm ~/*.sh

Command 6: mkdir

mkdir stands for “make directory” and it lets you create a new empty directory.

Examples

To make a new directory called newfolder1:

mkdir newfolder1

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.

Examples

To open the file called file1 for editing:

nano file1

Once you’ve opened a file with nano and made changes, you can save your changes with ctrl + o. To exit back to the terminal, press ctrl + x.

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 q.

Examples

To view the contents of a file called file1:

less file1

To view the results of a command (e.g. dmesg) in less, use the pipe character “|” followed by less:

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.

Examples

To output the contents of a file called file1 on the terminal screen:

cat file1

To output the contents of multiple files on the terminal screen:

cat file1; cat file2; cat file3

To combine two files (file1 and file2) into one file (file3):

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).

Examples

To find all files and folders matching “test1” in the ~/projects directory:

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.

Examples

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 ~/projects:

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.

Examples

To view disk space information in a human-readable format:

df -h

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.

Examples

To view disk usage for all files and folders in the current directory, in human-readable format:

du -h

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.

Examples

To view all diagnostic messages in scrollable format with the less reader:

dmesg | less

To view only those diagnostic messages that mention “usb”, use dmesg with 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 tail:

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

One thought on “14 Basic Ubuntu Commands with Easy Examples”

Leave a Reply

Your email address will not be published. Required fields are marked *