DEV Community

Cover image for Basic Linux Commands for Developers
Bitlearners
Bitlearners

Posted on

Basic Linux Commands for Developers

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
Enter fullscreen mode Exit fullscreen mode
  • 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 ..
Enter fullscreen mode Exit fullscreen mode
  • cd .. Moves up one directory level.

pwd
The pwdcommand prints the current working directory.

pwd

Enter fullscreen mode Exit fullscreen mode

man
The man command displays the manual pages for other commands.

man ls

Enter fullscreen mode Exit fullscreen mode

mkdir
The mkdir command creates a new directory.

mkdir new_directory
Enter fullscreen mode Exit fullscreen mode

rm
The rm command removes files or directories.

rm filename
rm -r directory_name
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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

Enter fullscreen mode Exit fullscreen mode

touch
The touch command creates an empty file or updates the timestamp of an existing file.

touch newfile.txt
Enter fullscreen mode Exit fullscreen mode

cat
The cat command concatenates and displays the content of files.

cat file.txt
Enter fullscreen mode Exit fullscreen mode

nano and vim
nano 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
Enter fullscreen mode Exit fullscreen mode

Process Management

ps
The ps command displays information about active processes.

ps
ps aux
Enter fullscreen mode Exit fullscreen mode
  • ps aux: Shows detailed information about all running processes. top The top command provides a dynamic view of system processes.
top
Enter fullscreen mode Exit fullscreen mode

kill
The kill command terminates a process by its PID.

kill PID
kill -9 PID
Enter fullscreen mode Exit fullscreen mode
  • kill -9: Forcefully terminates a process.

htop
htop is an interactive process viewer, providing a more user-friendly interface compared to top.

htop
Enter fullscreen mode Exit fullscreen mode

Networking

ping
The ping command checks the network connectivity between the local machine and a remote host.

ping test.com
Enter fullscreen mode Exit fullscreen mode

curl
The curl command transfers data from or to a server.

curl http://test.com
curl -O http://test.com/file.txt
Enter fullscreen mode Exit fullscreen mode
  • curl -O: Downloads a file.

wget
The wget command downloads files from the internet.

wget http://test.com/file.txt
Enter fullscreen mode Exit fullscreen mode

ssh
The ssh command connects to a remote machine over SSH.

ssh user@remote_host
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

System Monitoring

df

The dfcommand displays disk space usage.

df -h
Enter fullscreen mode Exit fullscreen mode
  • df -h: Shows disk space in human-readable format.

du
The du command estimates file and directory space usage.

du -sh directory_name
Enter fullscreen mode Exit fullscreen mode
  • du -sh: Shows the size of a directory in human-readable format.

free
The free command displays memory usage.

free -h
Enter fullscreen mode Exit fullscreen mode
  • free -h: Shows memory usage in human-readable format.

uptime
The uptime command shows how long the system has been running.

uptime
Enter fullscreen mode Exit fullscreen mode

Text Processing

grep
The grep command searches for patterns in files.

grep 'pattern' file.txt
grep -r 'pattern' directory_name
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • s/old/new/g: Replaces all occurrences of 'old' with 'new' in the file.

awk
The awkcommand is a powerful text processing language.

awk '{print $1}' file.txt
Enter fullscreen mode Exit fullscreen mode
  • {print $1}: Prints the first field of each line.

Version Control

git
git 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
Enter fullscreen mode Exit fullscreen mode
  • 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

apt
aptis a package management tool for Debian-based systems.

sudo apt update
sudo apt install package_name
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode
  • sudo apt update: Updates the package list.
  • sudo apt install: Installs a package.
  • sudo apt upgrade: Upgrades all installed packages. yum yumis a package management tool for RPM-based systems.
sudo yum update
sudo yum install package_name
sudo yum upgrade
Enter fullscreen mode Exit fullscreen mode

Scripting

bash
bashscripts automate tasks and streamline workflows.

#!/bin/bash
# This is a comment
echo "Hello, World!"
Enter fullscreen mode Exit fullscreen mode
  • #!/bin/bash: Indicates the script should be run in the Bash shell.
  • echo: Prints text to the terminal. cron cronschedules scripts and commands to run at specified times.
crontab -e
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)