Commands

man/ help/ info

  • To get manual or help or more information about any command

man

show manual of commands

//syntax
man command
//example
man ls
//to show all built in commands
man builtins

help

Most commands have --help argument

//syntax
command --help
//example
ls --help

info

//list all
info
//specific command
info command
//example of specific command
info ls

Note : In info output, press enter on navigation links to go to it's section

Docs

  • /usr/share/doc has documentation information

short/ long args

Most commands have short hand arguments specified by single dash (-), long arguments specified by double dash

//short hand (show all files including starting with .)
ls -a
//long
ls --all

Combine short hands

Many short hands which doesn't accept any value can be combined

//example
ls -l -h -t
//combined
ls -lht

Note : -h = --human-readable, -t = sort by modification time, -l = use long listing format

pwd

  • To print working directory path

    pwd

ls

  • To list all files

    ls
  • To list even hidden files

    ls -a

    Note : -h = --human-readable, -t = sort by modification time, -l = use long listing format

    ls somePath

du (disk usage)

  • To check folder size (du = disk usage, -h = human readable format, -sh= summary h format)

    Link

    du -sh
    du -sh path

cd

  • Changing directories Link

    cd path
    cd .. //one level up
    //just cd to go to home directory
    cd

cp

  • Copy files

    cp -R src dest
    //example glob (name followed by any no.of.chars)
    cp my-file* newTestFolder/
    //example glob (name followed by single char)
    cp my-file? newTestFolder/

scp

  • Copy folder from windows/linux to linux using ssh

    //secure copy (scp), -r = recursive
    scp -r source_folder user@host:/path/to/destination
    scp -r E:/some_folder/some root@server:/home/user/target_folder
    (with root user if permisiion issue)
    scp -r E:/some_folder/some user@server:/home/user/target_folder
  • Copy folder from linux to windows using ssh

    scp -r user@server:/home/user/target_folder E:/some_folder/some
  • ssh key authentication

    scp -P portNum -i privateKeyFile user@server:/home/user/target_folder localPath

mv

  • Move files (f=force without prompt while override?)

    mv -f src dest

rm

  • Delete/remove files (rm)

    Link

    rm file_name
    rm -r directory_name
    rm -rf dirname //without file prompt of delete

mkdir

  • Create directory

    mkdir dir_name

cat

To create a file with content

(alternative is to use "touch" command to create file)

cat > sample.txt
//now cursor is at 1st line of file
//(type what ever u want then ctrl+D to exit file)

To see file content

cat sample.txt //to view file content

Empty large file content

echo "" > filename.ext

History

  • Interactive Search

    • Ctrl+r to start the search
    • Each time you press Ctrl+r, you’re searching backward for the next matching command
    • press Enter to execute command.
    • To edit a command before you execute it, press either the Left or Right arrow key.
    • ~/.bash_history file has history commands
  • To see commands history

history
  • To see last n commands
history 10
  • Repeating some Command from history list
!commandNumber
!37
  • Search command by ! & hit enter
!searchString
for safety first print command
!searchString:p
  • Clearing the History List
history -c

History Ref

whoami

To print current user name

whoami

hostname

To print computer name

hostname

find

link

  • Find in current folder & copy those files to target directory with folder structure

    find . -regex '.*\(jpg\|JPG\|jpeg\|png\|PNG\|gif\)' \! -path
    './node_modules/*' -exec cp --parents {} ./build/ \;
    breakdown
    find . //find currentDirectory or path
    -regex '.*\(jpg\|JPG\|jpeg\|png\|PNG\|gif\)' //match extensions
    \! -path './node_modules/*' //exclude path
    -exec cp //execute copy after find
    --parents //maintain folder structure
    {} // copy source, here output of find files
    ./build/ \; // copy target

locate

link

to search for files and directories by their names

Install

sudo apt update
sudo apt install mlocate

updatedb

a cron job is created at the time of installation that runs the updatedb command every 24 hours

//to update file index manually
sudo updatedb

Search

Output is absolute path of all files and directories that matches the search pattern and for which the user has read permission.

//ignore case
locate -i readme
//limit result count
locate -n 10 readme
//display result count (--count)
locate -c readme

Note :

  • By default, locate doesn’t check whether the found files still exist on the file system. If you deleted a file after the latest database update if the file matches the search pattern it will be included in the search results. (Can use -e/ --existing option)
  • Locate is faster than find, but has limited features.

tail

To print last N / tail of text file (It is the complementary of head command)

tail -n LineCount filePath
tail -n 3 file.txt

Note: -f command in tail is used to follow changes, so any new content written at end can be watched & printed

grep

link

grep [options] pattern [files]

Example 1 : Case insensitive search

$grep -i "UNix" geekfile.txt
o/p:
unix is great os. unix is opensource. unix is free os.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

Example 2 : Displaying the line count of number of matches

grep -c "unix" geekfile.txt
o/p:
2
  • sample piping
cat /some/file | grep searhTerm >> newResultFile

>, >> , 2>

  • ref Note: 2 arrows >> will append text, one arrow > will override text, number two with arrow 2> denotes to add only errors
//multi line example (append)
echo '
line 1
line 2
' >> filename.txt
//multi line sudo example (a = append)
echo '
line 1
line 2
' | sudo tee -a filename.txt

less

displays file contents or command output one page at a time in your terminal

command | less
less filePath

Check RAM, Processor, DiskSpace

//RAM
free -h
//Hard Disk
lsblk
cat /proc/cpuinfo
cat /proc/meminfo
//dynamic changing stats
top

env

  • list all Environment variables
env

Note : use 'env | less' to see one page output at a time

o/p:
SHELL=/bin/bash
WSL_DISTRO_NAME=Ubuntu-20.04
NAME=xxx-my-computer-name-xxx
PWD=/usr/share/i18n/locales //keeps track of current working directory
LOGNAME=someUser
HOME=/home/someUser
LANG=C.UTF-8
USER=someUser
PATH=/home/someUserBin/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:..so on
HOSTTYPE=x86_64
_=/usr/bin/env
OLDPWD=/usr/share/i18n
  • Print some variable value
echo $variable
echo $NAME

Custom variables

  • local variable (for that bash)
//set variable (local)
abc=4
//get variable
echo $abc
  • to export variable for child bash
export abc
//now exported variable works in child bash
bash
echo $abc
exit

Note : in linux server -> basics.md check ".profile , .bashrc" section

bash

  • with in a bash, can open one more bash
//to start new child shell
bash
//to exit from child shell
exit

Change EDITOR

  • Change default editor before running command

    export EDITOR=gedit

    Note : Add above line in ~/.bashrc file to change setting permanently (.)

Users & related

shadow, passwd, group

//following file contains password
/etc/shadow
//following file contains user details
/etc/passwd
//following file contains group details
/etc/group
//consider sample entry in passwd file
prasan:x:1000:1000:prasan,,,:/home/prasan:/bin/bash
- prasan : name
- x : indicates password exists in shadow file
- 1000 : user id
- 1000 : group id
- /home/prasan : home directory
- /bin/bash : default shell
//consider sample entry in /etc/group
prasan:x:1000:
- prasan : group name
- 1000 : group id
//consider one more entry
sudo:x:27:prasan
- sudo : group name
- 27 : group id
- prasan : It indicates that prasan belongs to sudo group

id

To display user details & it's groups info

id UserName
eg:
id prasan
//o/p :
uid=1000(prasan) gid=1000(prasan) groups=1000(prasan),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare)

who

Check all logged in users

  • who are active, when their session begin, from where (ip) they came from
who
//sample output
prasan :0 2020-06-21 15:48 (:0)
prasan pts/0 2020-06-21 17:13 (192.xxx.x.xxx)

w

Who logged in & what are they doing

w
//sample output
17:38:25 up 1:50, 2 users, load average: 2.43, 2.24, 2.24
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
prasan :0 :0 15:48 ?xdm? 27:02 0.01s /usr/lib/gdm3/gdm-x-session --run-script env ...more
prasan pts/0 192.xxx.x.xxx 17:13 0.00s 0.10s 0.00s w

last

who logged in (history, duration, ip address..etc) since the file /var/log/wtmp was created

last | less
//sample o/p
prasan pts/0 192.xxx.x.xxx Sun Jun 21 17:13 still logged in
prasan pts/1 192.xxx.x.xxx Sat Jun 20 23:08 - 00:37 (01:29)

useradd

  • create a new user
sudo useradd -m john
  • '-m' to create home directory
//sample test commands
cd /home/john
ls -a
//o/p
.. .bash_logout .bashrc .profile
  • above default skeleton files came from '/etc/skel' folder
cd /etc/skel
ls -a

set passwd

  • set passwd for new user created
sudo passwd john
//it will prompt to enter & verify password
  • Also user can change his passwd self after login by
passwd
//it will prompt to enter & verify password

userdel

userdel -r username

groupadd

  • to create new group
sudo groupadd groupName

groupdel

sudo groupdel groupName

usermod

  • adding user to existing group
sudo usermod -a -G groupName userName
  • '-a' to add

  • '-G' existing group

  • Note : In centos, user is added to 'wheel' group for sudo permissions

su

substitute user / switch user

su username
su //for root user

chown

Change owner for folder

//-R for recursive sub folders
chown -R userName pathFileOrFolder

chgrp

To change group

chgrp -R grpName pathFileOrFolder

chmod

To change permissions

//consider some folder (ls -l)
drwxr-xr-x 2 prasan prasan 4096 Jun 13 19:00 Desktop
d - directory
1st rwx - read, write & execute for owner
2nd rwx - read, write & execute for group
3rd rwx - read, write & execute for others
prasan - user
prasan - group
  • Numeric notation read (4), write (2), execute (1) = 7(full permissions)

    chmod -R 770 pathFileOrFolder
    Note : The first 7 sets the permissions for the user,
    the second 7 sets the permissions for the group,
    and the third 7 sets the permissions for others
    7, rwx: read, write, and execute
    6, rw-: read and write
    5, r-x: read and execute
    4, r--: read-only
    3, -wx: write and execute
    2, -w-: write only
    1, --x: execute only
    0, ---: none

    image

  • To check all users permissions at folder level (l denotes long list format)

    ls -l
    ls -al
    ls -dl //current directory permission (.)
    In output:
    drwx = d for directory else _ file, r =read, w = write, x = executable

    Note: with out actually going to some folder, can list it's files/folders

    ls -l folderPath
  • To check all users in group

    grep '^group_name_here:' /etc/group

Sticky bit

A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file created by some other user.

//add sticky bit
chmod +t folderPath/
//remove sticky bit
chmod -t folderPath/

compgen

To check all users

compgen -u

umask

Umask Link

u mask = user file creation mask , governs the permissions that will get set when a new file is created.

Check existing folder umask (s = symbolic representation)

umask -S
output (user, group & others):
u=rwx,g=rx,o=rx

Check existing folder umask (numeric)

umask
output :
022
  • Operating system subtracts umask value from existing permission & then apply new permissions. (.)

    existingPermission - umask = newPermission
    770 - 022 = 750
  • Ingeneral / GIT repo case, even new files in the folder must be available to group

    umask 002 //shell level (say existing 770 - 002 = 770)

To change permanently (in /etc/profile) add following :

umask 002

For changes to take effect run the following source command or logout and log in

source /etc/profile
  • Also change setting in '/etc/bashrc'

    Note : check later to set umask for specific folder permanently

  • Some link

ubuntu

  • In ubuntu change umask in '/etc/login.defs' file
  • also set following line in '/etc/pam.d/common-session'
session optional pam_umask.so umask=0002

symbolic link

  • A symbolic link, also known as a symlink or soft link, is a special type of file that points to another file or directory (like a shortcut in Windows)
sudo ln -s fileOrFolderPath symbolicLinkPath
//eg: Say you are at /media/prasan/someDrive
//creates a sym link
ln -s /home/prasan/myData myDataBox
//test
ls -l
lrwxrwxrwx 1 prasan prasan 20 Jun 21 23:14 myDataBox -> /home/prasan/myData
//so 'cd myDataBox' will go to actual /home/prasan/myData folder

Note: -s (--symbolic) option

deleting symlink

unlink symbolicLinkPath

Note: while deleting links, be careful - dont add / at the end of path.

tar

Ref

  • Tape archive to create or extract archives
Options:
--create (-c) - Create a new tar archive.
--extract (-x) - Extract the entire archive or one or more files from an archive.
--list (-t) - Display a list of the files included in the archive
--file (-f) - archive file name
--verbose (-v) - Show the files being processed by the tar command

Creating Tar Archive

Tar also supports vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress.

//short
tar -cf archive.tar file1 file2 file3
//long syntax
tar --create --file=archive.tar file1 file2 file3

Creating tar gzip (gz) archive

tar -czf archive.tar.gz file1 file2

Note : z option for gzip, also file extension can be 'tar.gz' or 'tgz'

//if ater tar, gzip needed
gzip archive.tar

Creating Tar bzip2 (bz2) archive

tar -cjf archive.tar.bz2 file1 file2

Note : file extension can be 'tar.bz2' or 'tbz'

Listing files in tar archives

//example
tar -tf archive.tar
//output
file1
file2
file3

Note : use the --verbose (-v) option to get more info like file owner, file size, timestamp

Extracting Tar Archive

//regular file
tar -xf archive.tar
//compressed file
tar -xf archive.tar.gz
// add the -v option to print the names of the files being extracted.
tar -xvf archive.tar

Extracting Tar Archive in a Different Directory

tar -xf archive.tar -C /path/to/new-folder

Extracting Specific Files from a Tar Archive

tar -xf archive.tar file1 file2

Note : file names must be exact names including the path, as printed by --list (-t)

Adding Files to Existing Tar Archive

tar -rvf archive.tar newfile

Note : use the --append (-r) option

Removing Files from a Tar Archive

tar --delete -f archive.tar file1

Other

  • Use crypt or gpg on the file for password protection

zip

zip ref

unzip ref

Install

//Debian / Ubuntu
sudo apt install zip unzip
//CentOS
sudo yum install zip unzip

ZIP Files

zip archivename.zip filename1 filename2 filename3

Note : To suppress the detailed output of the zip command, use the -q option

ZIP directory

zip -r archivename.zip directory_name
zip -r archivename.zip directory_name1 directory_name2 file1 file1

Compression Methods and Levels

The default compression method of Zip is deflate.

  • To specify a compression method, use the -Z option
zip -r -Z bzip2 archivename.zip directory_name
  • Compression levels from 0 (no compression) to 9 (optimal compression), 6 is default
zip -9 -r archivename.zip directory_name

Creating a Password Protected ZIP file

zip -e archivename.zip directory_name

You will be prompted to enter and verify the archive password

Unzip a ZIP file

unzip latest.zip

Use the -q switch to suppress the verbose output.

Unzip a ZIP File to a Different Directory

unzip latest.zip -d /path/to/directory

Unzip a Password Protected ZIP file

unzip -P myPasswOrd filename.zip
//or type password at prompt
unzip filename.zip

List the Contents of a Zip File

unzip -l latest.zip

Other

  • Creating Split Zip File , check zip ref link

check linux/ distro version

//check ubuntu version (lsb = linux standard base)
lsb_release -a
//check linux kernel version
uname -r

network

ip address

ip addr
ip a
ip address
hostname -I
ip --brief add

netstat

//check connected interfaces
netstat –i
//check open ports
netstat –l
//check port listened by application / user details
sudo lsof -i -P -n | grep LISTEN

resolve.conf

  • ubuntu network-configuration

  • nameserver in '/etc/resolv.conf' file. Normally this file is handled from 'systemd-resolved' instead direct editing

//example
nameserver 192.*.*.*
nameserver 8.8.8.8 //google DNS server
nameserver 127.*.*.*
  • Configure ethernet in /etc/network/interfaces or /etc/netplan

hosts

find hostname or machine name

vi /etc/hosts

output

127.0.0.1 localhost
127.0.1.1 ubuntu01

Note: we can also specify other hosts to communicate

ssh

  • changing default port, check ubuntu -> install-server file

bash

//executable permission for script files

chmod +x myScript.sh
  • bash file starts with following
#!/bin/bash
  • if environment variables to be used
#!/bin/bash -l
  • Reading value from user
echo 'Please enter your name'
read NAME_VAR
echo 'Your name is $NAME_VAR'

Note : normal / env variables are accessed by $

  • Read with print (.)
read -p "Press [Enter] to continue..."

locale

  • Actual source locales location /usr/share/i18n/locales
locale
o/p:
LANG=C.UTF-8 //C, en_US, en_IN are files in above locales path, here using that files some format.
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=
  • people use following localectl command for managing locales
//current status
localectl status
//list all generated locales available not source (say India locales doesn't exist)
localectl list-locales
//generate locale
sudo locale-gen en_IN
sudo locale-gen en_IN.UTF-8
//reconfigure default locale by selecting generated locale in UI
sudo dpkg-reconfigure locales
// recheck list
localectl list-locales
//reboot machine for defaults to reflect
//manually set (reboot)
sudo localectl set-locale LANG=en_IN.UTF-8
sudo localectl set-locale LC_TIME=en_IN.UTF-8
//OR check in /etc/default/locale
sudo update-locale LANG=en_IN.UTF-8
sudo update-locale LC_NUMERIC=en_IN.UTF-8
sudo update-locale LC_MONETARY=en_IN.UTF-8
sudo update-locale LC_TIME=en_IN.UTF-8
//also export time zone in /etc/profile for oracle app
export TZ=$(sudo cat /etc/timezone)
  • Note : LC_ALL overrides all other variables, so better don't set it
//default locale file location
/etc/default/locale
// to prints file pointing region or check by 'timedatectl'
ls -l /etc/localtime
//typical logic it points to some file /usr/share/zoneinfo/Asia/Kolkata
//reconfigure timezone by UI
dpkg-reconfigure tzdata

change time zone

ubuntu help

type (check executable)

type ssh
o/p:
ssh is /usr/bin/ssh
type git
o/p:
git is /usr/bin/git

timedatectl

query and change the system clock and its settings

Device details df/lsblk

  • To check file system disk sizes (h for human readable format)
df -h
o/p:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 201G 147G 55G 74% /
/dev/sdb1 201G 147G 55G 74% /home
/dev/sdb2 201G 147G 55G 74% /var

Note :

  • dev = devices
  • sd = SCSI devices (SATA disks)
  • sda = 1st disk
  • sdb = 2nd disk
  • sdb1 - number indicates partition, 1st partition in sdb disk
  • sdb2 - 2nd partition in sdb disk

More Ref

//check more
df --help
  • to check particular disk types
df –ht ext4
  • If device is not in list check by listing all blocks (in hierarchy)
lsblk
o/p:
NAME SIZE TYPE MOUNT_POINT
sda 500G
- sda5 100G
- sda2 400G
- ubuntu--vg-root 380G
- ubuntu--vg-swap 20G
  • Say sda5 is not showing in 'df -h', we have to mount that device
//create some directory in /media
sudo mkdir /media/new-external-disk
//mount the device to folder
sudo mount /dev/sda5 /media/new-external-disk
  • for NTFS respective package must be installed
  • to check disk is being used (when mount error happens)
ps aux | grep sdc
  • list blocks with labels
sudo lsblk -o name,mountpoint,label,size,uuid

lshw

list hardware & resources

lshw

hosts

check '/etc/hosts' file

alias

to check alias commands

alias newAliasName="someCmd -arg1"
  • add the same in .bashrc file, if alias needed across all sessions.
  • To remove use unalias
unalias newAliasName

sort

link

  • useful in piping too

wc

  • word count
wc filename
  • total number of lines, words and characters

Auto Mount partitions (udisksctl)

  • ref

  • In explorer, other locations -> can see partition path like /dev/sda4..etc

  • Find uuid for that partition

ls -al /dev/disk/by-uuid/
//sample output
lrwxrwxrwx 1 root root 10 Sep 2 20:27 7aadd2b8-aec8-4285-a192-bc3cef2fxxxx -> ../../sda6
lrwxrwxrwx 1 root root 10 Sep 2 20:27 9b5d26db-6f33-42d1-a220-15321f8bxxxx -> ../../sda4
  • Mount partition by ID (syntax)
udisksctl mount --block-device /dev/disk/by-uuid/<uuid>
  • example
udisksctl mount --block-device /dev/disk/by-uuid/7aadd2b8-aec8-4285-a192-bc3cef2fxxxx
udisksctl mount --block-device /dev/disk/by-uuid/9b5d26db-6f33-42d1-a220-15321f8bxxxx

Startup

  • add these commands in home dir-> shell file
  • add the shell file in start up applications, to auto mount on boot

telnet

telnet domainOrIP port
//example
telnet localhost 8080
  • To exit the Telnet session, type Ctrl + ]
  • This changes the command prompt to show as telnet>, type in the word 'close' to close the session.

SELinux permission

  • By File

    configure the /etc/sysconfig/selinux file.
    Change SELINUX=enforcing to SELINUX=disabled
    Note : You must reboot the system for the changes to take effect
  • By Command immediately to reflect until next reboot

    //To check setting:
    getenforce
    //To change setting until next reboot:
    setenforce Permissive

    fdisk

  • To View All Disk Partitions

    fdisk -l
    output:
    Disk /dev/mapper/centos_gitrepo-home:
    Disk /dev/mapper/centos_gitrepo-repo: 250.0 GB,
    Disk /dev/sdc: 1000.2 GB,
    ..etc
  • To View Partition on a Specific Disk

    fdisk -l diskPath
    eg: fdisk -l /dev/sdc

Create partition

Firewall & ports

Open port to public

firewall-cmd --zone=public --add-port=28018/tcp --permanent
firewall-cmd --reload

$'\r': command not found

You have Windows-style line endings, run following command in windows

dos2unix scriptname

Reset admin password

ref

  • Login to recovery mode -> terminal
## At this stage we have read-only filesystem, remounting it with write permissions:
mount -o remount,rw /
passwd jorge
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Errors

  • /usr/bin/sudo must be owned by uid 0 and have the setuid bit set version (permission issue on usr bin & lib)
mount -o remount,rw /
mount --all
chown root:root /usr/bin/sudo
chmod 4755 /usr/bin/sudo
chown root:root /bin/sudo
chmod 4755 /bin/sudo
chown root:root /bin/su
chmod 4755 /bin/su
shutdown -r now

Note : can also change on entire /usr/bin, /usr/lib

to change shell to bash terminal after format

sudo usermod --shell /bin/bash specificUser