DEV Community

Cover image for 18 Most-used Linux Commands You Should Know
Đặng Đình Sáng
Đặng Đình Sáng

Posted on

18 Most-used Linux Commands You Should Know

The command line interface(CLI) may seem scary if you're new to it, but it's actually quite straightforward once you understand some basic concepts. Think of the command line as a text-based interface for your computer - you type in commands and the computer responds, rather than using buttons and menus like a regular graphical interface. The more you utilize the command, the faster your task will go.
If you don't know about GUI and CLI then you go to find out Difference between CLI and GUI

A story

Before we start, I can tell you a story about how I used to remember all the commands. It has name is The Adventures of Linux Land.

One sunny day in Linux Land, all the commands were hanging out in the main directory. Lister the ls command was bored, so he suggested they each share what they do to help the users.

"I list out all the files and folders so they can see what's inside!" said Lister proudly. Nearby, Changey the cd command chimed in, "I help the users navigate between folders so they can find what they need."

Just then, Maker, the mkdir command came running over. "Guess what, I just helped a user create a new folder called 'Projects' for all their work!" But where was this new folder? Changey changed them into Projects to see.

Inside were some blank files, which reminded Gripper the cp command of his job. "I copy files so users can have backups or move them between folders safely!" Down the path, Mover the mv command agreed, "And I rename files the users drag me to move between folders."

Meanwhile, Permy the chmod command was busy making sure everyone had the right permissions to edit files. Findy the find command and Grepy the grep command searching high and low using clues to hunt down specific files for the users.

Later, the gang met up for a picnic. Packy the tar command packed up all the food and blankets so they could travel easily. Once there, Viewy the vi command, and Catty the cat command were put to work reading out stories and songs.

As night fell, they gazed at the starry sky. Monitor the top command listed the light processes while Sleepy the ps command snoozed nearby. And Killy the kill command waited quietly in case any bad programs caused trouble and needed to be eliminated.

Diggy the du command dug in the dirt to measure file sizes, while Networky the ifconfig and Pingy the ping commands chatted with faraway systems.

1. ls
List files and directories

ls path_to_folder
Enter fullscreen mode Exit fullscreen mode

Image description

ls -a command shows the hidden files and folders that start with a special character like .

Image description

You may notice that it shows . and .. in the output
ls . or ls -a . list all files and directories in the current folder, or you list all files and directories in child folders with ls ./child-folder/child-folder.

Image description

ls .. or ls -a .. list all files and directories of the parent of the current folder.

Image description

The ".." is a special code that means "parent directory". Every folder on your computer exists inside another folder, except for the very top-most ones. So ".." lets you peek inside the folder that is one level above your current one. For example, if you are inside the "Documents" folder, ".." would show you the contents of your /home/users (in Linux) or /users/user (in Mac) folder, which contains Documents along with other top-level stuff. And if you were deep inside nested subfolders, it helps you quickly bounce back up a level.
You can use multiple instances of ".." together in the ls command to go up multiple levels in the directory structure.

For example, if you were really deep inside a folder like:

/Projects/2023/Nov
Enter fullscreen mode Exit fullscreen mode

And wanted to see what was in the Documents folder directly, you could do:

ls ../../..
Enter fullscreen mode Exit fullscreen mode

The ".." tells it to go up one level each time. So here's what it's doing:

../ - Goes up from Nov to 2023
../ - Goes up from 2023 to Projects
../ - Goes up from Projects to Documents
Enter fullscreen mode Exit fullscreen mode

Then running ls will show you the contents of the Documents folder a few levels above your current location.
learning

Image description

If the path to go to the current folder is long, you have to use many .. codes like ../../../..., which is inconvenient so instead of using .. we can use ~/ for saving time.
The ~ symbol represents your home directory location. So no matter where you are in the filesystem, ls ~/ will always show you the contents of your top-level home folder.

Image description

Image description

Some key things to know about ~/:

  • It works the same as ~/ on macOS, Linux, and other Unix-like systems.
  • Your home directory path may be something like /home/your-username but ~/ hides that detail.
  • It saves you from having to type out the full path each time or cd back to your home.
  • Great for quickly checking your high-level files without changing directories.

Access to some important system folders (/var, /etc, /usr, /tmp ...) you just need to ls /var/, and regardless of where you are in the filesystem, it does not require an absolute path to it.

All special characters ., .., ~, system folders apply for commands that use the path to folder ls, cd, mkdir, rm, cp, mv, chmod, grep, find, tar, vi, cat, du ...

2. cd
Change the current directory

Image description

Basic command with cd:

cd folder-name
cd ./folder-name
cd folder-name/child-folder/.. <- go to child folder
cd ../../... <- go to parent folder
cd ~/ <- go to home folder
cd /etc
Enter fullscreen mode Exit fullscreen mode

example:

Image description

cd - command will switch you to the previous working directory. Every time you use cd to navigate somewhere, your shell remembers both the previous location and the new location.
So if you do several cd commands to hop around folders, you can cd - to instantly jump back to where you were before. This is much faster than retyping the full path each time.

Image description

3. mkdir
Create a new directory mkdir folder-name in the current dir.

Image description

Basic command:

➜ mkdir ../folder1 <- create folder in parent dir
➜ mkdir folder2 <- create folder in current dir
➜ mkdir -p folder2/folder3 <- create folder in child dir
➜ mkdir -p ~/Documents/Work/learning/folder/folder4 <- can use ~ same as cd
Enter fullscreen mode Exit fullscreen mode

By default, mkdir will only create one directory level at a time. So if you tried to make a nested folder like 'folder1/folder2' without -p, it would fail because folder1 doesn't exist yet.

The -p flag tells mkdir to make any necessary parent folders automatically as part of the command.

Image description

4. rm
Remove files or directories

rm path_to_file/path_to_folder
rm -r path_to_folder
Enter fullscreen mode Exit fullscreen mode

To delete folders, use the -r or -R option for a recursive removal of the contents as well. E.g rm -r foldername.

Image description

Always double-check your spelling and path when deleting to avoid removing the wrong thing. And be careful removing files owned by root/other users unless you're 100% sure. Stick to your own user files when learning.

Have you ever heard about the rm -Rf / command? It deletes the root "/" directory and all of its contents permanently erase all data on the entire system. This is an extreme command that should only be used deliberately by experts in very rare situations (like reinstalling an OS from scratch), you have to stay away from this command and don't try it.

5. cp
Copy files or directories

cp path_to_file_source path_to_file_dest <- for file
cp -r path_to_folder_source path_to_folder_dest <- for folder
Enter fullscreen mode Exit fullscreen mode

Image description

6. mv
Move or rename files or directories

mv path_to_oldDir path_to_newDir
mv path_to_oldName path_to_newName
Enter fullscreen mode Exit fullscreen mode

Example:

Image description

As you can see, after moving folder2 to a new folder3, folder2 is gone and folder3 is created.

7. chmod
Change file or directory permissions.
The chmod command allows you to modify the permissions of files and folders in Linux/macOS, controlling who can view, edit, or execute them. This is useful for sharing your stories with others while maintaining control.

Use ls -l to list permissions of current dir

Image description

chmod 644 init.txt - Allows everyone to read the init, but only you can modify it.

chmod 775 folder1 - Makes a folder readable/writable by everyone, so others can add their scenes.

chmod 700 story.txt - Only you can access this story file until it's ready.
Enter fullscreen mode Exit fullscreen mode

Example:

Image description

8. grep
Search for a pattern in files.

grep "her eyes" *.txt - Finds lines containing that phrase across multiple text files.

grep -r -i "magic" storyfolder/ - Searches folder case-insensitively for magic.
Enter fullscreen mode Exit fullscreen mode

Image description

9. find
Search for files and directories

find . -name "drafts_*" - This would find all files/folders beginning with "drafts_" in the current directory. Great for draft cleanup!

find Documents/Project -mtime -10 - Lists files modified within the last 10 days. Handy for tracking recent changes.
Enter fullscreen mode Exit fullscreen mode

Image description

10. tar
Manipulate tarball archive files.

tar czf Backups/Project1_archive.tar.gz Project1/ - Creates a gzipped tarball of Project1 folder for safekeeping.

tar tf File.tar - Lists all the contents without extracting, handy for checking archives.

tar xzf File.tar.gz - Extracts tarball back into current folder. Great for sharing projects.

tar cvf - Adds multiple sources to single archive for tidy bundles.
Enter fullscreen mode Exit fullscreen mode

Image description

11. vi
Edit files using text editors.

vi path_to_file
Enter fullscreen mode Exit fullscreen mode

More detail: https://www.cyberciti.biz/faq/linux-unix-vim-save-and-quit-command/

Image description

12. cat
Display the content of files

cat path_to_file
Enter fullscreen mode Exit fullscreen mode

More detail: https://www.cyberciti.biz/faq/linux-unix-appleosx-bsd-cat-command-examples/

Image description

13. top
Display processes and resource usage top.

More detail: https://www.cyberciti.biz/faq/how-to-find-the-usage-of-individual-cpu-top-command-usage-freebsd/

Image description

14. ps
Display processes information.

More detail: https://www.cyberciti.biz/faq/show-all-running-processes-in-linux/

15. kill
Terminate a process by sending a signal.
I often use it with lsoft to check PID.

lsoft -i tcp:[port]
kill -9 [PID]
Enter fullscreen mode Exit fullscreen mode

Image description

16. du
Estimate file space usage.

Image description

17. ifconfig
Configure network interfaces.

ifconfig <- Displays the status of all network interfaces like Ethernet, WiFi, etc. Shows technical details like IP address, MAC address, broadcast address and more.

ifconfig interfaceName <- Shows status of a specific interface like en0 or wlan0. Good for troubleshooting a single connection.

ifconfig interfaceName up/down <- Enables or disables an interface without unplugging it. Handy if you need to isolate a network issue.

ifconfig interfaceName inet addr/netmask <- Allows manually setting IP address rather than using DHCP. Useful for testing on a local network.
Enter fullscreen mode Exit fullscreen mode

18. ping
Test network connectivity between hosts.

ping websiteaddress - Checks if you can reach a domain or public IP. Great for troubleshooting web access issues.

ping internalip - Confirms LAN connectivity between local machines without public IPs. Useful for team workflows.

ping -c 3 google.com - Pings up to 3 times only for quick checks rather than indefinite pings.

ping -s filesize - Sends larger packet sizes to test performance over different data amounts.
Enter fullscreen mode Exit fullscreen mode

Combining ping with ifconfig allows isolating a connection by the interface to pinpoint problems.

Conclusion

Overall, familiarizing yourself with basic Linux file management and troubleshooting skills will serve you well as a writer, whether working solo or with teams. Let me know if any other command explanations would be helpful as you continue exploring the Linux environment!

Top comments (0)