Community for developers to learn, share their programming knowledge. Register!
Command Line Basics

Commonly Used Commands in Linux


In the ever-evolving world of technology, understanding the Linux command line is essential for developers and system administrators alike. This article serves as a comprehensive guide to some of the most commonly used Linux commands. If you're looking to enhance your skills or get a refresher on these commands, you can get training on our this article. Let’s dive into the essentials that will empower you to navigate and manipulate the Linux environment effectively.

Navigating the File System: cd, ls, pwd

Effective navigation of the Linux file system is foundational for any user. Here are three commands that are indispensable in this regard:

cd (Change Directory)

The cd command allows users to change their current directory. For example, to move into a directory named Documents, you would use:

cd Documents

To go back to the previous directory, you can use:

cd ..

ls (List)

The ls command lists the contents of a directory. You can use various options to customize the output. For instance, ls -l provides a detailed list, showing permissions, owners, and sizes.

ls -l

pwd (Print Working Directory)

To find out your current directory, pwd is your go-to command. Simply typing pwd will output the full path of your current working directory.

pwd

These commands form the backbone of file system navigation in Linux, allowing users to traverse directories with ease.

File Manipulation Commands: cp, mv, rm

Once you're familiar with navigating the file system, the next step is manipulating files. Here are key commands for file operations:

cp (Copy)

The cp command is used to copy files or directories. To copy a file named file1.txt to a new file called file2.txt, the command would be:

cp file1.txt file2.txt

To copy an entire directory and its contents, you would use the -r option:

cp -r /source_directory /destination_directory

mv (Move)

The mv command serves a dual purpose: it can move files and rename them. For instance, to move file1.txt to a different directory:

mv file1.txt /path/to/destination/

To rename a file, simply specify the new name:

mv oldname.txt newname.txt

rm (Remove)

The rm command is used to delete files or directories. Caution is advised here, as this action is irreversible. To remove a file, you would use:

rm file1.txt

To remove a directory and its contents, use the -r option:

rm -r /path/to/directory

By understanding these file manipulation commands, you can effectively manage your files within the Linux operating system.

Viewing and Editing Files: cat, nano, vim

Viewing and editing files is a common task that every developer must master. Here are some vital commands to facilitate this:

cat (Concatenate)

The cat command is perfect for displaying the contents of a file in the terminal. For example, to view file1.txt, you would use:

cat file1.txt

You can also concatenate multiple files into one:

cat file1.txt file2.txt > combined.txt

nano (Text Editor)

nano is a user-friendly command-line text editor that is great for beginners. To edit a file using nano, simply type:

nano file1.txt

This opens the file in the editor, allowing you to make changes. To save your work, press CTRL + O, and to exit, press CTRL + X.

vim (Vi IMproved)

For those who prefer a more powerful editor, vim is an excellent choice, albeit with a steeper learning curve. To open a file with vim, use:

vim file1.txt

In vim, you can enter insert mode by pressing i, allowing you to edit the file. To save and exit, press ESC, type :wq, and hit Enter.

Mastering these file viewing and editing commands will significantly enhance your productivity in Linux.

System Information Commands: top, htop, df

Monitoring system performance and resource usage is crucial for developers and administrators. Here are some commands that provide valuable system information:

top (Task Manager)

The top command displays real-time information about system processes, including CPU and memory usage. Simply type:

top

This command opens an interactive interface showing active processes. You can sort and filter processes based on various criteria.

You'll see a dynamic screen that updates every few seconds, showing the processes in descending order of CPU usage. Key columns to note include:

  • PID: Process ID.
  • USER: The user running the process.
  • %CPU: The percentage of CPU usage.
  • %MEM: The percentage of memory usage.
  • COMMAND: The command associated with the process.

You can interact with top by pressing keys like k to kill a process or q to exit.

htop (Enhanced Task Manager)

htop is an enhanced version of top, providing a more user-friendly interface and additional features. If it's not installed, you can usually add it via your package manager. To run htop, simply type:

sudo apt install htop  # On Debian-based systems
htop

This command allows you to navigate processes with the arrow keys and easily kill processes by selecting them.

df (Disk Free)

The df command is essential for monitoring disk space usage. It provides a report on the file system disk space usage for all mounted filesystems. To use df, type:

df -h

The -h flag stands for "human-readable", which formats the sizes in KB, MB, or GB, making them easier to comprehend. Key columns in the output include:

  • Filesystem: The name of the filesystem.
  • Size: Total size of the filesystem.
  • Used: Amount of space used.
  • Avail: Amount of space available.
  • Use%: Percentage of space used.
  • Mounted on: Directory where the filesystem is mounted.

Utilizing df effectively allows you to manage disk resources, ensuring that you have sufficient space for your applications and data.

Networking Commands: ping, ifconfig, curl

Networking is an essential skill for developers, and Linux provides several commands to manage and troubleshoot network connections. Key commands include ping, ifconfig, and curl.

ping (Networking Utility)

The ping command tests connectivity between your system and another host. To use it, type:

ping example.com

This command sends packets to the specified address and reports the response time, allowing you to diagnose network issues like latency or packet loss.

ifconfig (Short for Interface Config)

Although ifconfig is being gradually replaced by the ip command, it remains widely used for displaying and configuring network interfaces. To see your current network settings, run:

ifconfig

This command provides information about all active network interfaces, including IP addresses and status.

curl (Client URL)

The curl command is a versatile tool for transferring data over various protocols, including HTTP and FTP. You can use curl to fetch data from a web server:

curl http://example.com

For API interactions, curl can send various types of requests. For example, to make a POST request, you can use:

curl -X POST -d "param1=value1&param2=value2" http://example.com/api

This flexibility makes curl an indispensable tool for developers working with web services and APIs.

Summary

In this article, we explored a variety of commonly used Linux commands that are essential for intermediate and professional developers. We covered how to navigate the file system with commands like cd, ls, and pwd, manipulated files using cp, mv, and rm, and viewed and edited files with cat, nano, and vim. Additionally, we discussed system information commands such as top, htop, and df, along with networking commands like ping, ifconfig, and curl.

By mastering these commands, you can significantly enhance your efficiency and productivity in a Linux environment, allowing you to focus on the creative aspects of development rather than being bogged down by routine tasks. 

Last Update: 20 Jan, 2025

Topics:
Linux