GithubHelp home page GithubHelp logo

linux-commands-you-must-know-'s Introduction

Linux Commands You Must



I'll try to update these if needed, and if you'd like to contribute, you are free to do so. You can use this to learn commands, practice for interviews, or whatever you want to do.

Feel free to contact me!

Table of Contents


Linux Network Commands


SSH

login into a remote Linux machine using SSH

### SSH
SSH username@ip-address or hostname

dir

Display files in the current directory of a remote computer

whois

Shows whois information about the given domain such as registrar info, registrant info, admin info if available.

## Displays available information of apple.com
whois apple.com

ping

Sends ICMP packets to given address to check connectivity.

## Sends ICMP packets to google.com to check connectivity
ping google.com

ifconfig

Shows IP addresses of all interfaces

ifconfig

iwconfig

Shows wireless properties of the interfaces such as ESSID, Encryption keys.

iwconifg

host

Performs IP lookup for the domain name or vice-versa.

## Displays IP address of apple.com
host apple.com

## Displays domain name of 17.253.144.10
host 17.253.144.10

ss

Displays the information on all connections (TCP, UDP, unix Sockets). Previously accomplished by netstat command.

## Displays information on all connections
ss

## Only currently listening sockets
ss -l

## Displays UDP sockets
ss -u

## Displays TCP sockets
ss -t

## Displays Unix sockets
ss -x

## Displays TCP sockets that are listening. Works with -u and -x as well
ss -t -a

wget

Download the file from a given source.

## Saves the index.html file of the google homepage
wget google.com

##Downloads the D5100 manual pdf at the given location
wget https://cdn-10.nikon-cdn.com/pdf/manuals/dslr/D5100_EN.pdf

curl

Transfer data to or from a server using supported protocols like HTTP, FTP, SMTP, IMAP.

## displays content of the URL on the terminal
curl https://www.google.com

## download and saves the pdf in your local machine
curl -O https://cdn-10.nikon-cdn.com/pdf/manuals/dslr/D5100_EN.pdf

## download files from FTP servers. You don't need -u parameter if it's not authenticated.
curl -u <username>:<password> ftp://oshan.net/index.js

## display the definition of the word using DICT protocol. The following line displays the dictionary definition of 'aversion'
curl dict://dict.org/d:aversion

EXTRA TIP: You can view manual for curl using your terminal with man curl command.


Linux File Commands


pwd

Displays the path of the current directory.

## Outputs the path of the directory you are in
pwd

ls

Shows the content of your directory.

## Shows the content of current directory
ls

## Shows the content including . files or hidden files of current directory
ls -a

## Shows the content with permission and other details
ls -l

ls -al

cd

Change your current directory to given path.

## Takes you to the downloads folder if there is one
cd Downloads

## Toggle between your last two working directories
cd -

## Takes you to the parent directory
cd ..

## Takes you to the HOME directory
cd

mkdir

Create a new directory in a given filepath.

## Creates a directory named Projects in current directory
mkdir Projects

rm

Delete a file or directory.

## Deletes the file.txt in working directory or specified path
rm file.txt

## Deletes the directory dir1 iff it's empty
rmdir dir1

## Deletes the directory dir1 along with its contents
rm -r dir1

mv

Move file/directory from one place to other. You can also use this to rename files.

## Move file.txt to directory dir1
mv file.txt dir1

## Rename file.txt to index.txt
mv file.txt index.txt

## Move directory dir1 to directory dir2
mv dir1 dir2

## To avoid overwriting index.txt if it already exists
mv -n file.txt index.txt

You can also move multiple files such as mv file1 file2 file3 dir.


cp

Copy file/directory from one place to another.
Basic format:

cp [Options] sourceFile/Dir destinationFile/Dir
## Copies file.txt to dir1
cp file.txt dir1

## Copies directory dir1 to dir2 along with the contents
cp -R dir1 dir2

## Copies content of file.txt to file2.txt. You can use -i or -b or -f options according to your requirement. -i asks for confirmation if the file needs to be overwritten. -b creates a backup before overwriting file. -f forces the overwriting without any warning/backup.
cp file.txt file2.txt

You can also move multiple files such as cp file1 file2 file3 dir.


touch

Create or update a file on a specified path.

## Creates file file1.txt in the current director
touch file1.txt

## Creates multiple files file1.txt and index.js in the current working directory
touch file1.txt index.js


diff

Display difference between two files

## show difference between the contents of two files
dif [options] file1 file2 or dif file1 file2

cat

  1. Displays the content of the file
  2. Create a new file
  3. Copy the content of one file to other
  4. Merge files
## Display the content of file.txt
cat file.txt

## Create a file with the text "This is the first line"
cat > file2.txt
This is the first line.

## Copy the content of file2.txt to file.txt
cat file2.txt > file.txt

## Append "This is the second line" in the existing file file2.txt
cat >> file2.txt
This is the second line.

## Merge contents of file1 and file2 as merged_file
cat file1.txt file2.txt > merged_file.txt

head/tail

Displays the first 10 lines of file or last 10 lines of file. Helps when you are viewing a large file.

## Displays the first 10 lines of file.txt
head file.txt

## Displays the last 10 lines of file.txt
tail file.txt


tar

Command to extract and compress files

## Work with compression using tar and its options 
tar [options] [archive file] [file or directory]

## Creating a gzip compression on a tar archive. this creates a tar file called file.tar.gz which is the archive of .c files
tar cvzf file.tar.gz *.c

Linux Date Commands


date

Displays date and time of the system.

## Displays date and time of the system
date

## Displays date and time of 5 days ago
date --date="5 days ago"

## Similarly, displays date and time of 10 seconds ago
date --date="10 seconds ago"

cal

Displays calendar.

## Displays the current month with day highlighted
cal

## Displays the third month of 1997
cal 03 1997

Linux Search Commands


grep

Search for pattern in given file.

## Searches and displays lines containing string "linux" in file.txt
grep "linux" file.txt

## Searches and displays lines containing string "linux" in files in directory dir
grep -r "linux" dir

## Searches for the exact word "linux" in file.txt
grep -w "linux" file.txt

## Ignores the case while searching
grep -i "linux" file.txt

find

Searches for files in given directory with given criteria.
Generally the pattern is:

find [starting_directory] [criteria]
## displays the path of all files in the current directory
find .

## displays the path of files with name "index.js" in current directory
find . -name index.js

## displays the path of files greater than 500kb in size
find . -size +500k

You can use +/- with -atime or -mtime to get the files with last accessed time or modified time.
You can also execute commands on the result with -exec command or with prompt using -ok command.

## finds the file named index.js and delete it after displaying prompt
find . -name index.js -ok rm {} \;

Package Manager Commands


rpm

Manages rpm packages.

## Install package named "oshan.rpm"
rpm -ivh oshan.rpm

## Update if the package is already installed "oshan.rpm"
rpm -Uvh oshan.rpm

## Remove package named "oshan.rpm"
rpm -e oshan.rpm

## Verify package named "oshan.rpm"
rpm -V oshan.rpm

## List all the RPM packages
rpm -qa

Generally, most of the distros use their own package manager, and it's prefereable to use package manager like yum or dnf instead of rpm because it handles all the dependencies.
I use OpenSUSE, and I have zypper as my package manager.

apt is used commonly for Debian family systems to handle dpkg.


User Commands


useradd

Add new users. (needs superuser privileges)

## Add new user named friends
sudo useradd friends

## Set password for friends
sudo passwd friends

## Specify userid 3001, group as users while adding user
sudo useradd -u 3001 -g users oshan

## Create user with expiry date as 2021-02-30
sudo useradd -e 2021-02-30 oshan

## Check Account/Password expiry dates for oshan
sudo chage -l oshan

userdel

Delete users. (need superuser privileges)

## Delete user named friends
sudo userdel friends

## Delete user friends and it's home directory
sudo userdel -r friends

last

Displays the last logins on the system.
Tips: If you want to check if anyone is using your computer when you are not home this is the command you need.

#Outputs the info on last logins
last

Similar commands:

## Shows the active user id and group id
id

## Shows the username and login time of active user
who

Process Management Commands


ps

Displays process status.

## returns all the process running under the current user
ps ux

## returns the status of process with process ID 5266
ps 5266

top

Displays in real time all the running process along with CPU usage, Memory usage, Priority, CPU time.

## You can press q to exit from the display
top

kill

kills the process with given process ID

## Terminates the process with PID 5766
kill 5766

## Kills all process named code
killall code

pidof

returns the Process ID of given process name

## returns the process ID of process name code
pidof code

## returns the process ID of process name containing code. Here code is the pattern instead of process name.
pgrep code

pkill

kills the process with the process name of the given pattern

## kills all the processes whose names has pattern code
pkill code

jobs

returns the list of background jobs

## returns the background jobs with status
jobs

## send stopped processes to background jobs
bg

## bring the background job code to foreground
fg code

free

returns the memory status (RAM)

## Outputs the memory status in kilobytes
free

## Outputs the memory status in gigabytes or you can use -m for megabyte, -b for byte
free -g

Hardware Commands


lsusb

Lists USB buses and the devices connected to them.

## Displays USB buses and the devices connected
lsusb

## Displays detailed information about USB buses and connected devices
lsusb -v

lspci

Lists PCI buses and the devices connected ot them. You might need root privileges to run this command.

## Displays PCI buses and the devices connected
lspci

## Displays detailed information of PCI buses. The amount of details increase with the number of v in the options
lspci -v
lspci -vv
lspci -vvv

lshw

Lists the hardware configuration of the machine. Device information, Motherboarad configuration, CPU version, Memory configuration, etc. You might need root privileges to run this command.

## Displays the hardware configuration of the machine
lshw

## Displays the information in html format with tree structure
lshw -html

EXTRA TIPS: lshw -html > index.html to save the output as index.html and view it in your browser.


lsblk

Lists the information about block devices.

## Lists the block devices
lsblk

Using different options you can modify your result. Some of the useful ones are:
lsblk -all list all devices including empty ones
lsblk -O list all devices with all the availables columns
lsblk -o ColumnName list devices with given column information


fdisk

List out partition information and lets you modify it as well. You might need root privileges to run this command.

## List the partition and their sizes, number of sectors
fdisk -l

## lets you create/delete partition or other commands in given disk
fdisk /dev/sdb1

dmidecode

List hardware information from the BIOS. You might need root privileges to run this command.

## List the hardware information from the BIOS
dmidecode

## List the hardware information with little detail only. It's more readable.
dmidecode -q

dmesg

List detected hardware and boot messages.

## List the boot messages with detected hardware
dmesg

## List the messages in human readable format
dmesg -H

## Read the information and clear it
dmesg -c

EXTRA TIP: dmesg -H > file.txt saves the output in file.txt.


badblocks

Used to check for badblocks in disk partition. You might need root privileges to run this command.

## Check for bad blocks in partition /dev/sda with progress of the scan. -s parameter is optional.
badblocks -s /dev/sda

EXTRA TIP: badblocks -s /dev/sda -o file.txt saves the list of badblocks found in file.txt.


hdparms

Get/Set hard disk parameters.

## Check hard disk drive speed. I got 999.36 MB/sec
hdparm -t /dev/sda1

## Check hard disk cache read speed. I got 6458.49 MB/sec
hdparm -T /dev/sda1

## you can see all other things you can do with this command
hdparm -h

Linux Remote Commands


ssh (Secure Shell)

SSH lets you connect remote systems and enables secured transfer of files over insecure networks. It is used widely over its predecessors telnet which isn't as secured as SSH, and there are possible problems like eavesdropping.

## Install ssh server (On debian systems )
sudo apt-get install openssh-server

## Install ssh server on OpenSUSE or you can use your Package Manager
sudo zypper install openssh-server

## Start ssh server
sudo service ssh start

## Check ssh server status
sudo service ssh status

## Connect to remote server
ssh <username>@<host_ip_address>

## Connect to host using specific port
ssh -p <port> <username>@<host_ip_address>

telnet

Telnet is a client-server protocol based on TCP connections. It dates back to almost 50 years ago. SSH is preferred over, but still it doesn't hurt to learn about this as it is still in use.

## Install ssh server (On debian systems )
sudo apt-get install openssh-server

## Install ssh server on OpenSUSE or you can use your Package Manager
sudo zypper install openssh-server

## start the connection setup
telnet <hostname>

## start using IP address and port number
telnet <IP_ADDRESS> <PORT>

Linux File Permission Commands


chmod

Change the file access rights. The permission is expressed in three sets of three characters to denote permission for file owner, group owner, and all other users.

For files:

rwx rwx rwx
Read, write, and execute permission for file owner Read, write and execute permission for group owner Read, write, and execute permission for all other users

Here, we will use octal notation to make changes to file permissions.

rwx rwx rwx 111 111 111
--- --- --- 000 000 000
rw- rw- rw- 110 110 110

111 => 7
110 => 6
101 => 5
100 => 4
011 => 3
010 => 2
001 => 1
000 => 0

So, it's easy to give permission using octal notation to files.

## Give read, write, and execution permission to everybody
chmod 777 index.js

## Give read, write, and execution permission to nobody
chmod 000 index.js

## Give all permission to directory owner and no permission to anybody else
chmod 700 dir

#  Recursively set read, write, and execute permissions to the file owner and no permissions for all other users on a given directory:

chmod -R 700 dirname

# Recursively Change the File’s Permissions

# To recursively operate on all files and directories under the given directory, use the -R (--recursive) option:

chmod -R MODE DIRECTORY

# For example, to change the permissions of all files and subdirectories under the /var/www directory to 755 you would use:

chmod -R 755 /var/www

sudo

Get superuser (root) privileges temporarily. You need to enter root password for that. There are some commands that need superuser privileges because the file containing them don't have permission for other users.

sudo fdisk -l

chown

Change ownership of files. You need sudo privileges for executing this command.

## Change ownership of index.js from user to root
sudo chown root index.js

chgrp

Change group owner of file. You need sudo privileges for executing this command.

## Change group ownership of index.js from user to root
sudo chgrp root index.js

Linux Text Editors


vi

Split vim window horizontally inside vim mode. You need to be inside your vim window to run this command.

## split vim window
ctrl. + ws


vi

Switch between windows. you need to have split window activated first.

## switch between vim windows
ctrl. + ww


vi

Split vim window vertically inside vim mode. You need to be inside your vim window to run this command.

## split vim window
ctrl. + wv


vi

Quit window. exit vim window with this command.

## quit vim window
ctrl. + wq


vi

Cursor movement inside vim mode. use this commands to navigate around vim

## move up
k

## move down
j

## move left
h

## move right
l


vi

Copy a line on vim. this command copies contents of a vim cursor line.works outside insert mode.

## copy line
yy


vi

Paste a line on vim. this command copies contents of a vim cursor line. works outside insert mode.

## copy line
p

Linux Timezones Commands


timedatectl

Display current Timezone

## this displays current timezone
timedatectl

timedatectl list-zones

Displays a list of timezones

## this displays a list of available timezones
timedatectl list-zones

sudo timedatectl set-timezone <your_time_zone>

Set Timezone

## this will set timezone to your preferred timezone on the list, for example let us set for Africa/Lagos.  You have to run the command as root user or sudo user
sudo timedatectl set-timezone Africa/Lagos

linux-commands-you-must-know-'s People

Contributors

ubongndoh avatar belikeoscar avatar ejirolaureld avatar sayahaya20 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.