Ubuntu, one of the most widely used Linux distributions, offers a powerful and flexible operating system for both beginners and advanced users alike. Command-line tools play a crucial role in Ubuntu, allowing users to harness the true potential of the system. In this article, we will explore eighteen popular commands that every Ubuntu user should know. Whether you’re a seasoned Linux enthusiast or a newcomer, mastering these commands will enhance your Ubuntu experience and make you more efficient in managing your system.
1. sudo: Administrative Privileges
The “sudo” command grants temporary administrative privileges to regular users. It allows you to execute commands as a superuser, providing access to system-wide configurations and files. This command ensures the security and integrity of your system
sudo systemctl stop service_name
2. APT-GET: Installing and Managing Software
The “apt-get” command is a package management tool that simplifies the process of installing, upgrading, and removing software packages in Ubuntu. Whether you want to install a new application or update your system, “apt-get” is the go-to command.
sudo apt-get update
sudo apt-get install package_name
3. cd: Navigating Directories
The “cd” command is used to change directories within the Ubuntu file system. It allows you to move between folders, access different locations, and perform operations on specific files
cd .. # go back one directory
cd ~ # go to home directory
4. ls: Listing Directory Contents
The “ls” command lists the files and directories within a specified location. It provides essential information such as file permissions, ownership, size, and modification dates, enabling you to navigate and manage your file effectively.
ls -l # use long listing format
ls -a # list all, does not ignore entries starting with .
5. cp: Copying Files and Directories
The “cp” command allows you to make copies of files or directories. It enables you to duplicate data while preserving file attributes, permissions, and ownership. This command is especially useful when creating backups or transferring files.
cp file.txt /directory1/directory2 # copies the file to directory2
cp -r directory /directory1/directory2 # copies recursively, the directory to directory2
6. rm: Removing Files and Directories
The “rm” command is used to delete files and directories permanently. It is essential to exercise caution while using this command, as files removed with “rm” cannot be easily recovered.
rm file.txt # deletes 1 file
rm -r directory # deletes directory and everything in it
7. cat: Concatenating and Displaying File Contents
The “cat” command is used to display the contents of one or multiple files. It can also be used to concatenate files and redirect the output to another file or command.
cat file.txt # display contents of 1 file
cat file1.txt file2.txt # display contents of multiple files
cat file1.txt > newfile.txt # copy the contents of 1 file and place it into a new file
8. grep: Searching Within Files
The “grep” command searches for specific patterns or text within files. It is a powerful tool for locating information, filtering output, and troubleshooting issues within your system.
grep "search_string" file.txt # search the file for the search_string text
grep -r "search_string" /directory1/directory2 # search recursively, directory2 for any files that contain the search_string
9. chmod: Changing File Permissions
The “chmod” command allows you to modify file permissions, granting or restricting access for users, groups, and others. It is an essential command for managing file security and ensuring privacy.
chmod +x script.sh # adds eXecute permission to the file
chmod 644 file.txt
chmod 700 private_directory
Number is in the format: User, Group, Other
10. history: Viewing Command History
The “history” command displays a list of previously executed commands, therefore, It allows you to recall and reuse commands without retyping, saving time and effort.
history # display history of all previous commands
!10 # re-run line 10
11. find: Searching for Files
The “find” command helps locate files and directories based on various criteria such as name, size, type, and modification time. Further more, it offers advanced search capabilities and is particularly useful when searching for specific files or performing bulk operations
find / -name "filename" # search all files and directories for filename
find /directory1/directory2 -name "filename" # search directory2 for filename
12. tar: Compressing and Extracting Files
The “tar” command is used to create compressed archive files and extract their contents. It supports various compression algorithms and allows for the efficient packaging and distribution of files and directories.
tar -cvf archive.tar file1 file2 # create archive.tar with file1 and file2
tar -xvf archive.tar # extract the contents within archive.tar
13. rsync: File Synchronization
The “rsync” command is a powerful tool for synchronizing files and directories between local and remote systems. It ensures efficient and incremental transfers, making it ideal for backups, data replication, and remote file synchronization.
rsync -avz source_directory/ destination_directory/ # local sync of directories
rsync -avz local_file.txt remote_server:/directory1/directory2 # sync a file to a remove server
14. sed: Stream Editor
The “sed” command is a versatile stream editor for processing text. It allows you to perform search-and-replace operations, text transformations, and advanced editing tasks using powerful regular expressions.
sed 's/search_term/replace_tem/g' file.txt # search the file for a term and replace it, print the output
15. grep with Regular Expressions: Advanced Searching
Combining the “grep” command with regular expressions expands its search capabilities. Regular expressions enable complex pattern matching, making it easier to search for specific text patterns or extract information from files.
grep -E "regex_pattern" file.txt
grep -E "(test)" file.txt # look for the word test within the file file.txt
You can test your REGEX patterns here -> regex101.com
16. systemctl: Managing System Services
The “systemctl” command is used to manage system services within Ubuntu. It allows you to start, stop, enable, disable, and monitor services, providing control over various aspects of the system’s behavior.
sudo systemctl --type=service --state=running # lists all running services
sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl enable service_name
17. df: Checking Disk Space Usage
The “df” command displays information about disk space usage on file systems. DF helps you monitor available disk space, identify storage bottlenecks, and make informed decisions when managing your storage.
df -h # prints sizes in powers of 1024 (Human Readable)
18. du: Analyzing Directory Sizes
The “du” command calculates the disk usage of files and directories. It provides insights into the sizes of individual files and directories, enabling you to identify storage-intensive locations and optimize disk space usage.
du -h /directory1/directory2
These 18 popular commands will expand your Ubuntu knowledge and allow you to perform tasks more efficiently. From remote access and file synchronization to powerful text manipulation and system service management, mastering these commands empowers you to take full control of your Ubuntu system. Experiment with these commands, and embrace the power of the command line in Ubuntu!