DEV Community

Puru
Puru

Posted on • Edited on

Linux Commands for Developers

Alt Text


Promotion: Organize your Gmail labels as tabs

GitHub logo tuladhar / gmail-labels-as-tabs

An open-source chrome extension to organize your Gmail labels as tabs.


An introduction to basic Linux commands for developers

Linux is an open-source operating system that powers Android phone, public cloud, smart TV, IoT devices, satellites — it’s everywhere; from your smart phones to Large Hadron Collider (LHC).

Linus Torvalds is the creator of the Linux kernel. He also created the distributed version control system “Git” that developers use every day.

Knowing how to use Linux commands is an essential skill for developers to have.

TABLE OF CONTENT

  1. Setup Playground

  2. Comment

  3. Package Manager

  4. Directory Navigation

  5. System Information

  6. Hardware Information

  7. System Monitoring, Statistics & Debugging

  8. File and Directory

  9. Process Management

  10. File Permissions

  11. Networking

  12. Text Search

  13. Disk Usage

  14. Pipes and Redirection

  15. Environment Variable


Setup Playground

To learn and try out the Linux commands, it’s safe to run it inside a Linux container. Docker is a popular choice for the running containers, if you do not have it installed, go ahead and install it from the link below.

Get Docker

Once, Docker is installed and running. Go ahead and run the following command to start a Ubuntu docker container:

$ docker pull ubuntu

$ docker run --rm -it ubuntu bash
root@e4675284d809:/#
Enter fullscreen mode Exit fullscreen mode

And inside the container, run the following commands to restores content and packages that are found on a default Ubuntu in order to make it more suitable for interactive and learning use.

# Type 'y' when prompted
root@e4675284d809:/# unminimize
Enter fullscreen mode Exit fullscreen mode

Now, install a program called tmux for running multiple shell sessions in a single terminal window.

$ apt update
$ apt install tmux
Enter fullscreen mode Exit fullscreen mode

Start tmux by running tmux command

$ tmux
Enter fullscreen mode Exit fullscreen mode

TMUX in a nutshell

  • Start new tabbed window: Press Ctrl+b, release and then press c

  • Start new window horizontally: Press Ctrl+b, release and press "

  • Start new window vertically: Press Ctrl+b, release and press %

  • Switch between tabbed window: Press Ctrl+b, release and press p or n

  • Switch between horizontal or vertical window: Press Ctrl+b, release and then press ;

  • Kill a window: Press Ctrl+b, release and then press x

  • Rename a tabbed window: Press Ctrl+b, release and then press ,

  • Scroll through window console output: Press Ctrl+b, release and then use arrows to move


Comment

Comment in Linux command-line starts with #

$ # This is a comment
Enter fullscreen mode Exit fullscreen mode

Package Manager

apt is a package manager **to manage packages in **Ubuntu Linux.

Update the packages repository

$ apt update
Enter fullscreen mode Exit fullscreen mode

Upgrade packages in bulk

$ apt
Enter fullscreen mode Exit fullscreen mode

Search for a package named htop

$ apt search htop
Enter fullscreen mode Exit fullscreen mode

Show information about a package

$ apt show htop
Enter fullscreen mode Exit fullscreen mode

Install a package named htop

$ apt install htop
Enter fullscreen mode Exit fullscreen mode

Remove a package named htop

$ apt remove htop
Enter fullscreen mode Exit fullscreen mode

Install multiple packages

$ apt install htop less
Enter fullscreen mode Exit fullscreen mode

apt-file is a program to search for packages containing files. Very helpful if you do not remember the name of the package, but you know the command.

Install apt-file using apt

$ apt install apt-file
Enter fullscreen mode Exit fullscreen mode

Update the apt-file cache

$ apt-file update
Enter fullscreen mode Exit fullscreen mode

Search for a package that provides postgres command

$ apt-file search bin/psql
Enter fullscreen mode Exit fullscreen mode

Directory Navigation

Change to /home directory

$ cd /home
Enter fullscreen mode Exit fullscreen mode

Change to the previous directory

$ cd -
Enter fullscreen mode Exit fullscreen mode

Go up one level of the directory tree

$ cd ..
Enter fullscreen mode Exit fullscreen mode

Print’s current directory you are in

$ pwd
Enter fullscreen mode Exit fullscreen mode

System Information

Display Linux kernel information

$ uname -a
Enter fullscreen mode Exit fullscreen mode

Display kernel release information

$ uname -r
Enter fullscreen mode Exit fullscreen mode

Show how long the system has been running + load

$ uptime
Enter fullscreen mode Exit fullscreen mode

Show system hostname

$ hostname
Enter fullscreen mode Exit fullscreen mode

Display the IP addresses of the host

$ hostname -I
Enter fullscreen mode Exit fullscreen mode

Show system reboot history

$ last reboot
Enter fullscreen mode Exit fullscreen mode

Show the current date and time

$ date
Enter fullscreen mode Exit fullscreen mode

Show this month’s calendar

$ cal
Enter fullscreen mode Exit fullscreen mode

Display who is online

$ w
Enter fullscreen mode Exit fullscreen mode

Who you are logged in as

$ whoami
$ id
Enter fullscreen mode Exit fullscreen mode

Hardware Information

Display CPU information

$ cat /proc/cpuinfo
Enter fullscreen mode Exit fullscreen mode

Display number of CPU cores

$ nproc
Enter fullscreen mode Exit fullscreen mode

Display memory information

$ cat /proc/meminfo
Enter fullscreen mode Exit fullscreen mode

Display environment variables of a process, e.g: PID 1

$ cat /proc/1/environ
Enter fullscreen mode Exit fullscreen mode

Display free and used memory ( -h for human-readable, -m for MB, -g for GB.)

$ free -h
Enter fullscreen mode Exit fullscreen mode

System Monitoring, Statistics, Debugging

Display and manage the running processes

$ top
Enter fullscreen mode Exit fullscreen mode

Install and use a friendly interactive process viewer (alternative to top)

$ apt install htop
$ htop
Enter fullscreen mode Exit fullscreen mode

Display processor related statistics (refresh every 1 second)

$ mpstat 1
Enter fullscreen mode Exit fullscreen mode

NOTE: If you encounter following error: /usr/bin/mpstat: No such file or directory. Search and install the package that provides mpstat tool.*

$ apt-file search bin/mpstat
sysstat: /usr/bin/mpstat

$ apt install sysstat
Enter fullscreen mode Exit fullscreen mode

Display virtual memory statistics (refresh every 1 second)

$ vmstat 1
Enter fullscreen mode Exit fullscreen mode

Display disk I/O statistics (refresh every 1 second)

$ iostat 1
Enter fullscreen mode Exit fullscreen mode

List all open files on the system

$ lsof
Enter fullscreen mode Exit fullscreen mode

List files opened by the user (e.g: root)

$ lsof -u USER
Enter fullscreen mode Exit fullscreen mode

List files opened by a certain process with PID (e.g: 1)

$ lsof -p PID
Enter fullscreen mode Exit fullscreen mode

Display disk space occupied by current directory ( -h for human-readable, -s summarize)

$ du -sh
Enter fullscreen mode Exit fullscreen mode

Execute “df -h”, showing periodic updates every 1 second (pro tip: -d flag shows visual updates)

$ watch -n1 df -h
Enter fullscreen mode Exit fullscreen mode

File and Directory

List all files (including hidden) in a long listing human-readable format in the current directory (specifying . is optional).

$ ls -hal .
Enter fullscreen mode Exit fullscreen mode

Display the present working directory

$ pwd
Enter fullscreen mode Exit fullscreen mode

Create one or more new empty file

$ touch file1 file2
Enter fullscreen mode Exit fullscreen mode

Create a new directory

$ mkdir dir1
Enter fullscreen mode Exit fullscreen mode

Create a directory tree using -p option

$ mkdir -p dir1/dir2/dir3
Enter fullscreen mode Exit fullscreen mode

List the directory tree using tree command

$ tree dir1
Enter fullscreen mode Exit fullscreen mode

NOTE: Install tree package, if you encounter the following error:
bash: tree: command not found*

$ apt install tree
Enter fullscreen mode Exit fullscreen mode

Copy (duplicate) file(s) from one directory to another (-v option for enabling verbose mode)

$ cp -v file1 dir1/file1-copy
Enter fullscreen mode Exit fullscreen mode

Copy directory and all it’s content to a new directory

$ cp -vr dir1 dir1-copy
Enter fullscreen mode Exit fullscreen mode

Rename or move a file. If file2 is a directory, then file1 into moved into that directory

$ mv -v file1 file1-rename
$ mv -v file1-rename dir1
Enter fullscreen mode Exit fullscreen mode

Remove a file or empty directory (-f option force deletes without asking)

$ rm file1
Enter fullscreen mode Exit fullscreen mode

Remove a directory and its contents recursively (-v option for enabling verbose mode)

$ rm -vr dir1
Enter fullscreen mode Exit fullscreen mode

Create a symbolic link (pointer) to a file or directory

$ ln -s file1 file1-link
Enter fullscreen mode Exit fullscreen mode

Write a simple text to a file

echo "hello, world!" > hello.txt
Enter fullscreen mode Exit fullscreen mode

View the contents of a file

$ cat hello.txt
Enter fullscreen mode Exit fullscreen mode

Paginate through a large file

$ less hello.txt
Enter fullscreen mode Exit fullscreen mode

Display the first 20 lines of a file

$ head -n 20 hello.txt
Enter fullscreen mode Exit fullscreen mode

Display the last 20 lines of a file

$ tail -n 20 hello.txt
Enter fullscreen mode Exit fullscreen mode

Display the last 10 lines of a file and follow the file as it updated.

$ tail -f hello.txt
Enter fullscreen mode Exit fullscreen mode

Process Management

A process is a running instance of a program.

Display your currently running processes

$ ps
Enter fullscreen mode Exit fullscreen mode

Display every process on the system.

$ ps auxf
Enter fullscreen mode Exit fullscreen mode

Display process information for the process name

$ ps uf -C processname
Enter fullscreen mode Exit fullscreen mode

Display interactive real-time view of running processes

$ top
$ htop
Enter fullscreen mode Exit fullscreen mode

Look-up process ID based on a name

pgrep nginx
Enter fullscreen mode Exit fullscreen mode

Kill a process with a given process ID. By default TERM signal is sent

$ kill PID
Enter fullscreen mode Exit fullscreen mode

Send a custom signal to a process with given process ID

$ kill -s SIGNAL_NUMBER pid
Enter fullscreen mode Exit fullscreen mode

List all available signals

$ kill -l
Enter fullscreen mode Exit fullscreen mode

Kill a process based on a name

$ pkill nginx
Enter fullscreen mode Exit fullscreen mode

Run a command as a background job

$ (sleep 30; echo "woke up after 30 seconds") &
Enter fullscreen mode Exit fullscreen mode

List background jobs

$ jobs
Enter fullscreen mode Exit fullscreen mode

Display stopped or background jobs

$ bg
Enter fullscreen mode Exit fullscreen mode

Brings the most recent background job to the foreground

$ fg
Enter fullscreen mode Exit fullscreen mode

Brings job N to the foreground

$ fg N
Enter fullscreen mode Exit fullscreen mode

Kill job N

$ kill %N
Enter fullscreen mode Exit fullscreen mode

File Permissions

Give all permission to the owner, read execute to the group and nothing to others

# Create a file
$ touch file1

# Set permission using either of the method
$ chmod 750 file1
$ chmod u=rwx,g=rx,o= file1

# List the file permission
$ ls -lh file1
Enter fullscreen mode Exit fullscreen mode

Change ownership of a file or directory to a given user and group

$ chown user:group file1
Enter fullscreen mode Exit fullscreen mode

Networking

Display information of all available network interfaces

$ ip addr
$ ifconfig -a          # deprecated
Enter fullscreen mode Exit fullscreen mode

Display information of eth0 interface

$ ip addr show eth0
$ ifconfig eth0        # deprecated
Enter fullscreen mode Exit fullscreen mode

Display IP routing table

$ ip route
$ route                # deprecated
Enter fullscreen mode Exit fullscreen mode

Ping a hostname or IP address

$ ping google.com
$ ping 8.8.8.8
Enter fullscreen mode Exit fullscreen mode

Display registration information of a domain

$ whois medium.com
Enter fullscreen mode Exit fullscreen mode

DNS lookup a domain

$ dig medium.com A     # IPv4 addresses
$ dig medium.com AAAA  # IPv6 addresses
$ dig medium.com NX    # Nameservers

$ host medium.com     # IPv4 addresses
Enter fullscreen mode Exit fullscreen mode

Display hostname and IP address of the local machine

$ hostname
$ hostname -i
Enter fullscreen mode Exit fullscreen mode

Download files from a remote HTTP server

$ wget [http://ipv4.download.thinkbroadband.com/5MB.zip](http://ipv4.download.thinkbroadband.com/5MB.zip)
$ curl --output 5MB.zip [http://ipv4.download.thinkbroadband.com/5MB.zip](http://ipv4.download.thinkbroadband.com/5MB.zip)
Enter fullscreen mode Exit fullscreen mode

Display all process listening on TCP or UDP ports

$ netstat -plunt
$ lsof -i
$ lsof -i tcp     # only TCP ports
Enter fullscreen mode Exit fullscreen mode

Text Search

Search for a pattern in a text file

$ grep pattern file

# For example:
$ grep root /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Search recursively for a pattern in a text file inside a directory

$ grep -R "/bin/bash" /etc
Enter fullscreen mode Exit fullscreen mode

Search for pattern and output N lines before (B) or after (A) pattern match

$ grep -B 5 root /etc/passwd
$ grep -A 3 root /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Find files within a directory with a matching filename

find /etc -iname 'passwd'
find /etc -iname 'pass*'  # glob pattern
Enter fullscreen mode Exit fullscreen mode

Find files based on filesize

find / -size +1M #  larger than 1MB
find / -size -1M # smaller than 1MB
Enter fullscreen mode Exit fullscreen mode

Disk Usage

Show free and used space of disk storages

df -h
Enter fullscreen mode Exit fullscreen mode

Show disk space consumed by a directory or file

du -sh /var/log
du -h 5MB.zip
Enter fullscreen mode Exit fullscreen mode

Interactive disk usage explorer

apt install ncdu
ncdu
Enter fullscreen mode Exit fullscreen mode

Pipes and Redirection


REDIRECTION

Redirect normal output (stdout) from a command to a file

echo "hello" > hello.stdout.txt
echo "world" > hello.stdout.txt
Enter fullscreen mode Exit fullscreen mode

Redirect error output (stderr) from a command to a file

cat somefile 2> cat.stderr.txt
Enter fullscreen mode Exit fullscreen mode

Redirect both normal and error output from a command to a file. Useful for logging.

ps auxf >& processes.txt
Enter fullscreen mode Exit fullscreen mode

Append normal output (stdout) from a command to a file unlike > which overwrites the file

echo "hello" >> hello2.stdout.txt
echo "world! >> hello2.stdout.txt
Enter fullscreen mode Exit fullscreen mode

Append error output (stderr) from a command to a file

cat some-unknown-file 2>> cat2.stderr.txt
Enter fullscreen mode Exit fullscreen mode

Append both normal and error output (stderr) from a command to a file

ps auxf &>> processes.txt
Enter fullscreen mode Exit fullscreen mode

PIPES

The shell pipe **is a way to **communicate between commands.

Create a dummy file to learn to pipe

mkdir pipes-example
cd pipes-example
touch {1..10}.txt
Enter fullscreen mode Exit fullscreen mode

Example 1: Let’s use sort command

ls -1 *.txt | sort -n    # sorts the output in ASC order
ls -1 *.txt | sort -nr   # sorts the output in DESC order
Enter fullscreen mode Exit fullscreen mode

Example 2: Let’s use head & tail command

ls -1 *.txt | sort -n | head -n 5  # show the first 5 lines
ls -1 *.txt | sort -n | tail -n 5  # show the last 5 lines
Enter fullscreen mode Exit fullscreen mode

Example 3: Search for a pattern in a text file

cat /etc/passwd | grep root    # show lines containing string 'root'
Enter fullscreen mode Exit fullscreen mode

Environment Variables

List all environment variables

$ env
Enter fullscreen mode Exit fullscreen mode

Display value of an environment variable

echo $HOME
echo $SHELL
Enter fullscreen mode Exit fullscreen mode

Create an environment variable

export PORT=80
export PLATFORM=medium.com
Enter fullscreen mode Exit fullscreen mode

Delete an environment variable

unset PORT
Enter fullscreen mode Exit fullscreen mode

PATH is one of the common and important environment variables. What do you think will happen if you unset it?

$ echo $PATH
$ unset PATH
Enter fullscreen mode Exit fullscreen mode

… and that’s it!

Every Linux command can be an encyclopedia of options on itself. If you want to dig deeper into all available options, author, examples, then use man command e.g: man htop or man man to learn about man itself.

Top comments (11)

Collapse
 
kevinschweikert profile image
Kevin Schweikert

Instead of ifconfig -a it's better to use the newer command ip a

Collapse
 
ptuladhar3 profile image
Puru

That's right Kevin. I've updated with ip command. Thanks!

Collapse
 
peter279k profile image
peter279k • Edited

ifconfig command is too old and it should use ip address command instead.

Collapse
 
pahosler profile image
pahosler

ip is useful, but ifconfig is much more readable not to mention after decades of use I tend to use it reflexively.

Collapse
 
cocoonkid profile image
cocoonkid

same here but ifconfig does omit certain informations that ip shows you. I force myself to switch too :-)

Collapse
 
peter279k profile image
peter279k

Just notice that the nmcli command can be added on Networking section.

It can use this to check current network connection, modify network connection information, activate/inactivate specific network connection info and so on :).

Collapse
 
walkingwithcode profile image
Ian

A great list! I've started using Docker for projects recently, and this will come in handy.

I've spotted a small typo though:


Remove a package named htop

$ apt remove less

Collapse
 
ptuladhar3 profile image
Puru

Thanks Ian. Typo fixed 😊

Collapse
 
suckup_de profile image
Lars Moelleken


strace -s 2048 -p <PID>

or

strace -s 2048 <command>

is also very helpful sometimes ;)

Collapse
 
omarkhatib profile image
Omar

Nice bro , you got them all in one place.

Collapse
 
ardianta profile image
Dian

Thank you, it very useful for newbies