Show Menu
Cheatography

A basic cheat sheet to document linux commands

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

Viewing Output

cat <fi­len­ame>
view contents of file
cat <fi­le> | sort
alphab­etical output; does not change file
more <fi­len­ame>
view contents 1 screen at a time
less <fi­len­ame>
view contents, scroll up and down
 
/ allows for a keyword search
sort <fi­le>
sorts file; does not change file
sort -r <fi­le>
reverse sorts file; does not change file
sort <fi­le> > somefi­le.txt
creates somefi­le.txt of sorted content of
touch <to­uch.tx­t>
creates new file
 
updates access and modifi­cation times on a file

Searching for Files

find -name <file>
search for files by filename
find <dir path> -name <file>
search with dir path
find <dir path> -type d -name <file>
narrow search for directory only
find <dir path> -type f -name <file>
narrow search for files only
find -user <username>
search files by user
which <command>
returns location of command based on PATH settings
whereis <command> | tr " " '\n'
returns location of binary, source files and man pages
type
returns info about command type
The format of the whereis command is designed to pipe the output to the translate command and format the output line by line, by changing spaces to newline characters

Tests and Actions with find

Tests:
Actions:
-nouser = file not owned by user
-print­=de­fault output
-name = file name, can use wildcards
-ls = output long style listing
 
-exec = execute "cmd {} \;"
 
-ok = same as exec, but prompts for permission
The syntax for using find this way is:
find -options /path -tests -actions

File Transfer

scp [flag] <fi­len­ame> <us­er@­des­tin­ati­on_­hos­t:r­emo­te_­dir­ect­ory>
 

Compre­ssion and Archive

tar
collects a series of files and direct­ories into a single file
tar <op­tio­ns> <name of tar.ta­r> <path to dir being backed up>
create tar file
 
options:
 
c = create; v = verbose
 
f = file; x = extract; z = zip
tar -tf <name of tar file>
view contents of zipped archive file
tar xvfz <name of file to extrac­t>
restore archive files
To change the directory being extracted to add
--dire­cto­ry=­nam­e_o­f_d­ire­ctory
to the end of the command;
Often need sudo permis­sions;
When zipping, name file with .tar.gz extension

Chmod Octals

rwx
7
111
rw-
6
110
r-x
5
101
r--
4
100
-wx
3
011
-w-
2
010
--x
1
001
---
0
000
The order of permis­sions in the chmod command is owner, group, others

Input and Output redire­ction

>
create­/ov­erwrite file
>>
create­/append to file
<
direct file contents to a command or script
2>
redirect error output to a file or location
&>
redirect stdout and stderr to a file or location
* /dev/null location is a "­black hole" for sending things you don't need. When you inspect the directory you will find it empty

It can be used for redire­cting output that you don't want to see

Comparing Files

diff <fi­le1> <fi­le2>
outputs differ­ences between 2 files
 
context option: -c
diff <fi­lep­ath­1> <fi­lep­ath­2>
outputs differ­ences between 2 direct­ories
comm <fi­le1> <fi­le2>
compares 2 sorted files
cmp <fi­le1> <fi­le2>
compares files byte by byte
 
returns position of 1st difference