Linux Cheat Sheet
Linux Cheat Sheet
Password
Change password for user
passwd user
SSH
miranda-zhang/ssh.md
Searching:
Find
find . -type f -name "*.conf"
Grep
Example:
1
grep -lr 'gceu' .
grep pattern dir - Search for pattern in dir.
Flags:
-l (or --files-with-matches) option is used to only print filenames of match-
ing files, and not the matching lines (this could also improve the speed, given
that grep stop reading a file at first match with this option).
-r recursive
-i ignore-case
--include search only files that match the file pattern
grep -ril --include \*.py --include \*.html ".id"
• command | grep pattern – search for pattern in the output of command
More about regex.
Grep OR
https://www.thegeekstuff.com/2011/10/grep-or-and-not-operators/
grep 'pattern1\|pattern2' filename
grep -E 'pattern1|pattern2' filename
Locate
Find all instances of file
locate file
System Info:
• cal – show this month’s calendar
• uptime – show current uptime
• w – display who is online
• whoami – who you are logged in as
• finger user – display information about user
• htop - a lightweight text-mode process viewer packed with handy features
such as killing processes without entering their PID, displaying full com-
mand lines, etc with a colour display
2
OS Version
Show kernel information
uname -a
Ubuntu
lsb_release -a
CentOS
hostnamectl
http://whatsmyos.com
Cpu information
cat /proc/cpuinfo
Show number of cores only
grep -c ^processor /proc/cpuinfo
https://stackoverflow.com/questions/6481005/how-to-obtain-the-number-of-
cpus-cores-in-linux-from-the-command-line
Memory information
cat /proc/meminfo
Show memory and swap usage, show output in gigabytes.
free -g
date time
Show the current date and time, in the format of: date+“T”+hour+minute+second+UTC
time zone offset
$ date +"%FT%H%M%S%z"
2019-02-19T104943+1100
https://www.cyberciti.biz/faq/linux-display-date-and-time/
Disk
df -h
show disk usage with human readable unit.
du -h --max-depth=1
show directory space usage, only one level deep. ## IP
3
ip a
One line command to just grep the ip:
ip a | grep "scope global" | grep -Po '(?<=inet )[\d.]+'
For older version of centOS:
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
4
log
journalctl -u nginx.service --since today
https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-
view-and-manipulate-systemd-logs
Environment variables
Ubuntu doc
Dollar sign
The dollar sign $ can actually be used to combine the values of environment
variables in many shell commands. For example, the following command can
be used to list the contents of the “Desktop” directory within the current user’s
home directory.
ls $HOME/Desktop
5
solutions will only apply the environment to the single shell process, but not to
anything you launch through the GUI including new terminal windows.
App info
which app
show which app will be run by default
man command
show the manual for command
• whereis app – show possible locations of app
File Commands:
• cd dir - change directory to dir
• cd – change to home
• pwd – show current directory
• mkdir dir – create a directory dir
• rm file – delete file
• rm -r dir – delete directory dir
• rm -f file – force remove file
• rm -rf dir – force remove directory dir *
• cp file1 file2 – copy file1 to file2
• cp -r dir1 dir2 – copy dir1 to dir2; create dir2 if it doesn’t exist
• mv file1 file2 – rename or move file1 to file2 if file2 is an existing directory,
moves file1 into directory file2
• touch file – create or update file
• cat > file – places standard input into file
• more file – output the contents of file
• head file – output the first 10 lines of file
• tail file – output the last 10 lines of file
• tail -f file – output the contents of file as it grows, starting with the last
10 lines
6
Directory listing:
show hidden files with-a, show details with -l
ls -al
Determine file type, i.e. check if somthing is a symbolic link.
file
Counting Files in the Current Directory
ls -1 | wc -l
This uses wc to do a count of the number of lines (-l) in the output of ls -1.
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html
File Permissions
• chmod octal file – change the permissions of file to octal, which can be
found separately for user, group, and world by adding:
• 4 – read (r)
• 2 – write (w)
• 1 – execute (x)
Examples: * chmod 777 – read, write, execute for all * chmod 755 – rwx for
owner, rx for group and world
Compression
• tar cf file.tar files – create a tar named file.tar containing files
• tar xf file.tar – extract the files from file.tar
• tar czf file.tar.gz files – create a tar with Gzip compression
• tar xzf file.tar.gz – extract a tar using Gzip
• tar cjf file.tar.bz2 – create a tar with Bzip2 compression
• tar xjf file.tar.bz2 – extract a tar using Bzip2
• gzip file – compresses file and renames it to file.gz
• gzip -d file.gz – decompresses file.gz back to file
Process Management:
• top – display all running processes
• kill pid – kill process id pid
• killall proc – kill all processes named proc *
• bg – lists stopped or background jobs; resume a stopped job in the back-
ground
• fg – brings the most recent job to foreground
• fg n – brings job n to the foreground
7
ps
Display your currently active processes - https://unix.stackexchange.com/questions/163145/how-
to-get-whole-command-line-from-a-process
Show full command including arguments.
$ ps -p [PID] -o args
Network:
• ping host – ping host and output results
• whois domain – get whois information for domain
• dig domain – get DNS information for domain
• dig -x host – reverse lookup host
• wget file – download file
• wget -c file – continue a stopped download
Installation:
Debian
dpkg -i pkg.deb
RPM
rpm -Uvh pkg.rpm
CentOS
yum install pkg
Ubuntu apt update apt upgrade apt install pkg
Node.js, Javascript package manager
npm install
Audit
Possible log location >/var/log/audit/audit.log
grep "denied" /var/log/audit/audit.log
8
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security_guide/sec-
understanding_audit_log_files
Plot
Graph with gnuplot interactively:
apt update
apt install gnuplot-x11
gnuplot
# SET TERMINAL
set term gif
set output 'frequency.gif'
# set title "Word Frequency"
# Axes label
# set xlabel "Words"
# set ylabel "Frequency"
Symlink
https://shapeshed.com/unix-ln/#how-to-create-a-symbolic-link
Create symbolic link link to file
ln -s source_file link
Without -s, then create hard link.