Linux Commands for Developers
Tags: linux, devops, developer
Published: 2020-08-28 12:14:09

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k3d3wfe5ucem44iqf2bi.png)

- - -

### **Promotion:** Organize your Gmail labels as tabs
{% github tuladhar/gmail-labels-as-tabs no-readme %}

- - -

## An introduction to basic Linux commands for developers

[Linux](https://www.linux.com/what-is-linux/) is an open-source operating system that powers [Android phone](https://en.wikipedia.org/wiki/Android_(operating_system)), [public cloud](https://www.redhat.com/en/resources/state-of-linux-in-public-cloud-for-enterprises), smart TV, IoT devices, satellites — it’s everywhere; from your smart phones to [Large Hadron Collider](https://www.linux.com/topic/open-source/how-cern-using-linux-open-source/) (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](#toc1)

1. [Comment](#toc2)

1. [Package Manager](#toc3)

1. [Directory Navigation](#toc3)

1. [System Information](#toc5)

1. [Hardware Information](#toc6)

1. [System Monitoring, Statistics & Debugging](#toc7)

1. [File and Directory](#toc8)

1. [Process Management](#toc9)

1. [File Permissions](#toc10)

1. [Networking](#toc11)

1. [Text Search](#toc12)

1. [Disk Usage](#toc13)

1. [Pipes and Redirection](#toc14)

1. [Environment Variable](#toc15)

- - -

## Setup Playground <a name='toc1'></a>

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**](https://docs.docker.com/get-docker/)

Once, Docker is installed and running. Go ahead and run the following command to start a [Ubuntu docker](https://hub.docker.com/_/ubuntu) container:

    $ docker pull ubuntu

    $ docker run --rm -it ubuntu bash
    root@e4675284d809:/#

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

Now, install a program called [tmux](https://github.com/tmux/tmux/wiki) for running multiple shell sessions in a single terminal window.

    $ apt update
    $ apt install tmux

Start tmux by running tmux command

    $ tmux

### **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 <a name='toc2'></a>

**Comment** in Linux command-line starts with #

    $ # This is a comment

- - -

## Package Manager <a name='toc3'></a>

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

**Update the packages repository**

    $ apt update

**Upgrade packages in bulk**

    $ apt

**Search for a package named htop**

    $ apt search htop

**Show information about a package**

    $ apt show htop

**Install a package named htop**

    $ apt install htop

**Remove a package named htop**

    $ apt remove htop

**Install multiple packages**

    $ apt install htop less

`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

**Update the apt-file cache**

    $ apt-file update

**Search for a package that provides postgres command**

    $ apt-file search bin/psql

- - -

## Directory Navigation <a name='toc4'></a>

**Change to /home directory**

    $ cd /home

**Change to the previous directory**

    $ cd -

**Go up one level of the directory tree**

    $ cd ..

**Print’s current directory you are in**

    $ pwd

- - -

## System Information <a name='toc5'></a>

**Display Linux kernel information**

    $ uname -a

**Display kernel release information**

    $ uname -r

**Show how long the system has been running + load**

    $ uptime

**Show system hostname**

    $ hostname

**Display the IP addresses of the host**

    $ hostname -I

**Show system reboot history**

    $ last reboot

**Show the current date and time**

    $ date

**Show this month’s calendar**

    $ cal

**Display who is online**

    $ w

**Who you are logged in as**

    $ whoami
    $ id

- - -

## Hardware Information <a name='toc6'></a>

**Display CPU information**

    $ cat /proc/cpuinfo

**Display number of CPU cores**

    $ nproc

**Display memory information**

    $ cat /proc/meminfo

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

    $ cat /proc/1/environ

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

    $ free -h

- - -

## System Monitoring, Statistics, Debugging <a name='toc7'></a>

**Display and manage the running processes**

    $ top

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

    $ apt install htop
    $ htop

**Display processor related statistics (refresh every 1 second)**

    $ mpstat 1
> **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

**Display virtual memory statistics (refresh every 1 second)**

    $ vmstat 1

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

    $ iostat 1

**List all open files on the system**

    $ lsof

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

    $ lsof -u USER

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

    $ lsof -p PID

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

    $ du -sh

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

    $ watch -n1 df -h

- - -

## File and Directory <a name='toc8'></a>

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

    $ ls -hal .

**Display the present working directory**

    $ pwd

**Create one or more new empty file**

    $ touch file1 file2

**Create a new directory**

    $ mkdir dir1

**Create a directory tree using -p option**

    $ mkdir -p dir1/dir2/dir3

**List the directory tree using tree command**

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

    $ apt install tree

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

    $ cp -v file1 dir1/file1-copy

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

    $ cp -vr dir1 dir1-copy

**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

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

    $ rm file1

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

    $ rm -vr dir1

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

    $ ln -s file1 file1-link

**Write a simple text to a file**

    echo "hello, world!" > hello.txt

**View the contents of a file**

    $ cat hello.txt

**Paginate through a large file**

    $ less hello.txt

**Display the first 20 lines of a file**

    $ head -n 20 hello.txt

**Display the last 20 lines of a file**

    $ tail -n 20 hello.txt

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

    $ tail -f hello.txt

- - -

## Process Management <a name='toc9'></a>

A **process** is a running instance of a program.

**Display your currently running processes**

    $ ps

**Display every process on the system.**

    $ ps auxf

**Display process information for the process name**

    $ ps uf -C processname

**Display interactive real-time view of running processes**

    $ top
    $ htop

**Look-up process ID based on a name**

    pgrep nginx

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

    $ kill PID

**Send a custom signal to a process with given process ID**

    $ kill -s SIGNAL_NUMBER pid

**List all available signals**

    $ kill -l

**Kill a process based on a name**

    $ pkill nginx

**Run a command as a background job**

    $ (sleep 30; echo "woke up after 30 seconds") &

**List background jobs**

    $ jobs

**Display stopped or background jobs**

    $ bg

**Brings the most recent background job to the foreground**

    $ fg

**Brings job N to the foreground**

    $ fg N

**Kill job N**

    $ kill %N

- - -

## File Permissions <a name='toc10'></a>

![](https://cdn-images-1.medium.com/max/2000/0*glWMCYK3ZLqD0Fek)

![](https://cdn-images-1.medium.com/max/2000/0*JMQf6GT93ITz8pbu)

**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

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

    $ chown user:group file1

- - -

## Networking <a name='toc11'></a>

**Display information of all available network interfaces**

    $ ip addr
    $ ifconfig -a          # deprecated

**Display information of eth0 interface**

    $ ip addr show eth0
    $ ifconfig eth0        # deprecated

**Display IP routing table**

    $ ip route
    $ route                # deprecated

**Ping a hostname or IP address**

    $ ping google.com
    $ ping 8.8.8.8

**Display registration information of a domain**

    $ whois medium.com

**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

**Display hostname and IP address of the local machine**

    $ hostname
    $ hostname -i

**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)

Display all process listening on TCP or UDP ports

    $ netstat -plunt
    $ lsof -i
    $ lsof -i tcp     # only TCP ports

- - -

## Text Search <a name='toc12'></a>

**Search for a pattern in a text file**

    $ grep pattern file

    # For example:
    $ grep root /etc/passwd

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

    $ grep -R "/bin/bash" /etc

**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

**Find files within a directory with a matching filename**

    find /etc -iname 'passwd'
    find /etc -iname 'pass*'  # glob pattern

**Find files based on filesize**

    find / -size +1M #  larger than 1MB
    find / -size -1M # smaller than 1MB

- - -

## Disk Usage <a name='toc13'></a>

**Show free and used space of disk storages**

    df -h

**Show disk space consumed by a directory or file**

    du -sh /var/log
    du -h 5MB.zip

**Interactive disk usage explorer**

    apt install ncdu
    ncdu

## Pipes and Redirection <a name='toc14'></a>

- - -

### REDIRECTION

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

    echo "hello" > hello.stdout.txt
    echo "world" > hello.stdout.txt

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

    cat somefile 2> cat.stderr.txt

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

    ps auxf >& processes.txt

**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

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

    cat some-unknown-file 2>> cat2.stderr.txt

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

    ps auxf &>> processes.txt

- - -

### 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

**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

**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

**Example 3: Search for a pattern in a text file**

    cat /etc/passwd | grep root    # show lines containing string 'root'

## Environment Variables <a name='toc15'></a>

**List all environment variables**

    $ env

**Display value of an environment variable**

    echo $HOME
    echo $SHELL

**Create an environment variable**

    export PORT=80
    export PLATFORM=medium.com

**Delete an environment variable**

    unset PORT

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

    $ echo $PATH
    $ unset PATH
- - -
## … 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.