Description
Ubuntu is one of the most popular Debian-based distributions of the Linux Operating System.
The latest LTS (or long-term support) release version is 20.04, which will be available and supported till 2025.
Ubuntu supports both Command Line Interface (CLI) and Graphical User Interface (GUI) to perform various tasks on the Operating System.
- Command Line Interface (CLI) is a basic utility of any machine, which acts as a key to every operating system.
- CLI commands pass instructions directly to the operating system, whereas each GUI action invokes the respective CLI commands in the background.
However, most of the tasks are performed using the CLI. So, it's good to know the most commonly used commands.
CLI Terminal
In order to execute CLI commands, we need to first open the terminal, which can be opened using the keyboard keys Ctrl+Alt+t
together.
CLI Commands
Here is a list of the most commonly used Ubuntu/Linux CLI commands.
Command | Description |
> pwd | Returns the current working directory, where pwd stands for Print Working Directory. |
> dir | Lists all the directories in the current working directory. |
> ls | Lists all the files and directories in the current working directory. |
> ls /var/www/assets | Lists all the files and directories in the specified directory. |
> ls -al | Prints all the files and directories in the current working directory, along with detailed information. |
> ls -R | Lists all the subdirectories in the current working directory. |
> ls -a | Lists all the files and directories in the current working directory, along with the hidden files and directories. |
> cd desktop | Changes directory to "desktop" which must be an immediate subdirectory of the current working directory. |
> cd / | Changes directory to the root directory. |
> cd /var/www/assets | Changes directory to the specific directory, based on the absolute path provided. |
> cd | Changes directory to the home directory. |
> touch file.txt | Creates a new file (if not exist) or changes the file's timestamp in the current working directory. |
> stat file.txt | Returns the file details. |
> cat file.txt | Displays the content of a file. |
> cat file1.txt file2.txt > output.txt | Concatenates and saves the contents of multiple files to another file in the current working directory. |
> cat /var/www/file1.txt /var/www/file2.txt > /var/www/output.txt | Concatenates and saves the contents of multiple files to another file in the current working directory, using their absolute paths. |
> mkdir newdir | Creates a new directory in the current working directory. |
> rm file.txt | Removes a file from the current working directory. |
> rm /var/www/file.txt | Removes a file based on the absolute path provided. |
> rmdir testdir | Removes a directory from the current working directory. |
> rmdir /var/www/testdir | Removes a directory based on the absolute path provided. |
> cp file.txt otherdir | Copies a file from the current working directory to any other directory. |
> cp /var/www/file.txt /var/www/otherdir | Copies a file from the current working directory to any other directory, based on the absolute paths. |
> cp testdir -r otherdir | Copies a directory from the current working directory to any other directory. |
> cp /var/www/testdir -r /var/www/otherdir | Copies a directory from the current working directory to any other directory, based on the absolute paths. |
> mv file.txt otherdir | Moves a file from the current working directory to any other directory. |
> mv /var/www/file.txt /var/www/otherdir | Moves a file from the current working directory to any other directory, based on the absolute paths. |
> mv testdir otherdir | Moves a directory from the current working directory to any other directory. |
> mv /var/www/testdir /var/www/otherdir | Moves a directory from the current working directory to any other directory, based on the absolute paths. |
> head file.txt | Returns the first ten lines of the specified file. |
> tail file.txt | Returns the last ten lines of the specified file. |
> uname | Returns the operating system. |
> uname -a | Returns the operating system details, including its version and release number. |
> wget https://download.virtualbox.org/virtualbox/6.1.26/VirtualBox-6.1.26-145957-Win.exe | Downloads the content from the Internet. |
> sudo apt install package_name | Installs a package. This works with Ubuntu Advanced Packaging Tool (APT), which requires sudo privileges for the user. It can be used to install, remove, or perform any maintenance tasks on a Ubuntu machine. |
> sudo apt remove package_name | Removes a package. |
> sudo apt update package_name | Updates a package. |
> sudo apt upgrade package_name | Upgrades a package to its latest version available on the Internet. |
> sudo apt upgrade | Upgrades all the packages to their latest version available on the Internet. |
> history |
Returns the list of recently used commands with numeric numbers against each of them. If we want to execute any of the commands from the history, then we need to use an exclamation mark against its numeric number to get that command. |
> !2 | Gets the 2nd command from history. |
> clear | Clears the terminal screen. |
> cat file.txt | grep keyword | The command grep helps in the keyword search, which searches for a string pattern in a file. |
> man cat | Returns a detailed user manual for the specific command, where the command is "cat" in this example. |
> ps | Lists all the active processes. |
> gzip file.txt | Zips a file, where the generated zipped file will be "file.txt.gz" as the file name is "file.txt" in this example. |
> gunzip file.txt | Unzips a zipped file, where the zip file name is "file.txt.gz" in this example. |
> hostname | Returns the hostname. |
> ping google.com | Checks the connectivity to a specific server, which is "google.com" in this example. |
> w | Lists all the currently logged-in users and their details. |
> sudo useradd mike | Adds a new user to the system, which is "mike" in this example. Ubuntu supports multiple users accessing the system at the same time, so it allows the creation of multiple users. |
> sudo userdel mike | Deletes an existing user from the system, which is "mike" in this example. |
> passwd arun | Prompts for the specified user's password change, where the user is "arun" in this example. |
Overall
We now know the basic and most commonly used CLI commands on a Ubuntu/Linux machine.