Skip to content

bentlusty/linux-commads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 

Repository files navigation

linux-commads

Commands

unminimize

Restores content and packages that are found on a default Ubuntu in order to make it more suitable for interactive and learning use

tmux

Running multiple shell sessions in a single terminal window.

Basic tmux:

  • 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

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

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

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 PIS

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

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

Display information of eth0 interface

ip addr show eth0

Display IP routing table

ip route

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 addresse

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

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published