Top 30 Linux Command
Top Linux Command

Top 30 Linux Command

As a software engineer, navigating the Linux command line is an invaluable skill. Linux provides a powerful environment for development, system administration, and more. In this article, we'll explore 30 essential Linux commands that software engineers should be familiar with, complete with practical examples to illustrate their usage.

1. ls - List Files

List files and directories in the current directory.

Example:

ls [/directory/folder/path]        

2. cd - Change Directory

Change the current working directory.

Example:

cd /path/to/directory        

Here are some navigation shortcuts:

  • cd ~[username] – goes to another user’s home directory.
  • cd .. – moves one directory up.
  • cd- – switches to the previous directory.


3. pwd - Print Working Directory

Display the current directory's full path.

Example:

pwd        

4. mkdir - Make Directory

Create a new directory.

Example:

mkdir [option] new_directory        

Here are several common mkdir command options:

  • -p – creates a directory between two existing folders. For example, mkdir - p Music/2023/Songs creates a new 2023 directory.
  • -m – sets the folder permissions. For instance, enter mkdir -m777 directory to create a directory with read, write, and execute permissions for all users.
  • -v – prints a message for each created directory.

5. touch - Create Empty File

Create a new empty file.

Example:

touch new_file.txt        

6. cp - Copy Files and Directories

Copy files or directories from one location to another.

Example:

  • Copying one file from the current directory to another folder. Specify the file name and target path:

cp filename.txt /home/username/Documents        

  • Duplicating an entire directory. Pass the -R flag followed by the source and destination directory:

cp -R /home/username/Documents /home/username/Documents_backup        

7. rm - Remove Files and Directories

Remove files or directories (use with caution).

Example:

rm [option] unwanted_file.txt        

To modify the command, add the following options:

  • -i – prompts a confirmation before deletion.
  • -f – allows file removal without a confirmation.
  • -r – deletes files and directories recursively.

Note: Use the rm command with caution since deletion is irreversible. Avoid using the -r and -f options since they may wipe all your files. Always add the -i option to avoid accidental deletion.

8. rmdir - Remove Directories

Use the rmdir command to delete an empty directory in Linux.

rmdir [option] directory_name        

If the folder contains a subdirectory, the command will return an error. To force delete a non-empty directory, use the -p option.

9. touch - Create Empty File

Create a new empty file.

Example:

touch new_file.txt        

10. mv - Move or Rename Files

Move or rename files and directories.

Example (move):

mv filename.txt /home/username/Documents        

Example (rename):

mv old_filename.txt new_filename.txt        

11. cat - Concatenate and Display File Content

Display the contents of a file.

Example:

cat filename.txt        

There are various ways to use the cat command:

  • cat > filen.txt creates a new file.
  • cat file1.txt file2.txt > file3.txt merges file1.txt with file2.txt and stores the output in file3.txt.
  • tac file.txt – displays content in reverse order.

12. grep - Search Text

The global regular expression or grep command lets you find a word by searching the content of a file.

Example:

grep blue notepad.txt        

13. head and tail - Display Beginning/End of File

Display the first or last lines of a file.

Example (head):

head file.txt  # Display first 5 lines        

Example (tail):

tail file.txt # Display last 5 lines        

14. chmod - Change File Permissions

Change file permissions (read, write, execute).

Example:

chmod 755 script.sh          

Read(1), write(2), and execute(4) for owner (1+2+4=7); read(1) and execute(4) for group(1+4=5) and others(1+4=5)

15. chown - Change File Owner

Change the owner of a file.

Example:

chown new_owner:group file.txt        

16. du - Disk Usage of Directories

Display disk usage of directories and files.

Example:

du -sh /path/to/directory        

The du command has several options, such as:

  • -s shows the specified folder’s total size.
  • -m provides folder and file information in MB.
  • -k – displays information in KB.
  • -h informs the displayed folders and files’ last modification date.

17. zip, unzip - Compress and Decompress Files

The zip command lets you compress items into a ZIP file with the optimal compression ratio. Here’s the syntax:

zip [options] zipfile file1 file2….        

For example, this command compresses note.txt into archive.zip in the current working directory:

zip archive.zip note.txt        

Use the unzip command to extract the compressed file. Here’s the syntax:

unzip file_name.zip        

18. ps - Process Status

List running processes.

Example:

ps [option]        

The ps command accepts several options, including:

  • -T – displays all processes associated with the current shell session.
  • -u username – lists processes associated with a specific user.
  • -A – shows all the running processes.

19. kill - Terminate Processes

Terminate processes by their process ID (PID).

Example:

kill [PID]        

20. uname - Unix Name

The uname or unix name command prints information about your machine, including its hardware, system name, and Linux kernel.

Example:

uname [option]        

While you can use it without an option, add the following to modify the command:

  • -a – prints all the system information.
  • -s – outputs the kernel name.
  • -n – shows the system’s node hostname.

21. sudo - Run with special privilege

Superuser do or sudo is one of the most basic commands in Linux. It runs your command with administrative or root permissions.

Example:

sudo useradd username        

You can also add an option, such as:

  • -k – invalidates the timestamp file.
  • -g – executes commands as a specified group name or ID.
  • -h – runs commands on the host.

22. Useradd, Userdel commands

Use useradd to create a new Linux user account and change its password with the passwd command.

Example:

useradd [option] username        
passwd username        

Both the useradd and passwd commands require sudo privileges. To delete a user, use the userdel command:

userdel username        

23. ping - Check Server Availability

It lets you check whether a network or server is reachable, which is useful for troubleshooting connectivity issues.

Example:

ping www.google.com        

24. wget - Download Files

Use the wget command to download files from the internet using HTTP, HTTPS, or FTP protocols. Here’s the syntax:

wget [option] [url]        

For example, enter the following to download the latest version of WordPress:

wget https://wordpress.org/latest.zip        

25. curl - Retrieve Content

The curl command transfers data between servers. Its common usage is for retrieving a web page’s content to your system using its URL. Here’s the syntax:

curl [option] URL        

However, you can add various options to modify the curl command behavior for other tasks:

  • -o or -O – downloads files from a URL.
  • -X – changes the default HTTP GET method.
  • -H – sends a custom header to the URL.
  • -F – uploads a file to the specified destination.

26. scp - Copy Files on server

The scp command securely copies files or directories between systems over a network.

Example:

scp [option] [source username@IP]:/[directory and file name] [destination username@IP]:/[destination directory]        

For a local machine, omit the hostname and IP address. Use the following options to modify the copying behavior:

  • -P – changes the port for copying. The default is 22.
  • -l – limits the scp command’s bandwidth.
  • -C – compresses transferred data to make it smaller.

27. rsync - Sync Files

The rsync command lets you sync files or folders between two destinations to ensure they have the same content. Here’s the syntax:

rsync [options] source destination        

If your destination or source is a folder, enter the directory path like /home/directory/path. To sync a remote server, use its hostname and IP address, like host@185.185.185.185.

This command has various options:

  • -a – enables archive mode to preserve file permissions, dates, and other attributes.
  • -v – shows visual information about the transferred file.
  • -z – compresses the transferred file data to reduce their size.

28. ifconfig command

The ifconfig command lets you list and configure your system’s network interface. In newer Linux distros, it is equivalent to the ip command.

Example:

ifconfig [interface] [option]        

Running it without arguments displays information about all network interfaces in your system. To check a specific interface, add its name as an argument without an option. For a more specific task, use the following options:

  • s – summarizes the network interfaces and their configuration. This option goes before the interface name.
  • up and down – enables and disables a network interface.
  • inet and inet6 – assigns an IPv4 and IPv6 address to a network interface.
  • netmask – specifies the subnet mask to use with an IPv4 address.

29. netstat command

The netstat command is used to display your system’s network information, like sockets and routing. Here’s the command syntax:

netstat [option]        

Use various options to modify the displayed information. Some common ones are:

  • -a – displays listening and closed sockets.
  • -t – shows TCP connections.
  • -u – lists UDP connections.
  • -r – displays routing tables.
  • -i – shows information about network interfaces.
  • -p – lists programs’ names and process IDs.
  • -c – continuously outputs network information for real-time monitoring.

30. dig command

The dig or domain information groper command gathers DNS data from a domain. Unlike nslookup, it is more detailed and versatile. Here’s the syntax:

dig [option] target [query_type]        

Replace target with a domain name. By default, this command only shows A record type. Change query_type to check a specific type or use ANY to query all of them. To run a reverse DNS lookup, add the -x option and use the IP address as the target.

Conclusion

These 30 Linux commands are just the tip of the iceberg in the Linux command-line world. Mastering these commands will empower software engineers to efficiently navigate, manage, and develop in a Linux environment. As you delve deeper into Linux, you'll discover many more commands and options to streamline your development workflow and system administration tasks. Happy coding!


To view or add a comment, sign in

More articles by Satya Narayan Yadav

Others also viewed

Explore content categories