Show Menu
Cheatography

Bash Commands - ISM 4323 Cheat Sheet by

Bash Command Basics - initially created for ISM 4323 at USF

Bash

File system Root
/ - top of file hierarchy
Path
either absolute or relative
pwd
present working directory
cd
change directory
~
indicates home directory
..
moving up to folder above current
.
current folder
 
ls
lists all
ls -F
lists only folders
ls -l
show details
ls -a
show hidden files
ls -al
show hidden files + details
mkdir
creates folders
rmdir
removes folders
cp
copy
mv
move; can also be used to rename a file
rm
remove command
-i
adds intera­cti­vity; warning if will delete an existing file
UNIX file systems are CASE SENSITIVE
Folders are direct­ories
Copying and Moving Files
General syntax - <co­mma­nd> <so­urc­e> <ta­rge­t>
Examples - cp hello.txt hello_­wor­ld.txt
Notes on cp & mv
'cp' is only top level files, does not get what is in folders only folders on top
'mv' is always recursive
'cp -r' - copies a directory including all its content
Invasive Commands
'cp', 'mv', and 'rm' - no recovery possible

File Permis­sions

ls -al
10 charac­tesr; (1)file type + (9)file permis­sions
rwx
read, write, execute
owner, group, world
order which permis­sions are displayed
r
4 - indicates read permission
w
2 - indicates write permission
x
1 - indicates execute permission
r-x
read and execute (5)
755
rwe-er-er

Condit­ionals

IF/THEN
if [ <co­ndi­tio­n> ] then <co­mma­nds> fi
fi
terminates IF/THEN
if_[_<­equ­ati­on>_]
-eq
is equal to (integer)
-ne
is not equal to (integer)
-gt
greater than (integer)
-lt
less than (integer)
-ge
greater than or equal to (integer)
-le
less than or equal to (integer)
=
is equal to (string)
!=
is not equal to (string)
ELIF
altern­ative to nested-if statements
read
reads user input
bash <di­rec­tor­y> <va­ria­ble­s>
remember to put variables at end if variables are needed for command
 

Shell Scripting

grep <wo­rd>
print lines matching a pattern
redirect operator
(>) send output to file instead of displaying on screen
append operator
(>>) appends data instead of displaying on screen
cut -d" "
cut empty columns
cut -d, -f1,2,4
cuts out only columns 1,2,4
sort
sorts alphab­eti­cally
sort -n
sorts numeri­cally
uniq
removes duplicate lines from a text file; MUST BE SORTED FORT
tr "­abc­" "­xyz­"
subsitute <v1> to <v2>
tr "­[:l­owe­r:]­"­"­[:u­ppe­r:]­"
specify all lowercase letters to turn uppercase
new variable
supply variable name and its value; no spaces inbetween
variab­le1=2
valid; no spaces between equals
$((<eq­uat­ion­>))
arithmetic expansion
echo $<v­ari­abl­e>
prints <va­riable value>
" "
does not affect use
' '
used literally, does not call variables
$USER
lists current user
$PWD
prints present working directory
$HOME
home directory of current user
$RANDOM
o and 32767
sleep #
sleeps for # seconds

find

find
search for files
find [where to start searching from][­exp­ression to fine][­-op­tio­ns]­[what to find]
takes 2 arguem­ents; where to search & what to find; wildcards can be used
find /home -user alice -empty
finds files in the home folder from alice which are empty

Wildcards & Examples

Wildcards
?
matches a single character
*
matches any zero or more characters
[x..y]
matches a range of letters or numbers
Examples
ls
lists all files + extensions
ls *.doc
lists only files with .doc extensions
re - r <ta­rge­t>
removes folders with content; be very careful

While Loops

while
run until a specific conditions is met
CTRL+C
to close script
 

vim - file editor

vim "­fil­ena­me"
starts up in command mode
i
insert mode - start typing
:w
write
:q
quit vim type
:wp
save and quit
:q!
quit w/o saving
/word
search towards end of file for first occurance of word
?word
search towards beginning of the file for the word
n
search for next occurance
N
search for previous occurance
less
view file contents one screen at a time
head
top of file to see if it is the one you need
head -3
show the first three results
tail
bottom of file to see new log entries
-n
default output of ten line
-n#
changes number of lines outputed
-n & -n#
to be used with head or tail
YUM
YUM
yellowdog updater modifier - package modifier
su changes $ -> #

chmod

chmod
updates permis­sions on files and folders
su
grants super user privileges
ls -laF
lists all folders + access
chmod 775 engine­ering
changes permis­sions for the engine­ering folders
chown
change ownership
chgrp
change group
chown dave README
changes owner of README to dave
chgrp sales_grp README
changes the group that can access README to sales_grp

Encryption

aescrypt
secret key encryption
-e
encrypt the file
-d
decrypt the file
-p <pa­ssw­ord>
password to use; if omitted, the command will prompt the user
-o <fi­len­ame>
output file
md5sum
easy way to generate a checksum
#! /bin/bash
turns list of commands into one command // no need for bash ...

For Loops

for var in <it­em/­dir­ect­ory> do <co­mma­nds> done
how to format for loops
Sequences
{#..#}
#..#
first # starting value .. second# ending value
#1..#2..#3
#3 is increment between each number series
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Basic Japanese Cheat Sheet
          Java für Beginner > #01 Basics Cheat Sheet
          BASIC GERMAN CHEATSHEET Cheat Sheet