Show Menu
Cheatography

Linux1 Cheat Sheet (DRAFT) by

Shell commands

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Common

pwd
pinrt current working dir
cd
change directory
cd ../
change directory up 1
cd ~/
change dir to home.
ls
list content of directory
ls -l
detailed list
ls -h
readable byte format
ls -F
list all files + extension attributes
ls -lF
list files, ext, attrib, size, etc -> more detailed
tree
list directory in a tree like format
file
file command displays type of any file
file Desktop
will return Desktop: Directory
alias
to list all current alias for system­/user
alias LC="­new­Fun­cti­on"
creates a new alias "­LC" that does "­new­Fun­cti­on"
man
man pages
man -k
very similar to apropos
apropos
list all available man pages for specific word

Text + Redire­ction Examples

echo "­str­ing­"
sends string to stdout (terminal)
echo "­str­ing­" > file1.txt
sends string to file1.txt (redir­ection)
echo -e "­string \tNewS­tri­ng"
-e = enables extra character (?) use. \t option is for tab -> writes NewStr­ing­Aft­erTab after a tab, not space!
echo -e "­string \nNewS­tri­ng"
same as above but writes newstring after newline instead of tab.
cat
reads content of txt file and sends it to stdout (terminal)
cat > file.txt
with redire­ction before any file is mentioned -> will allow you to enter text at Terminal screen until Ctrl+d command (end) and sends it to file.txt
cat <<e­xit>
same as above but need to type exit as end command instead of Ctrl+d
cat file1.txt > file2.txt
reads content from file1.txt, sends it to file2.txt AND replaces any content!
cat file1.txt >> file2.txt
appends content of file1.txt to end of file2.txt
tac
same as cat but reads file in reverse!
head
show first 10 line of a file
head -n (X)
show the first X lines of a file
tail
show last 10 lines of text file
tail -n (X)
show last X lines of text file
more
displays first page of file
less
displays the last page of a file and allows to move through pages with enter + spacebar

Wildcards

*
Matches 0 or more occurr­ences of the previous character
Common
?
matches 0 or 1 occurr­ences of the previous char
Extended
+
matches 1 or more occurr­ences of prev char
Extended
.
Matches 1 character of any type
Common
[...]
Matches 1 character from the specified range
Common
[^...]
Matches 1 character NOT from the range specified within braces
Common
{ }
Matches a specific number or range of the prev char
Extended
^
Marches the following characters if they are the first characters on the line
Common
$
Matches the previous characters if they are the last char on the line
Common
(...|...)
Matches either of two sets of characters
Extended

misc

echo $PATH
see the content of the $PATH variable currently loaded in memory
which "­gre­p"
to search direct­ories of the PATH variable for a file called grep.
ln (link)
command to hard-link files
ln -s
command to create a symbolic link between 2 files
whoami
find out which user you are logged on as
groups
lists groups for current user unless we specify a user
cat /etc/group
to list all groups that exists on the current system
chown
change user
chown -R
change owner/­per­mis­sions recurs­ively (-R)
chgrp
change group command

Permis­sions

chown
change owner
chgrp
change group
chmod
change mode of file (permi­ssions
u
user
g
group
o
other
a
All categories
+
adds a permission
-
removes a permission
=
makes a permission equal to
chmod u+w file1
change user permis­sions to write for file1
rwx
4+2+1 = 7
rw-
4+2 = 6
r-x
4+1 = 5
r--
4
-wx
2+1 = 3
-w-
2
--x
1
---
0

Compre­ssion + Exatra­ction

compress -v file1 file2
compresses file1 into file1.z and file2 into file2.z
zcat compre­sse­dfi­le1.z
command to display contents of a compressed file
uncompress -v
command to uncompress the file
gzip -v file1
compresses fil1 with gzip utility (better compre­ssion ratio) to file1.gz
gunzip -v file1.gz
to decrom­press a .gz file
bzip2 -v file1
compress file1 using bzip2 utility to file1.bz2
bunzip2 -v file1.bz2
uncompress command
tar
tape archive utility. Used for backups and compre­ssion!
tar -cvf /backu­p.tar *
command to create an archive called backup.tar that contains the contents of the current directory
tar -tvf /backu­p.tar
command to view the contents of an archive
tar -xvf /backu­p.tar
extract the contents of the archive to our current directory
tar -zcvf /backu­p.t­ar.gz*
creates a gzip-c­omp­ressed archive called /backu­p.t­ar.gz that contains the contents of the current directory
tar -ztvf /backu­p.t­ar.gz
to view the contents of a gzip-c­omp­ressed archive
tar -zxvf /backu­p.t­ar.gz
to extract contents of a gzip-c­omp­ressed archive
cpio
similar to tar utility
 

Directory + files

mkdir
make directory
rmdir
remove directory -> unlike rm -rf
touch
can be used to create file.txt
rm
remove file
rm -rf
DELETE ALL! -> remove files recurs­ively and force it
rm -i
-i ooption to prompt for removal before deletion
mv file1.txt Newfil­e.txt
Ironically -> command to rename files. Rename file1.txt to Newfil­e.txt
cp /dir/file1 /newdi­r/file1
copy command to copy file1 from dir to newdir

File search + Manipu­lation

find
find for files in a directory hierarchy
locate
find files by name
cut
with options -> can cut out sections and fields of a text file and send it to stdout
cut -d ' ' -f (X)
-d = delimiter option. Anything that seperates the first field by the delimiter will be counter as 2...3...4...5 in field. -f = field option.
date
sends out the current date to stdout. Useful with redire­ctions
grep string file.txt
look for anything that contains string in file.txt
grep -i string
return any hit that matches string exactly
grep -i string
Can also be used to exclude case sensit­iveness
grep -r string
-r = looks for string recurs­ively down all files in the current directory tree.
grep -c 'string' Test.txt
Returns the numbers of instances of string in Test.txt. Similar to wc (word count) command
grep -v string text1.txt
-v option -> return lines that do not contain string
grep -e
extended grep - same as egrep
egrep
extended grep
egrep "­(lo­dge­|la­ke)­" text1.txt
View lines that contain either lodge of lake.
sed
stream editor -> can be used instead of cut & to search + replace.
sed -i -e 's/few­/asd/g' Test.txt
replace all instances of few in the file Test.txt with 'asd'. Find and replace command

Vi Commands

vi text.txt
opens text.txt in VI editor
i
enter insert mode
a
Changes to insert mode & places cursor after the current character to enter text
o
Change to insert and opens new line beneath current
I
changes to insert mode and places cursor at start of current line
A
Changes to insert mode and places cursor at end of line
Escape
leave insert mode
$
go to end of line in reg mode
:wq
save and exit
:q!
exit and do not save
ctrl+s­hift+v
paste keyboard shortcuts
G
move the cursor to last line of document
x
deletes character cursor is on
d$
deletes the cursor character to the end of the line
u
undo the last function
.
repeats the last function
y$
Copies the current line into a temporary buffer in memory for later use
p
Pastes the content underneath current line or after current word
P
Pastes content above current line or after current word
w filename
Saves the current document to file named filename
:r !date
reads output of date command into document under the current line
:set all
displays all vi enviro­nment settings
:s/the­/THE/g
searches for reg expression "­the­" and replaces each occurence globally throughout the current line with the word "­THE­"
:1,$ s/the/­THE/g
same as above but does this globally from line 1 to the end of the document

filesystem

cat /dev/proc
show all devices currently used by the system & it's major number
mount
mount command
mount -t
to specify which filesystem to be mounted use -t option
umount
unmount command
mkfs
make filesystem command
mkfs -t ext
use -t command + filesystem chosen to specify filesystem to be used
mke2fs
make ext2 file system.
fuser -u
to check if a directory or device is currently used by a user
cat /etc/fstab
list contents of fstab file which is file to edit to add devices to auto-mount at boot.
mkisofs
make iso image command
fdisk
partit­ioning utility
cdisk
Same as fdisk but with GUI
pvcreate /dev/sda4
creates physical volume
vgcreate vg00 /dev/sda4
create volume group
lvcreate -L 5GB -n Data vg00
create logical volume named Data that's 5GB in size.
df
disk free space command (shows free space on all mounted filesy­stems)
df -h
readable format
du
directory usage -> useful to know which directory is using up the most space
fsck
filesy­ste­mcheck
lsblk
list block devices (including Partit­ions)