Introduction
Linux is one of the most popular platforms for developers due to its versatility, performance, and open-source nature. It is possible to significantly increase productivity and efficiency in development tasks by mastering Linux commands. There are several sections in this guide that cover essential Linux commands that developers should know, so they can be understood and applied more easily.
Basic Commands
ls
The ls
command lists the contents of a directory.
ls
ls -l
ls -a
-
ls -l:
Lists files in long format. -
ls -a:
Includes hidden files.
cd
The cd
command changes the current directory.
cd /path/to/directory
cd ..
-
cd ..
Moves up one directory level.
pwd
The pwd
command prints the current working directory.
pwd
man
The man
command displays the manual pages for other commands.
man ls
mkdir
The mkdir
command creates a new directory.
mkdir new_directory
rm
The rm
command removes files or directories.
rm filename
rm -r directory_name
-
rm -r
: Recursively removes a directory and its contents.
File Management
cp
The cp
command copies files or directories.
cp source_file destination_file
cp -r source_directory destination_directory
-
cp -r
: Recursively copies a directory and its contents.
mv
The mv
command moves or renames files or directories.
mv old_name new_name
mv file_name /path/to/destination
touch
The touch
command creates an empty file or updates the timestamp of an existing file.
touch newfile.txt
cat
The cat
command concatenates and displays the content of files.
cat file.txt
nano and vimnano and vim
are text editors. nano is easier for beginners, while vim is more powerful but has a steeper learning curve.
nano file.txt
vim file.txt
Process Management
ps
The ps
command displays information about active processes.
ps
ps aux
-
ps aux
: Shows detailed information about all running processes.
top
Thetop
command provides a dynamic view of system processes.
top
kill
The kill command terminates a process by its PID.
kill PID
kill -9 PID
- kill -9: Forcefully terminates a process.
htophtop
is an interactive process viewer, providing a more user-friendly interface compared to top.
htop
Networking
ping
The ping
command checks the network connectivity between the local machine and a remote host.
ping test.com
curl
The curl
command transfers data from or to a server.
curl http://test.com
curl -O http://test.com/file.txt
-
curl -O
: Downloads a file.
wget
The wget
command downloads files from the internet.
wget http://test.com/file.txt
ssh
The ssh
command connects to a remote machine over SSH.
ssh user@remote_host
scp
The scp
command securely copies files between hosts.
scp local_file user@remote_host:/path/to/destination
scp user@remote_host:/path/to/file local_destination
System Monitoring
df
The df
command displays disk space usage.
df -h
-
df -h
: Shows disk space in human-readable format.
du
The du
command estimates file and directory space usage.
du -sh directory_name
-
du -sh
: Shows the size of a directory in human-readable format.
free
The free
command displays memory usage.
free -h
-
free -h
: Shows memory usage in human-readable format.
uptime
The uptime
command shows how long the system has been running.
uptime
Text Processing
grep
The grep
command searches for patterns in files.
grep 'pattern' file.txt
grep -r 'pattern' directory_name
-
grep -r
: Recursively searches in directories.
sed
The sed
command is a stream editor for filtering and transforming text.
sed 's/old/new/g' file.txt
-
s/old/new/g:
Replaces all occurrences of ‘old’ with ‘new’ in the file.
awk
The awk
command is a powerful text processing language.
awk '{print $1}' file.txt
-
{print $1}:
Prints the first field of each line.
Version Control
gitgit
is a distributed version control system.
git init
git clone repository_url
git add .
git commit -m "commit message"
git push origin branch_name
git pull origin branch_name
-
git init:
Initializes a new Git repository. -
git clone:
Clones a repository. -
git add .:
Adds all changes to the staging area. -
git commit -m:
Commits changes with a message. -
git push:
Pushes changes to the remote repository. -
git pull:
Pulls changes from the remote repository.
Package Management
aptapt
is a package management tool for Debian-based systems.
sudo apt update
sudo apt install package_name
sudo apt upgrade
-
sudo apt update
: Updates the package list. -
sudo apt install
: Installs a package. -
sudo apt upgrade
: Upgrades all installed packages.
yum
yum
is a package management tool for RPM-based systems.
sudo yum update
sudo yum install package_name
sudo yum upgrade
Scripting
bashbash
scripts automate tasks and streamline workflows.
#!/bin/bash
# This is a comment
echo "Hello, World!"
-
#!/bin/bash:
Indicates the script should be run in the Bash shell. -
echo:
Prints text to the terminal.
cron
cron
schedules scripts and commands to run at specified times.
crontab -e
-
crontab -e:
Edits the crontab file to schedule tasks.
Example of a cron job that runs a script every day at midnight:
0 0 * * * /path/to/script.sh
Conclusion
By mastering these Linux commands, a developer can significantly improve the efficiency with which he or she manages files, processes, networks, and more. For effective and productive development in a Linux environment, understanding these commands is essential, whether you are a beginner or an experienced developer. The more you practice and use these commands, the easier it will be for them to become solidified and integrated into your daily routine.