Commands
man/ help/ info
- To get manual or help or more information about any command
man
show manual of commands
help
Most commands have --help argument
info
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
Combine short hands
Many short hands which doesn't accept any value can be combined
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
lsTo list even hidden files
ls -aNote : -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)
du -shdu -sh path
cd
Changing directories Link
cd pathcd .. //one level up//just cd to go to home directorycd
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 = recursivescp -r source_folder user@host:/path/to/destinationscp -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_folderCopy folder from linux to windows using ssh
scp -r user@server:/home/user/target_folder E:/some_folder/somessh 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)
rm file_namerm -r directory_namerm -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)
To see file content
Empty large file content
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
- To see last n commands
- Repeating some Command from history list
- Search command by ! & hit enter
- Clearing the History List
whoami
To print current user name
hostname
To print computer name
find
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/ \;breakdownfind . //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
to search for files and directories by their names
Install
updatedb
a cron job is created at the time of installation that runs the updatedb command every 24 hours
Search
Output is absolute path of all files and directories that matches the search pattern and for which the user has read permission.
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)
Note: -f command in tail is used to follow changes, so any new content written at end can be watched & printed
grep
Example 1 : Case insensitive search
Example 2 : Displaying the line count of number of matches
- sample piping
>, >> , 2>
- ref Note: 2 arrows >> will append text, one arrow > will override text, number two with arrow 2> denotes to add only errors
less
displays file contents or command output one page at a time in your terminal
Check RAM, Processor, DiskSpace
env
- list all Environment variables
Note : use 'env | less' to see one page output at a time
- Print some variable value
Custom variables
- local variable (for that bash)
- to export variable for child bash
Note : in linux server -> basics.md check ".profile , .bashrc" section
bash
- with in a bash, can open one more bash
Change EDITOR
Change default editor before running command
export EDITOR=geditNote : Add above line in ~/.bashrc file to change setting permanently (.)
Users & related
shadow, passwd, group
id
To display user details & it's groups info
who
Check all logged in users
- who are active, when their session begin, from where (ip) they came from
w
Who logged in & what are they doing
last
who logged in (history, duration, ip address..etc) since the file /var/log/wtmp was created
useradd
- create a new user
- '-m' to create home directory
- above default skeleton files came from '/etc/skel' folder
set passwd
- set passwd for new user created
- Also user can change his passwd self after login by
userdel
groupadd
- to create new group
groupdel
usermod
- adding user to existing group
'-a' to add
'-G' existing group
Note : In centos, user is added to 'wheel' group for sudo permissions
su
substitute user / switch user
chown
Change owner for folder
chgrp
To change group
chmod
To change permissions
Numeric notation read (4), write (2), execute (1) = 7(full permissions)
chmod -R 770 pathFileOrFolderNote : 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 others7, rwx: read, write, and execute6, rw-: read and write5, r-x: read and execute4, r--: read-only3, -wx: write and execute2, -w-: write only1, --x: execute only0, ---: noneTo check all users permissions at folder level (l denotes long list format)
ls -lls -alls -dl //current directory permission (.)In output:drwx = d for directory else _ file, r =read, w = write, x = executableNote: 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.
compgen
To check all users
umask
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)
Check existing folder umask (numeric)
Operating system subtracts umask value from existing permission & then apply new permissions. (.)
existingPermission - umask = newPermission770 - 022 = 750Ingeneral / 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 :
For changes to take effect run the following source command or logout and log in
Also change setting in '/etc/bashrc'
Note : check later to set umask for specific folder permanently
ubuntu
- In ubuntu change umask in '/etc/login.defs' file
- also set following line in '/etc/pam.d/common-session'
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)
Note: -s (--symbolic) option
deleting symlink
Note: while deleting links, be careful - dont add / at the end of path.
tar
- Tape archive to create or extract archives
Creating Tar Archive
Tar also supports vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress.
Creating tar gzip (gz) archive
Note : z option for gzip, also file extension can be 'tar.gz' or 'tgz'
Creating Tar bzip2 (bz2) archive
Note : file extension can be 'tar.bz2' or 'tbz'
Listing files in tar archives
Note : use the --verbose (-v) option to get more info like file owner, file size, timestamp
Extracting Tar Archive
Extracting Tar Archive in a Different Directory
Extracting Specific Files from a Tar Archive
Note : file names must be exact names including the path, as printed by --list (-t)
Adding Files to Existing Tar Archive
Note : use the --append (-r) option
Removing Files from a Tar Archive
Other
- Use crypt or gpg on the file for password protection
zip
Install
ZIP Files
Note : To suppress the detailed output of the zip command, use the -q option
ZIP directory
Compression Methods and Levels
The default compression method of Zip is deflate.
- To specify a compression method, use the -Z option
- Compression levels from 0 (no compression) to 9 (optimal compression), 6 is default
Creating a Password Protected ZIP file
You will be prompted to enter and verify the archive password
Unzip a ZIP file
Use the -q switch to suppress the verbose output.
Unzip a ZIP File to a Different Directory
Unzip a Password Protected ZIP file
List the Contents of a Zip File
Other
- Creating Split Zip File , check zip ref link
check linux/ distro version
network
ip address
netstat
resolve.conf
nameserver in '/etc/resolv.conf' file. Normally this file is handled from 'systemd-resolved' instead direct editing
- Configure ethernet in /etc/network/interfaces or /etc/netplan
hosts
find hostname or machine name
output
Note: we can also specify other hosts to communicate
ssh
- changing default port, check ubuntu -> install-server file
bash
//executable permission for script files
- bash file starts with following
- if environment variables to be used
- Reading value from user
Note : normal / env variables are accessed by $
- Read with print (.)
locale
- Actual source locales location /usr/share/i18n/locales
- people use following localectl command for managing locales
- Note : LC_ALL overrides all other variables, so better don't set it
type (check executable)
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)
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
- to check particular disk types
- If device is not in list check by listing all blocks (in hierarchy)
- Say sda5 is not showing in 'df -h', we have to mount that device
- for NTFS respective package must be installed
- to check disk is being used (when mount error happens)
- list blocks with labels
lshw
list hardware & resources
hosts
check '/etc/hosts' file
alias
to check alias commands
- add the same in .bashrc file, if alias needed across all sessions.
- To remove use unalias
sort
- useful in piping too
wc
- word count
- total number of lines, words and characters
Auto Mount partitions (udisksctl)
In explorer, other locations -> can see partition path like /dev/sda4..etc
Find uuid for that partition
- Mount partition by ID (syntax)
- example
Startup
- add these commands in home dir-> shell file
- add the shell file in start up applications, to auto mount on boot
telnet
- 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=disabledNote : You must reboot the system for the changes to take effectBy Command immediately to reflect until next reboot
//To check setting:getenforce//To change setting until next reboot:setenforce Permissivefdisk
To View All Disk Partitions
fdisk -loutput:Disk /dev/mapper/centos_gitrepo-home:Disk /dev/mapper/centos_gitrepo-repo: 250.0 GB,Disk /dev/sdc: 1000.2 GB,..etcTo View Partition on a Specific Disk
fdisk -l diskPatheg: fdisk -l /dev/sdc
Firewall & ports
Open port to public
$'\r': command not found
You have Windows-style line endings, run following command in windows
Reset admin password
- Login to recovery mode -> terminal
Errors
- /usr/bin/sudo must be owned by uid 0 and have the setuid bit set version (permission issue on usr bin & lib)
Note : can also change on entire /usr/bin, /usr/lib