Linux

30 Must-Know Linux Commands for Command Line Mastery

Linux is a powerful operating system used by developers, system administrators, and tech enthusiasts around the world. One of the key reasons for its popularity is the versatility and control it provides through the command line interface (CLI). With thousands of commands available, it can be overwhelming for beginners to know where to start. Let’s dive in and explore some these essential commands!

On This Page

Key Linux Commands and Their Use Cases

Linux

Among the most essential commands in Linux are ls, cd, grep, find, and ssh. The ls command is used to list files and directories, providing various options for detailed views. For example, ls -l displays files in a long format, showcasing permissions, owners, and sizes.

Meanwhile, the cd command facilitates navigation between directories, while grep is indispensable for searching through files. Using grep 'keyword' filename allows users to filter specific text, saving valuable time during data analysis.

The find command is useful for locating files with specific criteria, like modification dates or file types. It is particularly beneficial for systems with extensive file systems, where manual searching would be cumbersome. For remote management, ssh (Secure Shell) enables secure connectivity to other devices, making it essential for remote server maintenance.

Below is the complete list of commands .

CommandDescriptionExamplesUse Case
1. lsLists directory contents.ls -l (detailed view), ls -a (show hidden files), ls -lh (human-readable sizes)View files and their details, locate hidden files, check file sizes.
2. cdChanges the current directory.cd /home/user, cd .. (move up a level), cd ~ (go to home directory)Navigate between directories quickly.
3. pwdPrints the current working directory path.pwd, echo $PWDVerify the current directory path, troubleshoot navigation issues.
4. cpCopies files or directories.cp file.txt /backup/, cp -r dir1/ dir2/ (copy directories), cp -u source dest (only newer files)Backup files, copy directories, synchronize updates.
5. mvMoves or renames files or directories.mv old.txt new.txt (rename file), mv file.txt /newdir/ (move file)Rename files or reorganize directories.
6. rmRemoves files and directories.rm file.txt, rm -r folder/, rm -f file.txt (force remove)Permanently delete unwanted files or folders.
7. mkdirCreates new directories.mkdir newdir, mkdir -p parent/child (create nested directories)Organize files into new folders.
8. rmdirRemoves empty directories.rmdir emptydir, rmdir -p parent/child (remove parent if empty)Clean up unused empty directories.
9. touchCreates empty files or updates timestamps.touch newfile.txt, touch -t 202401011230 file.txt (set custom timestamp)Create placeholder files, update modification timestamps.
10. catDisplays file contents.cat file.txt, cat file1.txt file2.txt (concatenate), cat > newfile.txt (write to a file)View, combine, or create files.
11. lessViews file contents page by page.less logfile.txt, less +/search_term filename (search inside file)Easily navigate large files.
12. grepSearches for patterns in files.grep "error" logfile.txt, grep -i "warning" file.txt (ignore case), grep -r "function" /src/ (recursive)Find specific text, locate keywords, analyze logs.
13. findSearches for files based on criteria.find /home -name "*.log", find . -type d -name "backup*", find /var -size +100MLocate files by name, type, size, or date.
14. psLists currently running processes.ps aux, ps -ef, ps -u usernameView active processes, troubleshoot high CPU usage.
15. topProvides a real-time view of system resource usage.top, top -u user, top -p PID (specific process)Monitor system performance, track resource-heavy processes.
16. chmodChanges file permissions.chmod 755 script.sh, chmod +x file.sh (add execute), chmod -R 600 folder/Manage file permissions for security and access control.
17. chownChanges file ownership.chown user:group file.txt, chown -R user folder/Assign ownership of files or directories to specific users or groups.
18. tarArchives and extracts files.tar -czf archive.tar.gz folder/, tar -xzf archive.tar.gz (extract), tar -tvf archive.tar (list contents)Archive or compress files, extract backups.
19. sshSecurely accesses remote servers.ssh user@server, ssh -i key.pem user@host (use SSH key), ssh -L 8080:localhost:80 user@host (port forwarding)Remotely manage servers or establish secure tunnels.
20. scpSecurely copies files between systems.scp file.txt user@remote:/path/, scp -r folder user@remote:/path/Transfer files between local and remote machines securely.
21. wgetDownloads files from the web.wget http://example.com/file.txt, wget -r -np -nd http://example.com/dir/Download files, mirror directories, automate batch downloads.
22. curlTransfers data from or to a server.curl http://example.com, curl -o file.txt http://example.com, curl -I http://example.com (headers only)Fetch web data, test API endpoints, automate HTTP requests.
23. dfDisplays disk space usage.df -h, df -T, df -i (inode usage)Check available disk space, troubleshoot full partitions.
24. duShows disk usage for files and directories.du -sh folder/, `du -asort -n(sort by size),du -c` (total size)
25. unameDisplays system information.uname -a, uname -r (kernel version), uname -m (machine hardware)Check OS details, diagnose compatibility issues.
26. headShows the first few lines of a file.head file.txt, head -n 20 file.txt (first 20 lines)Preview the start of a large file.
27. tailDisplays the last few lines of a file.tail logfile.txt, tail -f logfile.txt (follow mode), tail -n 50 file.txt (last 50 lines)Monitor log files in real time, check recent entries.
28. historyLists previously entered commands.history, `historygrep ssh, !500` (run command #500)
29. aliasCreates shortcuts for commands.alias ll='ls -la', alias rm='rm -i' (interactive delete), unalias llSimplify repetitive commands, avoid dangerous commands.
30. killTerminates processes.kill PID, kill -9 PID (force terminate), killall processnameStop unresponsive processes, manage background tasks.

Be careful with commands like rm, kill, and chown as they can cause data loss or system changes.

Conclusion

Mastering these Linux commands will give you a solid foundation to work more efficiently and confidently in a Linux environment. As you grow more comfortable with these commands, you’ll be able to leverage the true power of the Linux command line and explore more advanced operations. So keep experimenting, and let the terminal be your playground!

FAQs

How do I copy files from one directory to another in Linux?

Answer: Use the cp command. For example, to copy a file named example.txt from the current directory to the backup folder, use cp example.txt backup/. For directories, use cp -r folder_name destination/ to copy the entire folder.

How can I view the contents of a file in Linux?

Answer: There are multiple commands to view file contents:
cat filename.txt – Displays the whole file.
less filename.txt – Opens the file with the ability to scroll.
head filename.txt – Shows the first 10 lines.
tail filename.txt – Shows the last 10 lines.

How can I find a specific file in Linux?

Answer: You can use the find command. For example, to find all .txt files in the /home directory, use find /home -name "*.txt". You can also search by size, type, or modification date.

How do I see a list of running processes?

Answer: Use the ps or top commands. ps aux shows a detailed list of running processes, while top provides a real-time view of active processes along with their CPU and memory usage.

How do I change file permissions in Linux?

Answer: Use the chmod command. For example, to make a script file executable, use chmod +x script.sh. You can adjust permissions for the owner, group, and others using numeric values like chmod 755 filename.

What command should I use to securely access a remote server?

Answer: Use the ssh command. For example, ssh user@server_ip will connect you to a remote server securely. If you have a private key, use ssh -i /path/to/key.pem user@server_ip.

How do I delete a directory in Linux?

Answer: Use the rm -r command. For example, rm -r folder_name will remove the folder and its contents. Be careful with this command, as it cannot be undone.

How can I check my disk space usage?

Answer: Use the df -h command to display disk space usage in a human-readable format. This will show the available and used space for each mounted partition.

How can I download files from the internet using the command line?

Answer: You can use the wget or curl commands. For example, wget http://example.com/file.zip will download file.zip to your current directory. Alternatively, use curl -O http://example.com/file.zip for a similar result.

What is the difference between su and sudo?

Answer: su (switch user) allows you to switch to another user account, usually the root user, for a session. sudo (superuser do) temporarily grants higher privileges to run a single command as root without changing the current user.

What does the alias command do?

Answer: The alias command creates shortcuts for long or frequently used commands. For example, alias ll='ls -la' creates a shortcut ll for ls -la. To remove an alias, use unalias alias_name.

How do I terminate an unresponsive process?

Answer: Use the kill command followed by the process ID (PID), e.g., kill 1234. If the process does not terminate, use kill -9 1234 to forcefully stop it. You can find the PID using ps or top.

You May Also Like

More From Author

+ There are no comments

Add yours