Ubuntu is a popular Linux distribution that offers a versatile and user-friendly environment for both beginners and experienced users. While the graphical user interface (GUI) is easy to use, mastering the command line interface (CLI) can help users unlock the full potential of the operating system. This comprehensive guide will introduce you to basic Ubuntu commands and terminal shortcuts, enabling you to perform tasks quickly and efficiently.
1. Navigating Your File System
1.1. pwd (Print Working Directory)
The pwd command displays your current location within the file system. This is particularly useful when you need to understand the context of your terminal session.
1.2. ls (List Files and Directories)
ls command lists the contents of your current directory, including files and subdirectories. To view the contents of a specific directory, provide its path as an argument.
ls ls /path/to/directory
1.3. cd (Change Directory)
cd command allows you to navigate through directories. To move to a specific directory, provide its path as an argument. To return to your home directory, simply type
cd without any arguments.
cd /path/to/directory cd
2. Managing Files and Directories
2.1. mkdir (Make Directory)
mkdir command creates new directories. To create a directory, provide its name as an argument.
2.2. touch (Create Empty Files)
touch command creates empty files. To create a new file, provide its name as an argument.
2.3. cp (Copy Files and Directories)
cp command is used to copy files and directories. To copy a file or directory, provide the source and destination paths as arguments. To copy a directory and its contents, use the
cp source_file destination_file cp -r source_directory destination_directory
2.4. mv (Move or Rename Files and Directories)
mv command moves or renames files and directories. To move a file or directory, provide the source and destination paths as arguments. To rename a file or directory, provide the current name and the new name as arguments.
mv source_file destination_file mv old_name new_name
2.5. rm (Remove Files and Directories)
The rm command deletes files and directories. To remove a file, provide its name as an argument. To delete a directory and its contents, use the
rm file_name rm -r directory_name
3. Working with File Contents
3.1. cat (Concatenate and Display Files)
cat command displays the contents of a text file. To view the contents of a file, provide its name as an argument. You can also use
cat to concatenate multiple files into one.
cat file_name cat file1 file2 > combined_file
3.2. less (View Large Text Files)
less command is an efficient way to view large text files. Instead of displaying the entire file,
less presents the file in pages, allowing you to scroll up and down. To exit
3.3. head (Display the First Lines of a File)
head command displays the first few lines of a text file. By default, it shows the first 10 lines. To display a different number of lines, use the
head file_name head -n 5 file_name
3.4. tail (Display the Last Lines of a File)
tail command displays the last few lines of a text file, similar to the
head command. By default, it shows the last 10 lines. To display a different number of lines, use the
tail file_name tail -n 5 file_name
4. Searching and Sorting
4.1. grep (Search for Text Patterns)
grep command searches for specific text patterns within files or directories. To search for a pattern, provide the pattern and the file name as arguments.
grep 'pattern' file_name
4.2. sort (Sort Lines of Text)
sort command sorts the lines of a text file in alphabetical or numerical order. To sort the contents of a file, provide its name as an argument.
5. Managing Users and Permissions
5.1. sudo (Execute Commands with Elevated Privileges)
sudo command allows you to execute commands with administrative privileges, similar to “Run as administrator” in Windows. This is useful when you need to modify files or directories that your user does not have permission to access.
5.2. chmod (Change File Permissions)
chmod command changes the access permissions of files and directories. The permissions are represented by a three-digit number, where each digit corresponds to the permissions for the owner, group, and others, respectively.
chmod 755 file_name
5.3. chown (Change File Ownership)
chown command changes the ownership of files and directories. To change the owner and group of a file or directory, provide the new owner and group names, followed by the file or directory name.
chown new_owner:new_group file_or_directory_name
6. Managing Processes
6.1. ps (Display Active Processes)
ps command displays a list of active processes running on your system.
6.2. kill (Terminate Processes)
kill command terminates a running process by specifying its process ID (PID). To kill a process, provide its PID as an argument.
7. Networking and System Information
7.1. ifconfig (Display Network Interfaces)
ifconfig command displays information about your network interfaces, including IP addresses and other network configuration details.
7.2. uname (Display System Information)
uname command provides basic information about your system, such as the kernel version and operating system release.
7.3. ping (Check Network Connectivity)
ping command is used to test network connectivity between your computer and a remote host. To use
ping, provide the domain name or IP address of the remote host as an argument.
8. Package Management
8.1. apt (Advanced Packaging Tool)
apt command is a powerful package management tool for Ubuntu and other Debian-based distributions. It allows you to install, update, and remove packages with ease.
To update your package index, run:
sudo apt update
To upgrade installed packages, run:
sudo apt upgrade
To install a specific package, run:
sudo apt install package_name
To remove a specific package, run:
sudo apt remove package_name
9. Terminal Shortcuts
9.1. Tab Autocompletion
Tab key to autocomplete file names, directory names, and commands while typing them in the terminal.
9.2. Command History
Use the Up and Down arrow keys to navigate through your previously executed commands.
9.3. Clear Terminal
clear to clear the terminal display.
10. Additional Basic Ubuntu Commands
10.1. alias (Create Custom Command Shortcuts)
alias command allows you to create custom shortcuts for frequently used commands.
10.2. dd (Create Bootable USB Sticks)
dd command is commonly used to create bootable USB sticks from ISO files.
sudo dd if=/path/to/iso/file of=/dev/sdX bs=4M && sync
/path/to/iso/file with the path to the ISO file and
/dev/sdX with the device identifier of your USB stick.
Mastering basic Ubuntu commands and terminal shortcuts can significantly enhance your productivity and efficiency when working with Ubuntu. This guide has provided an overview of essential commands and shortcuts to get you started. As you become more comfortable with the command line, you will discover even more powerful commands and shortcuts to help you accomplish tasks with ease.