Sysleaf

Linux command cheatsheet

. .
Linux command cheatsheet

Introduction

As a developer, we often have to deal with linux command, so this post is all about cheatsheet of common linux commands for quick reference.

Files & Directory

Description Command
list all files & directory under current directory ls
list all files & directory under ‘test’ directory ls test
list only files under current directory ls -f
list only directory under current directory ls -d */
list all (including hidden) files & directory ls -a
list(long format) all (including hidden) files & directory ls -la
list(long format) all (including hidden) files & directory with human readable size(KB,MB,etc) ls -lah
create new empty file ‘note.txt’ touch note.txt
delete file ‘note.txt’ rm note.txt
rename file ‘note.txt’ to ‘note1.txt’ mv note.txt note1.txt
move file ‘note.txt’ from directory ‘fav’ to ‘arch’ mv fav/note.txt arch
move file ‘note.txt’ from directory ‘fav’ to current directory mv fav/note.txt .
copy content of ‘note.txt’ to ‘info.txt’ cp note.txt info.txt
view content of file ‘note.txt’ on screen cat note.txt
view first 10 line of file ‘note.txt’ on screen head note.txt
view last 10 line of file ‘note.txt’ on screen tail note.txt
browse through content of file ‘note.txt’ on screen. use key ‘f’(forward) & ‘b’(backward) less note.txt
show current working directory pwd
create new directory ‘test’ mkdir test
go to directory ‘test’ cd test
go to parent directory cd ..
go to home directory cd ~
delete empty directory ‘test’ rmdir test
delete non-empty directory(including files & subfolder) ‘test’ rm -r test
userType: u=owner, g=group, o=other
permissionType: r=read, w=write, x=execute
permission value: r=4, w=2, x=1
change permission of file ‘note.txt’ to ‘u(rwx) g(rwx) o(rwx)’ chmod 777 note.txt
change permission of file ‘note.txt’ to ‘u(rwx) g(rwx) o(r-x)’ chmod 775 note.txt
change permission of file ‘note.txt’ to ‘u(rwx) g(r-x) o(rwx)’ chmod 757 note.txt
change permission of file ‘note.txt’ to ‘u(rw-) g(r–) o(r–)’ chmod 644 note.txt

Group & User Management

Description Command
create group ‘admin’ groupadd admin
delete group ‘admin’ groupdel admin
rename group ‘admin’ to ‘system’ groupmod -n system admin
show all groups on system. format is grpname:pwd:gid:memlist cat /etc/group
create user ‘john’ with default setting useradd john
create user ‘john’ with group ‘admin’ & home dir ‘/home/john’ useradd -d /home/john -g admin john
Create system(-r) user ‘mysql’ with noLogin(-s) ability & add to group ‘mysql’ useradd -r -g mysql -s /bin/false mysql
delete user ‘john’ userdel john
delete user ‘john’ & its home dir userdel -r john
change current group of user ‘john’ to ‘admin’ usermod -aG admin john
show all users details. format is usrnm:pwd:uid:gid:uinfo:home:shell cat /etc/passwd

Process management

Description Command
show which port is being used by which process sudo netstat -tulpn
show current terminal process ps
show all process with more detail ps -ef
show details of process whose pid is 123 & 142 ps -p 123,142
show details of process run by user u1 & u2 ps U u1,u2
show specific column in process list ps -ef --format uname,pid,ppid,%cpu,%mem,cmd
sort process list by column cmd & pcpu(process utilization) ps -ef --sort pcpu,cmd
search for pattern chrome* in process list `ps -ef
kill process with pid 123 & 143 kill 123 143
run program chrome in background mode chrome &
bring recent background program to forground fg

Compress & Extract

Description Command
create tar file ‘source.tar’ of directory ‘source’ tar -cf source.tar source
extract content of tar file ‘source.tar’ tar -xf source.tar source
create gzip compressed tar file ‘source.tar.gz’ of directory ‘source’ tar -czf source.tar.gz source
extract content of gzip compressed tar file ‘source.tar.gz’ tar -xzf source.tar.gz
extract content of xz compressed tar file ‘source.tar.xz’ tar -xpf source.tar.xz
run tar command in verbose mode while extracting gzip compressed tar file tar -xvzf source.tar.gz
Description Command
basic syntax of find find <where> <option> <what>
find file with name(case-insensitive) ‘demo’ in foler ‘test’ find test -iname demo
find file with name ‘demo’ in foler ‘test’ find test -name demo
find files in ‘test’ folder whose size is more than 20MB find test -size +20M
find files(type file) in folder ‘/etc’ whose name end with ‘conf’ find /etc -type f -name "*.conf*
file type option file:f, folder:d, link:l, chardevice:c, blockdevice:b
locate binary file of command ‘ls’ which ls
list all files in system matching pattern ‘*.txt’ locate *.txt

SSH & SCP

Description Command
generate ssh key(private & public) ssh-keygen -t rsa
print ssh public key on screen cut ~/.ssh/id_rsa.pub
ssh to host using default port 22 ssh user@host
ssh to host using port 28 ssh -p 28 user@host
basic syntax of scp scp <sourcFileLocation> <destFileLocation>
copy local file ‘/usr/a.txt’ to remote host location ‘/home’ scp /usr/a.txt usr@host:port:/home/
copy all file & subfolder of local folder ‘/home/doc’ to remote location ‘/home’ scp -r /home/doc usr@host:port:/home
copy file ‘/home/a.txt’ from remote host to local location ‘/usr’ scp usr@host:port:/home/a.txt /usr/

System & Hardware Information

Description Command
display basic system info(like os, kernal, processor, version etc) uname -a
since how long(hr:mm) system is running uptime
see reboot history last reboot
show current date & time date
display who is online & their activity w
display userid you have loggedin as whoami
list cpu info lscpu
list hardware info lshw
show all device class lshw -short or lshw -businfo
list hardware info which comes into disk & storage class lshw -class disk -class storage
list PCI lspci
list usb lsusb
show disk partition with size lsblk
show free & used space of each mounted filesystem df -h
show size of each files & subfolder of folder ‘image’ du -ah

Networking

Description Command
list all network interface details ifconfig -a
show details of interface wlan0 ifconfig wlan0
ping host ‘google.com’ ping google.com
check details of domain ‘google.com’ whois google.com
show listening ports & corresponding process netstat -nutlp
display hostname hostname
show local IP of host hostname -i
show network IP of host hostname -I
show name server details for domain ‘google.com’ nslookup google.com
show name server details for IP ‘google.com’ nslookup 216.58.197.78
lookup mailserver record of domain ‘google.com’ nslookup -querytype=mx google.com
lookup naemserver record of domain ‘google.com’ nslookup -querytype=ns google.com

Environment

Description Command
two types of env file by fileType script & non-script
script env files ~/.profile, /etc/profile
non-script env files ~/.pam_environment, /etc/environment
two types of env file by scope local/user & system-wide/alluser
local env files ~/.profile, ~/.bashrc
system-wide env files /etc/environment, /etc/profile
source the env file sources ~/.profile
export any env variable export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export path env variable export PATH=$PATH:$JAVA_HOME/bin

Others

Description Command
Run command ‘mysqld_safe’ as different user ‘mysql’ who doesn’t have login shell sudo -H -u mysql bash -c mysqld_safe