Promotion: Organize your Gmail labels as tabs
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
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:/#
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 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 pressc
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 pressp
orn
Switch between horizontal or vertical window: Press
Ctrl+b
, release and then press;
Kill a window: Press
Ctrl+b
, release and then pressx
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
Package Manager
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
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
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
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
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
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 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
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
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
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
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
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
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.
Top comments (11)
Instead of
ifconfig -a
it's better to use the newer commandip a
That's right Kevin. I've updated with
ip
command. Thanks!ifconfig
command is too old and it should useip
address command instead.ip
is useful, butifconfig
is much more readable not to mention after decades of use I tend to use it reflexively.same here but ifconfig does omit certain informations that ip shows you. I force myself to switch too :-)
Just notice that the
nmcli
command can be added onNetworking
section.It can use this to check current network connection, modify network connection information, activate/inactivate specific network connection info and so on :).
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
Thanks Ian. Typo fixed 😊
strace -s 2048 -p <PID>
or
strace -s 2048 <command>
is also very helpful sometimes ;)
Nice bro , you got them all in one place.
Thank you, it very useful for newbies