Home How-to's & guides Basic Ubuntu Commands And Terminal Shortcuts

Basic Ubuntu Commands And Terminal Shortcuts

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.

pwd

1.2. ls (List Files and Directories)

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

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

The mkdir command creates new directories. To create a directory, provide its name as an argument.

mkdir new_directory

2.2. touch (Create Empty Files)

The touch command creates empty files. To create a new file, provide its name as an argument.

touch new_file.txt

2.3. cp (Copy Files and Directories)

The 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 -r option.

cp source_file destination_file
cp -r source_directory destination_directory

2.4. mv (Move or Rename Files and Directories)

The 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 -r option.

rm file_name
rm -r directory_name

3. Working with File Contents

3.1. cat (Concatenate and Display Files)

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

The 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 less, press q.

less file_name

3.3. head (Display the First Lines of a File)

The 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 -n option.

head file_name
head -n 5 file_name

3.4. tail (Display the Last Lines of a File)

The 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 -n option.

tail file_name
tail -n 5 file_name

4. Searching and Sorting

4.1. grep (Search for Text Patterns)

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

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

sort file_name

5. Managing Users and Permissions

5.1. sudo (Execute Commands with Elevated Privileges)

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

sudo command_to_execute

5.2. chmod (Change File Permissions)

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

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

The ps command displays a list of active processes running on your system.

ps

6.2. kill (Terminate Processes)

The kill command terminates a running process by specifying its process ID (PID). To kill a process, provide its PID as an argument.

kill PID

7. Networking and System Information

7.1. ifconfig (Display Network Interfaces)

The ifconfig command displays information about your network interfaces, including IP addresses and other network configuration details.

ifconfig

7.2. uname (Display System Information)

The uname command provides basic information about your system, such as the kernel version and operating system release.

uname -a

7.3. ping (Check Network Connectivity)

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

ping example.com

8. Package Management

8.1. apt (Advanced Packaging Tool)

The 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

Press the 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

Type clear to clear the terminal display.

10. Additional Basic Ubuntu Commands

10.1. alias (Create Custom Command Shortcuts)

The alias command allows you to create custom shortcuts for frequently used commands.

alias new_command='original_command'

10.2. dd (Create Bootable USB Sticks)

The 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

Replace /path/to/iso/file with the path to the ISO file and /dev/sdX with the device identifier of your USB stick.

Conclusion

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.

Avatar of Aditya Singh
I'm a tech writer with over seven years of experience. I love to help people understand tech through my articles. I write about technology, mobiles, PCs, how-tos, and gadget reviews. I always stay updated with the latest in tech to give you the best info.
Exit mobile version