MEHDI.
RETURN_TO_INDEX

Linux Commands Every Developer Should Know by Heart

5 min read
#DevOps#Linux#Terminal#Productivity

Introduction

As a developer, I have found that having a solid grasp of Linux commands is crucial for efficient workflow and troubleshooting. In this article, I will cover the essential Linux commands that every developer should know by heart.

File Navigation

File navigation is a fundamental aspect of working with Linux. The following commands are used to navigate through the file system:

  • pwd: prints the current working directory
  • cd: changes the current working directory
  • cd ~: takes you to your home directory
  • cd ..: moves up one directory level
  • cd -: toggles between the last two directories

For example, to navigate to the /etc directory, you can use the following command:

cd /etc

You can also use the ls command to list the files and directories in the current working directory.

Text Processing

Text processing is a critical aspect of working with Linux. The following commands are used to process text:

  • grep: searches for a pattern in a file
  • awk: processes and analyzes text
  • sed: edits text

For example, to search for a pattern in a file, you can use the following command:

grep 'pattern' file.txt

To extract the first column of a CSV file, you can use the following command:

awk -F, '{print $1}' file.csv

To replace a pattern in a file, you can use the following command:

sed 's/old_pattern/new_pattern/g' file.txt

Practical One-Liners

Here are some practical one-liners that use the above commands:

  • grep -r 'pattern' *: searches for a pattern recursively in all files
  • awk -F, '{print $1}' file.csv | sort | uniq: extracts the first column, sorts it, and removes duplicates
  • sed 's/old_pattern/new_pattern/g' file.txt > new_file.txt: replaces a pattern in a file and writes the output to a new file

Process Management

Process management is an essential aspect of working with Linux. The following commands are used to manage processes:

  • ps: displays the current processes
  • kill: kills a process
  • killall: kills all processes with a given name
  • bg: runs a process in the background
  • fg: brings a process to the foreground

For example, to display the current processes, you can use the following command:

ps aux

To kill a process, you can use the following command:

kill process_id

To run a process in the background, you can use the following command:

command &

Practical One-Liners

Here are some practical one-liners that use the above commands:

  • ps aux | grep 'process_name': searches for a process by name
  • killall 'process_name': kills all processes with a given name
  • bg; fg: runs a process in the background and then brings it to the foreground

Networking Basics

Networking basics are an essential aspect of working with Linux. The following commands are used to configure and troubleshoot networks:

  • ifconfig: displays the network interface configuration
  • ping: tests the connection to a host
  • ssh: securely connects to a remote host
  • scp: securely copies files between hosts
  • wget: downloads a file from a URL

For example, to display the network interface configuration, you can use the following command:

ifconfig

To test the connection to a host, you can use the following command:

ping host_name

To securely connect to a remote host, you can use the following command:

ssh user_name@host_name

Practical One-Liners

Here are some practical one-liners that use the above commands:

  • ifconfig | grep 'inet addr': extracts the IP address from the network interface configuration
  • ping -c 1 host_name: tests the connection to a host with a single packet
  • scp file.txt user_name@host_name:/path/to/destination: securely copies a file to a remote host

SSH

SSH is a secure protocol for connecting to remote hosts. The following commands are used to configure and use SSH:

  • ssh-keygen: generates a pair of SSH keys
  • ssh-copy-id: copies the public key to a remote host
  • ssh: securely connects to a remote host

For example, to generate a pair of SSH keys, you can use the following command:

ssh-keygen -t rsa

To copy the public key to a remote host, you can use the following command:

ssh-copy-id user_name@host_name

To securely connect to a remote host, you can use the following command:

ssh user_name@host_name

Practical One-Liners

Here are some practical one-liners that use the above commands:

  • ssh-keygen -t rsa; ssh-copy-id user_name@host_name: generates a pair of SSH keys and copies the public key to a remote host
  • ssh user_name@host_name 'command': executes a command on a remote host

Practical Takeaways

In this article, we have covered the essential Linux commands that every developer should know by heart. By mastering these commands, you can improve your workflow, troubleshoot issues, and work more efficiently. Here are some practical takeaways:

  • Use grep, awk, and sed to process text
  • Use ps, kill, and killall to manage processes
  • Use ifconfig, ping, ssh, and scp to configure and troubleshoot networks
  • Use ssh-keygen, ssh-copy-id, and ssh to securely connect to remote hosts
  • Use practical one-liners to automate tasks and improve your workflow