Linux commands cheat sheet
Linux commands cheat sheet
cd change directory, case &> will include output and errors iptables -h list commands
(Example: sensitive --list list the rule in a chain or all
cd apt chains
Desktop)
apt list | grep Type a word to highlight --verbose details
cd home directory in red or -v
. this directory apt | grep searchword --line-nu‐ print line numbers when
cd .. go up one directory search mbers listing
cd - change dir to previous apt install app name --version or version number
working dir apt app name -V
pwd print working directory remove --list-rules list rules
ls list contents, -a all, -h human apt update or -S
readable apt upgrade --numeric numeric output of addresses
chown (cont) vi or vim text editor (cont) vi or vim text editor (cont)
:admins change group owner to admins, double tap ESC if you don't know where x delete the current character
the owner is unchanged you are in vi 3x delete the current character and the
bob: change the current owner to bob Navigating Command Mode 2 after it
and change the group to the login H or left left one character dd delete the current line
group for bob arrow 5dd delete the current line and the next
J or down one line 4 lines
text editors
down dW delete from the current cursor
gedit GUI (installed in Ubuntu) arrow position to the beginning of the next
kedit KDE default K or up up one line word
kate sudo apt install kate arrow d$ delete from the current cursor
nano command line based (installed in L or right right one character position to the end of the line
Ubuntu) arrow d0 delete from the current cursor
vi or vi IMproved (installed in Ubuntu) 0 (zero) go to the beginning of the position to the beginning of the line
vim current line d^ delete from the current cursor
emacs sudo apt install emacs-gtk SHIFT-6 to the first non whitespace location to the first non whitespace
(^) character on the current line character line
text editing basics SHIFT-4 to the end on the current line dG from the current line to the end of
make a backup cp <filename> <fi‐ ($) the file
before editing lename.bak> W to the beginning of the next d20g from the current line to the 20th line
nano <filename> load a text in nano word, or punctuation characters of the file
SHIFT- to the beginning of the next COPYING
nano text editor W (W) word, ignore punctuation yy y stands for yank which is copy, yy
characters copies the current line
^X to quit nano, ^ mean CTRL
B to the beginning of the previous
^O CTRL-O save the file
word or character
vi or vim text editor (cont) vi or vim text editor (cont) Technical commands (cont)
5yy copies the current line and vi file1 file2 file3 cat somete‐ to read the file with cat
the next 4 lines :n switch to the next file xt.txt
yW from the cursor location to :N switch to the previous file | pipe connects input to
the beginning of the next output
:buffers lists open files
word ls /bin example sort 2 dirs binaries
:buffers switch to buffer file 2
y$ from the cursor location to /usr/bin |sort
2
the end of the current line | less
:e open another file from vi.
y0 from the current cursor sort alphabetize a list
example :e filename.txt
location to the beginning of uniq used with sort, removes
:r copy an entire file to the vi
the line duplicates
session. example :r myfile.txt
y^ from the current cursor grep pattern finding
location to the first non
Networking commands head checks the first 10 lines of a
whitespace character in
file. -n # (to adjust #)
line ping send en ICMP echo request to
network hosts tail checks the last 10 lines of a
yG from the current line to the
file. -n#
end of the file traceroute print the route packets trace to
a network host tail -f /var/l‐ -f is a real time log file
y20G from the current line to the
og/mes‐ viewer switch(CTRL-C to
20th line of the file netstat print the network connections,
sages quit)
routing tables, interface stats,
PASTING
masquerade connections, and tee for use with pipe
p pastes the current copied
multi cast memberships echo *D display everything with a
text below the current line
ftp file transfer program capital D
P pastes the copied text
lftp an improved ftp echo command line calculator
above the current line
$((2+2))
wget non interactive network
Join lines J
downloader echo "$(c‐ calendar
vi has the maybe I will add a cheat al)"
ssh openSSH SSH client (remote
ability to do sheet for this later
login) history a list of all commands used
search and
scp secure copy (remote copy history: !88 !88 will use number 88 in
replace
program) history
/searchtext searches vi
sftp secure ftp
:%s/Line/‐ will search and replace USB checking
line/g Line with line globally
Technical commands lsusb list connected USB devices
EDIT MULTIPLE FILES
cat (brings files CTRL-D to quit,
dpkg
together, uses input) tell cat EOF
cat > sometext.txt; this a simple way to dpkg --list list all installed
is some text to be add text to a text packages
saved file -r or --remove remove a package
-P or --purge
--log=<filename> log changes made
-i or --install <.deb file install a deb file
name>
chmod octal chart umask new file default permissions system stuff (cont)
(cont)
octal binary - filemode PATH a colon separated list of directories
0 000 --- 1000 set to a directory prevents files from that are searched when you enter
being deleted in shared directory the name of an executable
1 001 --x
the last three are octal permissions program
2 010 -w-
PS1 prompt string 1, defines the
3 011 -wx
system stuff contents of shell prompt
4 100 r--
printenv | system information PWD current working directory
5 101 r-x less TERM the name of your terminal type
6 110 rw- printenv views the contents of a TZ time zone
7 111 rwx USER variable, example USER
USER your username
example owner, group, world contains your user name
setting set | less display shell and enviro‐ Storage Media command overview
octal 600 nment variables
mount mount a file system
binary seems somewhat echo echo can be used to view
umount un-mount a file system (pay
irrelevant $HOME single variables
attention to the spelling of the
chmod symbolic notation alias lists all alias commands command Umount)
u user [command]; to get a notification when a fdisk partition table manipulator
g group alert command is done(an alias
fsck check and repair a filesystem
[message] in ubuntu)
o others, world fdformat format a floppy disk
below are **
a all mkfs make a filesystem
some
example notation dd write block-oriented data
variables of
u+x add execute permission for the interest directly to a device
owner geniso‐ create an ISO 9660 image file
DISPLAY the name of your display
u-x remove execute permission enviroment image
from the owner (mkisos)
EDITOR the default text editor
+x add execute for everyone, wodim write data to an optical storage
SHELL name of the shell running
same as a+x (cdrecord)
HOME path to home directory
o-rw remove read and write for md5sum calculate MD5 checksum
LANG keyboard language
everyone but owner and group LISTING DEVICES AND FILESYSTEMS
OLD_PWD the previous working
u+x,go=rx add excute for owner, group /etc/fstab is a list of devices
directory
and world changed to read and
mount will view a list of mounted
execute PAGER the name of the program
filesystems
used for paging output.
Usually is app less ls /dev list devices
umask new file default permissions
Storage Media command overview (cont) system startup stuff (cont) Moving data directly to and from devices
with dd (cont)
/dev/fd* floppy disks ~/.bashrc a users personal
/dev/hd* hard drives startup file dd if=/dev/sdb copy 2 identical usb
files that start with a is -a option when of=/dev/sdc drives that are the same
/dev/lp* printers
period are hidden searching with ls size
/dev/sd* SCSI disks
dd if=/dev/sdb make a raw image of the
/dev/sr* Optical drives CD/DVD
fdisk commands of=flash_dri‐ drive
readers and burners
ve.img
sudo umount unmount the partition
sudo tail -f to see real time device
/dev/sdb1
/var/log/‐ changes in the system creating CD-ROM images
sudo fdisk mount the entire drive
syslog
/dev/sdb dd if=/de‐ copy the contents of a cd to
MOVING DEV MOUNT LOCATIONS v/cdrom an image file
fdsik commands
sudo mkdir make a dir to mount flash of=ubu‐
p print partition tables
/mnt/flash drive ntu.iso
l list known partition
sudo moves the location to the new cdrdao is used for audio cds
types
mount one (use the proper device creating an iso from a collection of files
/dev/sdb1 name before moving) t change a partitions
genisoimage if you had a directory called
/mnt/flash system id
-o cd- /cd-rom-files, you could use
df display disk space df -h for w write partition changes
rom.iso -R - this command to make an
human readable q quit fdisk J ~/cd-rom-‐ image of the directory
files
system startup stuff creating a new filesystem with mkfs
the -R -J the allow the use of long
/etc/p‐ a global configuration script that sudo mkfs -t make an ext3 filesystem options filenames
rofile applies to all users ext3 on device sdb1 partition defined
~/.bas‐ a users personal startup file. can /dev/sdb1 mounting an iso image directly
h_profile override the global config script sudo mkfs -t make a fat32 silesystem using the -o loop with mount
~/.bas‐ if ~/.bash_profile is not found, vfat /dev/sdb1 on device sdb1
mkdir /mnt/i‐ then the next line down
h_login bash attempts to read this one
so_image
testing and repairing filesystems with
~/.profile if neither bash profile or login
mount -t mounts the iso as a virtual
fsck
are found this is run. this is the
iso9660 -o drive
default in Ubuntu sudo fsck checks the filesystem on
loop
/etc/b‐ a global config script that applies /dev/sdb1 sdb1 partition
image.iso
ash/bas to all users /mnt/iso_‐
hrc Moving data directly to and from devices
image
with dd
blanking a RW CD
dd raw copy tool (dd
wodim there are other types of
has 2 names, data
dev=/d‐ blanking
definition and destroy
ev/cdrw
disk)
blank=fast
dd if=input_file syntax to dd raw
writing an image file
of=output_file copy
wodim writes an image to cd
[bs=block_size
dev=/d‐
[count=blocks]]
ev/cdrw
image.iso
md5sum will give you the md5 top display tasks with the top netstat -tulpn
image.iso checksum value active ones -l (L) display listening sockets
md5sum a cd-r write in disc at once jobs list active jobs -n don't resolve names
/dev/cdrom can be checked like this bg place a job in the background -p display PID
fg place a job in the foreground -s an overview - statistics
lynis (auditing,hardening)
kill send a signal to a process via -g group memberships
sudo apt install install it
PID
lynis -r router table
killall kill process by name
lynis audit perform local security -i interface table
shutdown shut down or reboot the system
system scan
xlogo creates a nonsense xlogo app Customizing the Prompt
lynis show version
for debuging and testing ps
version adjust how the prompt is displayed and
controls
operates
--verbose details
[CTRL-c] terminates programs started in
echo display the current prompt
--pentest non privileged scan
terminal
$PS1 setup
xlogo & & will make a program start in
systemctl (services) ps1_ol‐ make a backup before making
the background
d="$‐ changes called ps1_old. check
systemctl -all or - see all loaded
fg %1 will bring the program to the PS1" with echo $ps1_old
a processes
foreground
PS1="$p‐ restore the original
list-units list units currently in
[CTRL-z] stops or pauses a process s1_old"
memory
bg %1 put program in background PS1= clear all prompt data (no visible
list-sockets list sockets currently in
killall killall xlogo for example if prompt)
memory
[process multiples are running PROMPT ubuntu default value is: [\u@\h
list-unit-files list installed unit files
name] VALUES \W]\$
poweroff turn the computer off
pstree outputs a process list in a tree PS1="\$ " a minimal prompt
reboot shutdown and restart pattern
\a ASCII bell. Makes the
is-system-running checks to see if the vmstat system resource snapshot computer beep
system is running [vmstat5] for continuous feed
\d current date in day, month,
--type service -- searching for running xload GUI draws a graph, showing date format
state running services sys stats overtime
\h hostname of the local machine
tload a terminal version of xload minus the domain
ps (running processes)
tload a terminal version of xload \H full hostname
ps -e all running processes
\j number of jobs in the current
ps aux | search for telnet running
shell
grep telnet
\l name of the current terminal
ps #### # = number for PID to view
device
ps -aux custom running process
\n a newline character
search
\r a carriage return
ps --help list more commands
\s name of the shell program
all
\t current time in 24hour H:M:S
ps report a snapshot of the
current running processes \T current time in 12hour format
Customizing the Prompt (cont) Customizing the Prompt (cont) Searching for files commands (cont)
\@ current time in 12hour am/pm \033[1;37m white find ~ type f find files with the name .jpg
format BACKGROUND COLOR -name "*.J‐ larger than 1 mb and count
\A current time in 24 hour H:M PG" -size them
\033[0;40m black
+1M | wc -l
\u username of the current user \033[0;41m red
find test
\v version number of the shell \033[0;42m green
-cmin n match files of dirs whose
\V version and release number \033[0;43m brown
content was modified
of the shell
\033[0;44m blue exactly n minutes ago
\w name of the current working
\033[0;45m purple -cnewer file match files or directories
directory
\033[0;46m cyan whose content was last
\W last part of the working
modified more recently than
\033[0;47m light gray
directory
the file named
\! history number of the current
Searching for files commands -ctime match files or dirs that were
command
modified within the last
locate find files by name
\# number of commands entered 24hours
in shell find search for files in directory
-empty match empty files or dirs
hierarchy
\$ displays the $ character
-group match files or dirs belonging
unless you have root, in that xargs build and execute command
name to a group
case it displays # lines from standard input
-iname like name but case sensitive
\[ the start of a series of one or touch change file times
pattern
more non-printing characters stat display file or filesystem status
-inum n match files with inode
\] the end of a non-printing locate (find files the easy way)
number n
character sequence locate locate will search for any string
-mmin n match files of dir whose
COLOR TEXT bin/zip with bin and zip
contents were modified n
\033[0;30m black locate zip and fancier search mins ago
\033[0;31m red | grep bin
-mtime n match files or dirs whose
\033[0;32m green find (find files the hard way) contents where modified n
find ~ a list of directories in our home 24 hours ago
\033[0;33m brown
dir -name match files with wildcard
\033[0;34m blue
find ~ | wc use word count to find how pattern pattern
\033[0;35m purple
-l many files -newer file match files or dirs whose
\033[0;36m cyan
find ~ - find how many directories with content was modifies more
\033[0;37m light grey recently than specified file
type d | word count
\033[1;30m dark grey wc -l
\033[1;31m light red find ~ - find regular files with word
\033[1;32m light green type f | wc count
-l
\033[1;33m yellow
\033[1;34m light blue
\033[1;35m light purple
\033[1;36m light cyan
Searching for files commands (cont) Searching for files commands (cont)
-nouser match files or dirs that do find ~ (-type f -not - test for bad files
not belong to a user perms 0600) -or (- and directories
-nogroup match files that do not type d -not -perms
belong to a group 0700)