Cheatography
https://cheatography.com
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 <filename> |
view contents of file |
cat <file> | sort |
alphabetical output; does not change file |
more <filename> |
view contents 1 screen at a time |
less <filename> |
view contents, scroll up and down |
|
/ allows for a keyword search |
sort <file> |
sorts file; does not change file |
sort -r <file> |
reverse sorts file; does not change file |
sort <file> > somefile.txt |
creates somefile.txt of sorted content of |
touch <touch.txt> |
creates new file |
|
updates access and modification 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=default 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] <filename> <user@destination_host:remote_directory> |
|
|
Compression and Archive
tar |
collects a series of files and directories into a single file |
tar <options> <name of tar.tar> <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 extract> |
restore archive files |
To change the directory being extracted to add --directory=name_of_directory
to the end of the command;
Often need sudo permissions;
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 permissions in the chmod command is owner, group, others
Input and Output redirection
> |
create/overwrite 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 redirecting output that you don't want to see
Comparing Files
diff <file1> <file2> |
outputs differences between 2 files |
|
context option: -c |
diff <filepath1> <filepath2> |
outputs differences between 2 directories |
comm <file1> <file2> |
compares 2 sorted files |
cmp <file1> <file2> |
compares files byte by byte |
|
returns position of 1st difference |
|