18 Most-used Linux Commands You Should Know
Tags: beginners, learning, cli
Published: 2023-11-30 00:38:43

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](https://byjus.com/gate/difference-between-cli-and-gui/#:~:text=GUI%20lets%20a%20user%20interact,the%20help%20of%20various%20commands.)

## 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwpdudncrfodojpznl1z.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cplp9beimccg2jbm3tkw.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6b2sxw8d54fp64keix7g.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amo601ov94vkf3xu4ho6.png)

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
```
And wanted to see what was in the Documents folder directly, you could do:

```
ls ../../..
```

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
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crjhlrb046kza6p12ymp.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04mtrop21tjpeo0nrdml.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nbvspf1jjh43eazj370.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w7xana1vpup6ui4uxb7a.png)

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
```
example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjkd23srpfde0y4ro0mz.png)

`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/345fy97tsu4l5uloy263.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqstgbzgceiwzeagq0vp.png)

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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dpwhp296rwmm0kro56r9.png)

**4. rm**
Remove files or directories

```
rm path_to_file/path_to_folder
rm -r path_to_folder
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h033ijfdglazmg38dkux.png)

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56kppfzbr5bkmqqkko6l.png)

**6. mv**
Move or rename files or directories

```
mv path_to_oldDir path_to_newDir
mv path_to_oldName path_to_newName
```
Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhmdcfrhecb98fi8qo6l.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2dkv604d3yg3dw7vx0f.png)

```
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.
```
Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ikx33hj87mcs8diahgs.png)

**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.
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lpybvezawthltgowttdq.png)

**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.
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11ppoglm3ijhm3z6ovjx.png)

**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.
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krnh69w1y5eghtwlno3v.png)

**11. vi**
Edit files using text editors.

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmo4j2ijoe3374zdxn4e.png)

**12. cat**
Display the content of files

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/439z29vcu54wm6be84kz.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7wttfigfa709cq6kf4e.png)

**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]
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8s4nujhqi5byzbuqqh9f.png)

**16. du**
Estimate file space usage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qubnf4ljzi1rqqw97tq.png)

**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.
```

**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.
```
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!