ABSGResumen
🧩 Syntax:
#ADVANCED BASH SCRIPTING#
#special characters
# -> comment
\# #-> #scaped comment
echo ${PATH#*:} #-> #parameter substitution
echo $(( 2#101011 )) #-> #base conversion
#;
command; command #-> #separates commands
#;;
case "$var" in
a) echo "\var = a" ;; #case line terminator
esac
#;;& - ;& case (bash v4+) terminator
#. -> current directory
./scriptname.sh
#" -> scapes most special characters in a string
#' -> scapes all special characters in a string
#, -> links arithmetic operations & returns only the last one
let "t2 = ((a = 9, 15 / 3))"
# -> concatenates strings
for file in /{,usr/}bin/*filename
# ^includes "usr" to form /usr/bin
#,, , -> lowercase character coversion
var="STRING"
echo "${var,,}"
#\ -> escape character
echo \""quotation signs\""
#` -> command substitution
echo x=`echo "$x + 1" | bc`
#: -> No-OP/NOP = true = 0
:
echo $? #prints 0
#-
: ${name=`whoami`}
: ${1?"Use: 0"} #-> #command placeholder
: > data.log #-> #empties file data.log
:() #-> #used as function name
! #-> inverts/negates the effect of 'test' operators
x=0
if [ $x != "10"] #-> if x not equal 10
* #-> globbing filename expansion
rm -rf /*.*
** #-> represents exponentiation/extended file match
? #-> conditional test/single character matching
(( var1=var2<98?9:21 ))
#if [ "$var2" -lt 98 ]
#then
# var1=9
# else
# var1=21
#fi
#$ -> variable expansion/end-of-line in regex
#${} -> parameter substitution
#$'...' -> quoted string expansion
#$*,$@ -> positional parameters
#$? -> exit status
#$$ -> script's own PID
#() -> command group
(a=hi; echo $a) #starts a subshell/not visible to the rest of the script
#-
a=123
( a=321; )
echo "a = $a" #-> echoes 123
#-
array=(a b c)
#{} -> brace expansion
echo \"{these,appear,quoted}\" #"these" "appear" "quoted"
#-
cat {file1,file2,file3} > combined_file.txt
#-
cp file.{txt,backup} #-> #copies "file.txt" to "file.backup"
#-
echo {file1,file2}\ :{\ a," b",' c'}
#spaces need to be scaped
#{a..z} -> extended brace expansion
echo {a..z} #a b c d e.....z
#-
echo {0..9} #0 1 2 3 4...n
#-
base64_charset=( {A..Z} {a..z} {0..9} + / = )
#-
a=123
{ a=321; } #codeblock
echo "a = $a" #echoes 321
#-
ls . | xargs -i -t cp ./{} $1
#text placeholder
#{} \; -> pathname - not a shell builtin/used w/ 'find' command
#[] -> test expression
#[[]] -> (fexible) test expression
#[] -> array
#$[] -> integer expansion
a=1
b=2
echo $[$a+$b] #evaluating integer expression
#(()) -> integer expansion
#>, &>, >&, >>, <, <> #redirection
#(command) >, < (command) #process substitution
#<, > -> ASCII comparisson/dictionary word order
if [[ "$string1" < "$string2" ]]
#\<, \> -> word boundary
grep '\<word\>' file.txt
#>| -> force redirection
#|| -> logical OR
#& -> background job
sleep 10 &
#&& -> logical AND
#- -> option prefix
#~ -> home directory
cd ~user
cd ~
cd ~/
cd ~:
#~+ -> current working directory/$PWD
#~- -> previous working directory/$OLDPWD
#=~ -> regular expression match
###########
#VARIABLES#
###########
#valid
echo "$var"
echo "${var}"
var="a b c d"
echo $var #a b c d
echo "$var" #a b c d
#preserves whitespace
var1=20 var2=21 var3=22 #permitted but not portable
var_space=2\ ---\ word
echo "$var_space" #2 --- word
#spaces were scaped
var=23
unset var
echo "var = $var" #unset variables get null value
var0= #null value variable/uninitialized variable
let "var0 += 1" #arithmetic operations using uninitialized variable are possible
echo "$var0"
a=11bbcc
b=${a/11/aa} #substitute '11' w/ 'aa'
echo "b = $b" #aabbcc
#declare
declare -i var #declares var as integer
declare -r var #declares var as read only
declare -r -i var #declare var as read only and integer
{ var=112233; } #local variable / visible in codeblocks or functions
var=123 #environmental variable / visible to the shell and user
$0..$9 ${10} ${11} ${12} #positional parameters after $9 must be bracketted
$* & $@ #access all parameters
shift 3 #shift reassigns positional parameters
#$1 <- $2, $2 <- $3, $3 <- $4
n=3; shift $n #shifts 3 positions
#########
#QUOTING#
#########
grep '[Ww]ord' *.txt #quoting protects meaning
echo "$(ls -l)" #suppressing echo newline
#COMMAND $var1 $var2 $var3
#COMMAND "$var1" "$var2" "$var3"
#COMMAND "$var1 $var2 $var3"
echo -e "\v\v\v\v" #vertical tabs
echo -e "\040" #octal ASCII character
echo $'\n' #echo $'\letter' construction makes '-e' unnecessary
#newline
echo $'\a' #beep (depends on terminal)
echo $'\t \044 \t' #echoes ASCII character
echo $'\t \x44 \t' #echoes hexdecimal number
var=$'\044' #asigning ASCII character to var
ABC=$'\101\102\103\010' #echoes "ABC"
#\" #literal "
echo "\"Hello"\"
#\$ #literal dollar sign
echo "Cost is \$9.99"
#\\ #\
echo "\\" #echoes "\"
echo '\' #same effect
#escaping & quoting
echo \z #z
echo \\z #\z
echo '\z' #\z
echo '\\z' #\\z
echo "\z" #\z
echo "\\z" #\z
#command substition
echo `echo \z` #z
echo `echo \\z` #z
echo `echo \\\z` #\z
echo `echo \\\\z` #\z
echo `echo \\\\\\z` #\z
echo `echo \\\\\\\z` #\\z
echo `echo "\z"` #\z
echo `echo "\\z"` #\z
#here-doc
cat <<EOF
\z
`EOF #echoes \z
cat <<EOF
\\z
`EOF #echoes \\z
#
#
cat <<var
echo `date&&who`
`var #command substitution
echo 'hello
there' #echoes "hello there"
echo hello\
there #echoes "hellothere" - newline is scaped
echo "hello\
there" #same - newline scaped
echo 'hello\
there' #echoes "hello\ there"
####################
#Exit & Exit Status#
####################
true
echo "exit status \"true\" = $?" #0
! true #negating true
echo "exit status \"true\" = $?" #1
true
!true #not negating, repeating last "true" command
ls | nonexistent_command
echo $? #127
! ls | nonexistent_command
echo $? #0
#################
#Test Constructs#
#################
[[ $var -lt $var ]] #return exit status
(( 0 && 1 )) #AND
echo $? #exit status 1
#but
let "num = (( 0 && 1 ))"
echo $num #exit status 0
#but
let "num = (( 0 && 1 ))"
echo $? #exit status 1
(( 200 || 11 )) #OR
echo $? #0
#
let "num = (( 200 || 11 ))"
echo $num #1
#
let "num = ((200 || 11 ))"
echo $? #0
(( 200 | 11 )) #bitwise OR
echo $? #0
#
let "num = (( 200 | 11 ))"
echo $num #203
#
let "num = (( 200 | 11 ))"
echo $? #0
if cmp a b &> /dev/null #diffing w/ if
then echo "Identical files"
else echo "Files differ"
fi
#
if grep -q word filename
then echo "File contains word"
fi
#
word=linux
lettr_seq=inu
if echo "$word" | grep -q "$lettr_seq" #'-q' suppresses output
then
echo "$lettr_seq found in $word"
else
echo "$lettr_seq not found in $word"
fi
#
if command_with_status_0_unless_error
then echo "command succeeded"
else echo "command failed"
fi
#elif
#if [ condition1 ]
#then
# command
#elif [ condition2 ]
# then
#command
#else
# command
#fi
#&&, ||, <, > work inside [[]] - not inside []
if [[ -e $file ]] #no filename expansion/word splitting
then
echo "File exists"
fi
'---'
dir=/dir/dir
if cd "$dir" 2>/dev/null; then #hides error msg
echo "In $dir"
else
echo "Can't cd to $dir"
fi
'---'
foo=20
bar=22
[ "$foo" -ne "$bar" ] && echo "$foo" NEQ "$bar"
home=/home/home
[ -d "$home" ] || echo "$home doesn't exist"
'---'
dev0="/dev/sda2" # / (directory)
if [ -b "$dev0" ]
then
echo "$dev0 is block device"
fi
#
dev1="/dev/ttyS1" #PCMCIA
if [ -c "$dev1" ]
then
echo "$dev1 is character device"
fi
#
function show_IO()
{
[ -p /dev/fd/0 ] && echo PIPE || echo STDIN
}
show_IO "Input"
echo "Input" | show_IO
#
[[ $a == z* ]] #true if $a starts w/ "z"
[[ $a == "z*" ]] #true if $a = z*
[ $a == z* ] #file globbing/word splitting
[ "$a" == "z*" ] #true if $a = z*
#
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ] #'<' needs scaping between "[ ]"
#
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ] #'>' needs scaping too
#
if [ "$expr1" -a "$expr2" ] #logical AND
#true if both are true
if [ "$expr1" -o "$expr2" ] #logical OR
#true if either is true
#
echo $(( 1 && 2 )) $((3 && 0)) $((4 || 0)) $((0 || 0))
#
let "op = ((5+3, 7-1, 15-4))" #last arithmetic op to right value assigned to $op
echo "op = $op" #echoes 11
'---'
let "opn = ((a = 24/3, 15/3))"
echo "opn = $opn a = $a" #'$opn' echoes 5 - '$a' echoes 8
'---'
let "a = ((b = 5+3, ((2-1)) + ((3-2)) ))"
echo "a = $a" #'$a' echoes 2
let "c = $b+$a"
echo "c = $c" #'$c' echoes 10
'---'
#hexadecimal values
echo "$((0x9abc)) $((0x9abd))" #39612 & 39613
###################
###INTERNAL VARS###
###################
$BASH #bash path
$BASH_ENV #bash startup "#!/usr/bin/env bash"
$BASHPID #bash process ID - not the same as "$$"
$BASH_VERSION[n] #details about bash version
$CDPATH #'cd' command search path
$DIRSTACK #'dirs' command stack
$EDITOR #default editor (vi/emacs)
$EUID #"effective" user ID
$FUNCNAME #name of current function
func ()
{
echo "$FUNCNAME executing" #echoes "func"
}
$GLOBIGNORE #filename patterns excluded from matching
$GROUPS #groups current user belongs to
$HOME #user home directory
$HOSTNAME #set by gethostname() function
$HOSTTYPE #hardware architechture - like "$MACHTYPE" but less detailed
$IFS #internal field separator (fields, word boundaries, character strings)
$IGNOREEOF #ignore end-of-file
$LC_COLLACE #controls collation in filename expansion
$LC_CTYPE #cotrols character interpretation in globbing/pattern matching
$LINENO #line number where this variable appears
$MACHTYPE #...
$OLDPWD #previous working directory
$OSTYPE #...
$PATH #path to binaries
$PIPESTATUS #holds exit status
$PPID #process ID
$PROMPT_COMMAND #
$PS1 #primary prompt
$PS2 #secondary...
$PS3 #...
$PS4 #...
$PWD #working directory
$REPLY #"read" command default value
$SECONDS #...
$SHELLOPTS #lists enabled shell options
$SHLVL #shell nesting level
$TMOUT #shell login time limit
$UID #user ID
#######################
#positional parameters#
#######################
$0, $1, $2, ... #parameters supplied to the script
$# #number of arguments/positional paramenters
$* #all parameters (must be quoted)
$@ #same as "$*" (should be quoted)
#########################
####special parameters###
#########################
$- #tests script interactivity
$! #last backgorund process ID
$_ #command final argument
$? #exit status
$$ #script own PID
#########################
######type setting#######
#########################
declare -r #readonly (same as 'readonly var')
declare -i #integer
declare -a #array
declare -f #function
declare -x #export
declare | grep HOME #echoes variable
#declare | grep SHELL
#declare | grep UID
var=1
declare | grep var
$RANDOM #generates random integer (0 - 32767)
##################
######STRINGS#####
##################
#string lenght
string=abcABC123CBAcba
echo ${#string}
echo `expr lenght $string`
echo `expr "alph" : '.*'`
#matching substring beginning - substring=regular expression
echo `expr match "$string" 'abc[A-Z]*.2'`
echo `expr "$string":'abc[A-Z]*.2'`
#index
echo `expr index "$string" C12`
echo `expr index "$string" 1c` #'c' position before 1
#extraction
echo ${string:0} #extract from position 0/echoes "abcABC123CBAcba"
echo ${string:1} #from position 1/echoes "bcABC123CBAcba"
echo ${string:7} #.../echoes "23CBAcba"
echo ${string:7:3} #extract 3 characters including position 7/echoes "23A"
echo ${string:(-4)} #from right to left extract 4 characters/echoes "Cabc"
echo ${*:2} #echoes second and following parameter
echo ${@:2} #same as above
echo ${*:2:3} #echoes three positional paramenters starting at the second one
#expr substr $string $position $lenght
echo `expr substr $string 1 2` #start at char 1 extract 2
echo `expr substr $string 4 3` #start at char 4 extract 3
#expr match "$string" '\(regex\)'
echo `expr match "$string" '\(.[b-c]*[A-Z]..[0-9\)'`
#expr "$string" : '\(regex\)'
echo `expr "$string" : '\(.[b-c]*[A-Z]..[0-9]\)'`
echo `expr "$string" : '\(.......\)'`
#expr match "$string"'.*\(regex\)'
echo `expr match "$string" '.*\([A-C][A-C][A-C][a-c]*\)'`
#expr "$string" : '.*\(regex\)'
echo `expr "$string" : '.*\(......\)'`
#SUBSTRING REMOVAL#
#${string#substring}
echo ${string#a*C}
#${string##substring}
echo ${string##a*C}
#substring parameterization
X='a*C'
echo ${string#$X}
echo ${string##$X}
#${string%substring}
EXT=TXT
ext=txt
for i in ${ls *.$EXT}
do
mv -f $i ${i%.$EXT}.$ext
#replaces 'TXT' with 'txt'
done
#${string%substring}
echo ${string%b*c} #abcABC123CBAc
#${string%%substring}
echo ${string%%b*c} #c
#substring replacement
#${string/substring/replacement}
echo ${string/abc/xyz} #replaces first "abc" w/ "xyz"
#${string//abc/xyz}
echo ${string//abc/xyz} #replaces all "abc" w/ "xyz"
#parameterization
char=abc
repl=000
echo ${string/$char/$repl} #000ABC123CBAcba
echo ${string//$char/$repl} #000ABC123CBA000
#${string/#substring/replacement}
echo ${string/#abc/XYZ} #replaces first match w/ "XYZ"
#${string/%substring/replacement}
echo ${string/%abc/XYZ} #replaces last match w/ "XYZ"
########################
#parameter substitution#
########################
#${parameter}
#":" makes result different only when params are declared but null
id=${USER}-on-${HOSTNAME} #concatenating variables
echo "$id"
PATH=${PATH}:/opt/bin #temporarily adds "/opt/bin" to $PATH
#${parameter-default}, ${parameter:-default}
var1=1
var2=2
echo ${var1-$var2}
echo ${var3-$var2} #unset var3 now set to value of var2
echo ${user-`whoami`} #unset var "user" echoes result of "whoami" command
#${param=default}, ${param:=default}
echo ${var=abc} #
echo ${var=xyz} #if var unset set it to default/var already set so no change
#${param+alt_val}, ${param:+alt_val}
a=${param1+xyz}
echo "a = $a" #a =
param2=
a=${param2+xyz}
echo "a = $a" #a = xyz
param3=123
a=${param2+xyz}
echo "a = $a" #a = xyz
#${param?err_msg}, ${param:?err_msg}
: ${ZZX?"ZZX not set"} #ZZX not set, script exits with error msg
############################
#var length/subtrng removal#
############################
${#var} #length of $var
${var#pattern}
${var##patter} #remove shortest (#) or longest (##) front-end match of $pattern in $var
${var%pattern}
${var%%pattern} #remove shortest (%) or longest (%%) back-end match of $pattern in $var
${var:pos} #$var expanded from "position"
${var:pos:len} #$var expansion to max "length" from "position"
${var/pattern/replacement} #match of "pattern" in $var replaced by "replacemente"
#if "replacement" omitted "pattern" gets deleted
${var//patt/replc} #all matches of "patt" get replaced bu "replc"
#same as above
${var/#patt/replc} #if $var prefix matches "patt" substitute "replc" for "patt"
${var/%patt/replc} #if $var suffix matches "patt" subts "replc" for "patt"
${!varprefix*}
${!varprefix@} #matches all previously declared variables with "varprefix"
#################
######LOOPS######
#################
#for arg in [list]
#do
# command(s)
#done
#
for arg in [list]; do #if do is in same line ";" is needed
for planet in Mercury Venus Earth .....p
do
echo $planet
done
#
filelist="/path/tofile/file1
/path/tofile/file2
/path/tofile/file3
/path/tofile/file4"
for file in $filelist
do
if [ ! -e "$filelist" ]; then
echo "$filelist none existent"; echo
continue
fi
done
#
for file in * #filename globbing
do
ls -l "$file"
done
#
for file in [jx]* #list files beginning with "j" or "x"
do
rm -f $file #remove them
done
#
passwd=/etc/passwd
#filtering file content (field 1)
for user in $(awk 'BEGIN{FS=":"}{print $1}' < "$passwd")
#
echo_blah ()
{
echo "blah blah blah"
}
for word in $(echo_blah) #takes function (echo_blah) output
do
echo $word
done
#
LIMIT=10
for ((a=1; a <= LIMIT; a++)) #C-like syntax
#
#the comma permits concatenating operations
for ((a=1, b=1; a<=LIMIT; a++, b++))
do # ^ ^ incrementing two variables at the same time
echo -n "$a-$b"
done
#
for ((n=1; n<=10; n++))
{
echo -n "* $n *"
} #replacing "do-done" with "{}"
#
#while
var=0
LMT=10
while [ "$var" -lt "$LMT" ]
do
echo -n "$var"
done
#
while [ "$var" -lt "$LMT" ]; do #if "do" on the same line ";" is needed
echo -n "$var"
done
#
var=unset
previous=$var #multiple conditions while
while echo "previous-var = $previous"
echo
previous=var
[ "$var" != end ]
do
echo "Input variable #1 (end) "
read var
echo "variable #1 = $var"
done
exit 0
#
t=0
condition()
{
((t++))
if [ $t -lt 5 ]
then
return 0
else
return 1
fi
}
while condition #while + function
do
echo "Still going: t = $t"
done
#
cat $file |
while read line
do
...
done
#
#
while read value
do
rt=$(echo "scale=$SC; $rt + $value" | bc)
((ct++))
done
am=$(echo "scale=$SC; $rt / $ct" | bc)
echo $am; return $ct
} <"$datafile"
#
#until
#do
# command
#done
#until [condition-is-true];do
LMT=10
var=0
until ((var > LMT))
do
echo -n "$var"
((var++))
done
exit 0
#
#
#nested loops
outer=1
for a in 1 2 3 4 5
do
"$outer in outer loop"
done
inner=1
for b in 1 2 3 4 5
do
echo "$inner in inner loop"
let "inner+=1"
done
let "outer+=1"
echo
done
exit 0
#
#
#break
LMT=19
echo "Print 1 through 20, except 3 & 11"
a=0
while [ $a -le "LMT" ]
do
a=$(($a+1))
if [ "$a" -eq 3 ] || [ "$a" -eq 11 ]
then
continue
fi
echo -n "$a "
done
echo; echo
echo Printing 1 through 20, something happens after 2.
#
#
a=0
while [ "$a" -le "LMT" ]
do
a=$(($a+1))
if [ "$a" -gt 2 ]
then
break
fi
echo -n "$a "
done
echo; echo; echo
exit 0
#
#
for outerloop in 1 2 3 4 5
do
echo -n "Group $outerloop: "
for innerloop in 1 2 3 4 5
do
echo -n "$innerloop "
if [ "$innerloop" -eq 3 ]
then
break
fi
done
echo
done
echo
exit 0
#
#
for outer in I II III IV V #outer loop
do
echo; echo -n "Group $outer: "
#----------------------------
for inner in 1 2 3 4 5 6 7 8 9 10
do
if [[ "$inner" -eq 7 && "$outer" = "III" ]]
then
continue 2 #continue loop on 2nd level "outer loop"
#replace above line with a simple "continue"
#to see normal loop behavior
fi
echo -n "$inner " # 7 8 9 10 will not echo on "Group III"
done
#--------------------------
done
echo; echo
exit 0
#
#
while true
do
for n in .iso.*
do
[ "$n" = ".iso.opts" ] && continue
beta=${in#.iso.}
[ -r .Iso.$beta ] && continue
[ -r .lock.$beta ] && sleep 10 && continue
lockfile -r0 .lock.$beta || continue
echo -n "$beta: " `date`
run-isotherm $beta
date
ls -alF .Iso.$beta
[ -r .Iso.$beta ] && rm -f .lock.$beta
continue 2
done
break
done
exit 0
#
#
#testing & branching
#case(in)/esac
#case "$var" in
#"$condition1"
#command
#;;
#"$condition2"
#command
#;;
echo; echo "Hit a key..."
read key
case "$key" in
[[:lower:]] ) echo "Lowercase!";;
[[:upper:]] ) echo "Upper!";;
[0-9] ) echo "Digit!";;
* ) echo "Other";;
esac
exit 0
#
#
#menus using case
clear
echo "Contacts"
echo "Choose person: "
echo
echo "[E] - Evans"
echo "[J] - Jones"
echo "[S] - Smith"
echo
read person
case "$person" in
"E" | "e")
echo
echo "Roland Evans"
echo "4444 Street"
echo "(111) 222-3333"
;;
"J" | "j" )
echo
echo "Mildred Jones"
echo "3344 Street"
echo "(111) 222-3333"
;;
* )
echo
echo "Not registered"
;;
esac
echo
exit 0
#
#
#testing command-line parameters
case "$1" in
"") echo "Use: ${0##*/} <filename>"; exit $E_PARAM;;
#${0##*/} is ${var##patter} param substitution
-*) FIMENAME=./$1;;
* ) FILENAME=$1;;
esac
#
#
#menus using select
PS3='Choose vegetable: '
echo
choice_of()
{
select vegetable
do
echo
echo "favorite vegetable $vegetable"
echo
break
done
}
choice_of beans rice carrots radish spinach
# $1 $2 $3 $4 $5
exit 0
#
#
######################
#command substitution#
######################
#commands inside backticks
scr_name=`basename $0`
#to avoid errors is better to use
#xargs rm -- < filename
#using command output for another command
rm `cat filename`
#storing a list of files w/ specific extension
txt_files=`ls *.txt`
echo $txt_files
#
#same result
text_files=$(ls *.txt)
echo $text_files
#echoing unquoted removes
#trailing newline characters
#preserving trailing newline
dir_lst=`ls -l`
#
#scrambled output
echo $dir_lst
#organized output
echo "$dir_lst"
#setting content of files as vars
var1=`<filename`
var2=`cat filename`
#assigning a variable is not needed
echo "` <$0`"
#assigning a loop output to a variable
var1=`for i in 1 2 3 4 5
do
echo -n "$i"
done`
#
#var1 = 12345
echo "var1 = $var1"
#
i=0
var2=`while [ "$i" -lt 10 ]
do
echo -n "$i"
let "i += 1"
done`
#
#var2 = 0123456789
echo "var2 = 0123456789"
#$() is prefered to backticks "``
file1=$(cat $filename)
file2=$(<$filename)
#it also treats backslashes different
echo `echo \\`
#no output
#
echo $(echo \\)
#outputs \
#
#it also allows nesting
words=$( wc -w $(echo * | awk '{print $8}') )
##############################
#####ARITHMETIC EXPANSION#####
##############################
#variations
#using backticks
z=`expr $z + 3`
#expr performs the expansion
#
#double parentheses and let
#constructions replaced backticks
z=$(($z+3))
z=$((z+3))
#
#no assignment needed
n=0
echo "n = $n"
#n = 0
#
#(($n +=1)) is incorrect
(( n += 1 ))
echo "n = $n"
#n = 1
#
#quotes allow the use of spaces
let "z += 3"
#same as
let z=z+3
#
#
##################################
##INTERNAL COMMANDS AND BUILTINS##
##################################
#ECHO
#prints to stdout an
#expression or variable
echo Hello
echo $a
#-n supresses terminal newline
#echo requires -e to print
#escaped characters
#using echo and command substitution
#to set a variable
a=`echo "hello" | tr A-Z a-z`
# ^converts all uppercase into lowercase
#
#
#PRINTF
#printf allows to print formatted strings
#
declare -r pi=3.14159265358979
declare -r dconst=31373
printf "Pi 2 decimal places = %1.2f" $pi
#
#printing a linefeed
printf "\n"
#
#inserting tab
printf "Constant = \t%d\n" $dconst
#
#formatting error messages
ERROR_DIR=609
var=nonexistent_dir
error()
{
printf "$@" >&2
echo
exit $ERROR_DIR
}
cd $var || error $"%s doesn't exist!" "$var"
#
#
#READ'
#
#setting a variable value
echo -n "Input var1 value: "
read var1
''#no $ in front of var1 needed
#
#setting multiple variables
echo -n "Input var1 and var2 values: "
read var1 var2
''#variables need to be separated
#by space or tab
#
#if read is not supplied any variables
echo -n "Enter value: "
read #no variable supplied
var="$REPLY"
echo "\"var\" = "$var""
#
#discarding values
#in a code block
{
read #avoids reading line #1 of the file
read line2 #reads line #2 of the file
} <$0 #script redirects to itself
echo "Line 2 of the file is: "
echo "$line2"
echo
#
#multi-line input
echo "Enter a string ending in \\ then press <ENTER>."
echo "Enter a second string w/o \\ then press <ENTER>."
read var1
echo "var1 = $var1"
#var1 = first line second line
#the \ supresses the newline
#
echo "Enter another string terminated by \\"
read -r var2
#the -r causes the \ to be read literally
echo "var2 = $var2"
#first line \
#
#reading keystrokes w/o hitting enter
read -s -n1 -p "Press key " keys
#these option need to be in the same
#order presented in this writing
echo; echo "Key pressed was "\"$keys\""."
#
#detecting arrow keys
arrowup='\[A'
arrowdown='\[B'
arrowrt='\[C'
arrowleft='\[D'
insert='\[2'
delete='\[3'
succ=0
other=65
echo -n "Press a key... "
read -n3 key
echo -n "$key" | grep "arrowup" #check character detection
if [ "$?" -eq $succ ]
then
echo "Up-arrow pressed"
exit $succ
fi
#...
#code is too long :(
#...
echo "Other key pressed"
exit $other
#
#reading a variable from a file
read variable <filename
echo "variable = $variable"
#variable is set to the first line of 'filename'
#
#reading the whole file
while read line
do
echo "$line"
done <filename
#
#
#pushd/popd/dirs
#
#pushd pushes a directory into
#the top of the directory stack
pushd directoryname
#and changes the working directory
#to directoryname
#
#popd removes the directory at the
#top of the directory stack
popd directoryname
#
dir1=/tmp/directory
dir2=/usr/directory
#changes $PWD to dir1
pushd $dir1
#changes to $dir2
pushd $dir2
#
#pops the current directory on top (dir2)
popd
#dirs will list the directories
#in the stack
dirs
#
#
#let
#makes arithmetic operations on variables
#
let a=11 #same as 'a=11'
let a=a+5 #same as let "a = a + 5"
echo "11 + 5 = $a"
#
#let "a = a << 3"
let "a <<= 3"
echo "\"\$a\" (=16) left-shifted 3 places = $a"
#128
#
#let "a = a / 4"
let "a /= 4"
echo "128 / 4 = $a"
#32
#
#let "a = a - 5"
let "a -= 5"
echo "32 - 5 = $a"
#27
#
#let "a = a * 10"
let "a *= 10"
echo "27 * 10 = $a"
#270
#
#let "a = a % 8"
let "a %= 8"
echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)"
#
#C style operators
#increment
let a++
echo "6++ = $a"
#7
#
#decrement
let a--
echo "7-- = $a"
#6
#
#trinary operator
#
#true
let "t = a<7?7:11"
echo $t
#7
#
#true
let a++
let "t = a<7?7:11"
echo $t
#11
#
#
#eval
#evaluates strings and
#converts them into a command
a="echo"
b="Hello "
c="world!"
eval $a $b $c
#Hello world!
#
#linefeeds
y=`eval ls -l`
#linefeeds removed
echo $y
#linefeeds preserved
echo "$y"
#
#printing a sequence of variables
eval "`seq 3 | sed -e 's/.*/var&=ABCDE/'`"
#var1=ABCDE, var2=ABCDE, var3=ABCDE
#
#selecting from multiple variables
arr0=( A B C D E )
arr1=( F G H I J )
arr2=( K L M N O )
choose_arr ()
{
eval arr_memb=\${arr${arr_numb} [elem_numb] }
echo "Element $elem_numb of $arr_numb is $arr_memb"
}
#
#echo parameter number
params=$#
param=1
while [ "$param" -le "$params" ]
do
echo -n "Paramter No. "
echo -n \$$param
echo -n " = "
eval echo \$$param
(( param ++ ))
done
exit $?
#
#evaluating an expression
eval ${1}+=\"${x} ${y} \"
#
#
#set
#changes variable values
#
#setting a positional parameter
set `uname -a`
#
#reverse positional parameters
set a\ b c d\ e;
OIFS=$IFS; IFS=:;
echo
until [ $# -eq 0 ]
do
echo "### k0 = "$k""
k=$1:$k;
echo "### k = "$k""
echo
shift;
done
set $k
echo -
echo $#
echo -
echo
for i
do
echo $i
done
IFS=$OIFS
exit 0
#
#reassingning positional parameters
vars="one two three four five"
set -- $vars
f_parm=$1
s_parm=$2
shift; shift
r_parms="$*"
echo
echo "first param = $f_parm"
echo "second param = $s_parm"
exit 0
#
#
#unset
#deletes shell variables
#
#unsetting a variable
var=hello
echo "variable = $var"
unset var
echo "var after unset = $var"
#var is null
#
#
#export
#makes variables accessible to all
#child processes of the script
#
#exporting a variable to environment
column_nmb=$2
export column_nmb
#
#
#getopts
#getopts uses $OPTIND and $OPTARG to attach
#options in getopts constructs
#
#the : indicates the option 'c' will be
#passed an argument (d e)
while getopts ":abc:de" Option
do
case $Option in
a ) #do something w/ 'a'.
b ) #do something w/ 'b'.
c ) #do something w/ 'c' and $OPTARG.
esac
done
shift $(($OPTIND - 1))
#decrements the argument pointer
#so it points to the next one
#
#
#source or . (dot command)
#invokes/loads a script within a script
#
#loads data-file if it is executable
. data-file
#same effect as "source data-file"
#data-file must be present in the
#current working directory
#
#referencing data from within data-file
echo "var1 = $var1"
echo "var3 = $var3"
#
let "sum = $var2 + $var4"
echo "sum = $sum"
#
echo "message1 in data-file is \"$messg1\""
echo "message2 in data-file is \"$messg2\""
#calling a function inside data-file
messg_print
#
#passing arguments to the sourced file
source $file $arg1 $arg2
#
#
#shopt
#allows to change shell options on the fly
#
#correct directory spelling mistakes
shopt -s cdspell
#cd into unexistent directory
cd /hpme
pwd #/home
#the shell corrected the directory's name
#
#
#caller
#
#returns the line number, function and
#script name where it is located
#
one_function ()
{
caller 0
}
#must be inside a function
#
#true
#
#returns a successful exit status (0)
true
echo $?
#0
#
#endless loop
while true
do
command
command
command
done
#
#false
#returns an unsuccessful exit status (1)
#
false
echo $?
#1
#type
#
#identifies builtin/external commands
#type 'command'
type '['
#type [
#both are the same
#
#the -a option locates commands
type -a '['
#[ is a builtin
#[ is /usr/bin/[
#
#jobs
#lists jobs running in background mode
#
jobs
#jobs identifiers:
#%N = job number (N)
#%S = job begins with string S
#%?S = contains string S
#%% = current job (last started in background or
#stopped in background)
#%+ = current job (last started in background or
#stopped in background)
#%- = last job
#$! = last background process
#
#removing jobs from the list
disown
#
#wait
#
#suspend execution until all jobs
#running are terminated
updatedb /usr &
wait
#the rest of the script won't run until
#"updatedb /usr &" exits
#
#times
#displays execution time stats
#
times
#0m0.020s
#command
#
#disables command directive aliases and functions
command ls
#builtin
#
#runs commands as builtin temporarily disabling
#functions and external system commands w/ the same name
builtin command
#
#enable
#
#enables or disables builtin commands
#to make the shell run external commands
enable -n kill
#the shell will have to use /bin/kill
#
#autoload
#
#loads functions from external files (at first run)
#prevents function names conflicts
autoload -Uz functionname
#marks the function for autoloading, supress alias expansion
#and use zsh to load it
#
#cat/tac
#
#opens a file to stdout and concatenates
#files when combined w/ redirection (>, >>)
cat filename
#
cat file1 file2 > file3
#concatenates file1 & file2 into file3
#-n enumerates the lines
#-b enumerates the blank lines
#tac does the inverse to cat
#prints the file from end to start
cat file3
#line 3
#line 2
#line 1
#
#rev
#
#prints the lines of letter in a file
#in reverse order
rev file3
#1 enil
#2 enil
#3 enil
#
#cp
#
#copies files, overwrites files if they
#already exists
cp file1 file2
#overwrites file2 if it exists
cp -u orig_dir/* bck_dir
#copies the whole directory preventing
#identically named newer files to be overwritten
cp -a orig_dir/ bck_dir/
#copies all files in a directory tree
#
#mv
#
#moves files
mv orig_file target_dir/
#overwrites the file if it exists in
#the target directory
mv -f orig_file target_dir/
#moving a directory
mv orig_dir/ dest_dir/
#ask confirmation before overwriting files
mv -i file1 dest_dir/
#do not overwrite files
mv -n file1 dest_dir/
#
#rm
#
#remove files
rm file1
#removing a dashed-file (-filename)
rm -file2
#outputs rm: invalid option
#to delete a dashed-file
rm -- -file2
#or
rm ./-file2
#
#mkdir
#
#makes a directory
mkdir -p code/c/libs/asm #creates needed parents directories
#
#chmod
#
#changes attributes of files/directories
chmod +x file1 #makes file1 executable
#
chmod u+s file1 #sets "suid" permissions
#so a user can execute file1 with the same
#privs of the file owner
#
chmod 644 file1 #makes file1 readable/writable to owner
#but readable to others
chmod 444 file1 #makes file1 read-only for all except root
#
chmod 1777 orig_dir/ #everyone has read, write and execute
#permissions in the directory but only
#the owner and root can delete files
chmod 111 orig_dir/ #everyone has execute-only permissions
#but nobody except root can list the files
#or find then using the find command
#
chmod 000 orig_dir/ #no permissions for that directory
#nobody can't read, write, execute or list
#the files but you can rename the directory
#or delete it if it is empty, can also link
#the files but can't read, write or execute them
#
#chattr
#
#changes file attributes
chattr -i file1 #sets file1 as inmmutable
#cannot be modified, linked or deleted
#only root can set it to be removed
#
chattr -s file2 #sets file1 as secure
#when the file is deleted its block
#is overwritten with binary zeroes
#
chattr -u file1 #sets the file as undelete
#when the file is deleted its contents
#can still be retrieved (undeleted)
#
chattr -c file2 #sets file2 to be automatically compressed
#it will be compressed on disk writes and
#uncompressed on disk reads
#
#ln
#
#creates reference (alternate name) to a file
ln -s oldfile newfile #creates a soft link called 'newfile'
#to the original file 'oldfile'
#
######################
###COMPLEX COMMANDS###
######################
#find
#
#execute commands on all the files
#matched by the searched options
find ~/ -name '*.txt' #find all files with .TXT extension
#
find ~/ -name 'core*' -exec rm {} \;
#find files with 'core' in their
#name and if found execute "rm" on them
#-exec (not the same as exec builtin)
#
find /home/user/ori_dir -mtime -1
#find files in the directory tree that
#were modified last 24 hours
#
find /home/user/ori_dir -mtime 1
#same but EXACTLY a day ago
#mtime = modification time
#ctime = change time
#atime = access time
#
DIR=/home/user/ori_dir
find "$DIR" -type f -atime +5 -exec rm {} \;
#find and delete all files with
#accesstime older than five days (+5)
#{} = placeholder for the path name
#"-type filetype"
#f = regular file
#d = directory
#l = symbolic link
#
find /etc -exec grep '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;
#find all IP's in /etc
#
find /etc -type f -exec cat '{}' \; | tr -c '.[:digit:]' '\n' \
| grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$'
#filter
#[:digit:] = digit character class (0 to 9)i
#
find . -name "* *" -exec rm -f {} \;
#find all files containing whitespace
#in their name and removes them
#
find . -name '*[+{;"\\=?~()<>&*|$ ]*' -maxdepth 0 \
-exec rm -f '{}' \;
#find all file inside $PWD with special characters
#in their names
#-maxdepth 0 = don't go inside subdirectories in $PWD
#
inum=`ls -i | grep "$1" | awk '{print $1}'`
find . -inum $inum -exec rm {} \;
#remove files based on their inode number
#
#xargs
#
#applies the given commands to another
#command's output, defaults to 'echo'
#
find ~/mail -type f | xargs grep "Linux"
#filters files including Linux inside them
#
ls | xargs -p -l gzip
#gzip every file in the current directory
#one at a time
#
find /usr/bin | xargs file
#executes the file command on every file
#shown in the output of the previous command
#
ls | xargs -n 8 echo
#-n 8 limits the output to eight columns
#
find / -type f -print0 | xargs -0 grep -liwZ GUI | xargs -0 rm -f
#-0 allows handling arguments containing whitespaces
#or quotes
#
grep -rliwZ GUI / | xargs -0 rm -f
#same as above
#
ls *gif | xargs -t -nl -P2 gif2png
#converts all .GIF to .PNG
#-P allows multiprocess execution (-P2 two processes)
#-t prints to stderr
#-n1 one argument per command line
#
LNS=5
( date; uname -a ) >>logfile
echo ---------------------- >>logfile
tail -n $LNS /var/log/messages | xargs | fmt -s >>logfile
#generate logs w/ tail end of /var/log/message
#/var/log/messages must be readable
#
tail -n $LNS /var/log/messages /var/log/messages | tr -d "\"'" | xargs | fmt -s >>logfile
#removes unmatched single/double quotes
#
NO_ARGS=1
if [ -z "$1" ]
then
echo "Use: `baename $0` dirtocopy"
exit $NO_ARGS
fi
ls . | xargs -i -t cp ./{} $1
#-t sets "verbose"
#-i sets "replace strings"
#{} placeholder for text
#
BADARGS=1
if [ -z "$1" ]
then
echo "Use: `basename $0` proctokill"
exit $BADARGS
fi
PROC_NAM="$1"
ps ax | grep "$PROC_NAM" | AWK '{print $1}' | xargs -i kill {} 2&>/dev/null
#-i sets "replace strings"
#{} are replacement placeholder
#
ARGS=1
BADARG=2
NOFILE=3
if [ $# -ne "$ARGS" ]
then
echo "`basename $0` filename"
exit $BADARG
fi
if [ ! -f "$1" ]
then
echo "File \"$1\" doesn't exist."
exit $NOFILE
fi
cat "$1" | xargs -n1 | \
#list file one word per line
#
tr A-Z a-Z | \
#change chars to lowercase
#
sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
/g' | \
#filter out periods, commas
#change spaces to linefeed
#
sort | uniq -c | sort nr
#remove duplicates, prefix occurrence count
#and sort numerically
#
#expr
#
#arithmetic operations evaluator
#
expr 3 + 5 #spaces are needed
#returns 8
#
expr 5 \* 3 #multiplication character
#must be scaped
#
var=`expr $var + 1` #incrementing a variable
#
var=`expr substr $string $position $length`
#extract substring of $length
#starting at $position
#
a=3
b=`expr $a \> 10` #comparing $a to 10
#returns 0 (false)
#"greater than" character
#must be escaped
#
a=3
b=`expr $a \< 10` #comparing $a to 10
#returns 1 (true)
#"less than" chacrter
#must be escaped
#
a=3
b=`expr $a \>= 10` #comparing $a to 10
#"greater than or equal to" must be escaped
#
a=3
b=`expr $a \<= 10` #comparing $a to 10
#"greater than or equal to" must be escaped
#
a=12345word54321
b=`expr length $a` #saving length of $a in $b
echo "Length of $a is $b" #returns 14
#
b=`expr index $a 23` #position of character in substring
echo "position of \"2\" in $a is \"$b\""
#that matches string (23)
#
b=`expr substr $a 2 6` #extract substring in position 2
echo "substring in position 2 and 6 chars long is \"$b\""
#and with length 6
#
#REGULAR EXPRESSIONS
#
b=`expr match "$a" '[0-9]*'` #counting number of digits
#
b=`expr match "$a" '\([0-9]*\)'`#matching digits at the beginning
#of $a
#using ":" to substitute match
#
a=123zip321
echo "string is \"`expr "$a" : '\(.*\)'`\"."
#escaped parentheses
#match a substring
#
echo "length of \"$a\" is `expr "$a" : '.*'`."
#length of string
#number of digits at the beginning
#
echo "digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`."
#number of digits at the
#beginning
#
echo "Digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`."
#counting digits of a string
#
echo "First 7 characters of \"$a\" are `expr "$a" : '\(.......\)'`."
#printing first seven chars of $a
#
echo "Last 7 characters of \"$a\" are `expr "$a" : '.*\(.......\)'`."
#printing last seven chars of $a
#
strp_space=`expr "$strp_space" : '[[:space:]]*\(.*\)[[:space:]]*$'`
#stripping white space from beginning to end.
#
###################
###date and time###
###################
echo "Number of days of current year: `date +%j`."
#printing number of days since the year's beginning
#
echo "Number of seconds since 1970: `date +%s`."
#printing number of seconds since 1970 (UNIX Epoch)
#
begn=temp #sets beginning of filename
extn=$(date +%s) #sets filename extension
filenm=$begn.$extn #combines $begn.$extn
#
echo "Temp filename = $filenm" #prints temporary filename
#
minhr=60
dayhr=24
diff () {
printf '%s' $(( $(date -u -d"$target" +%s) -
$(date -u -d"$curent" +%s)))
}
current=$(date -u -d '2003-02-01' '%F %T.%N %Z')
target=$(date -u -d '2003-02-01' '%F %T.%N %Z')
#-
date +%N | sed -e 's/000$//' -e 's/^0//'
#generating a random integer
#stripping leading/trailing zeroes
#
#at
#executes a set of commands
#at a given time
#
at 2:30 am Friday < commands-set.list
#executes the commands set
#friday at 2:30 AM
#
#sleep
#pauses execution for a
#given amount of seconds ()
#
sleep 3 #pauses for 3 seconds
#
sleep 3 h #pauses for 3 hours
#
#uniq
#
#removes duplicate lines
#
cat lst-1 lst-2 | sort | uniq > final.lst
#removes duplicate lines
#and writes output to final.lst
#
uniq -c final.lst #prefix number of ocurrences
#of each word
#
sort final.lst | uniq -c | sort -nr
#sorts the output in reverse numerical
#order (sort -nr)
#
sed -e 's/\.//g' -e 's/\,//g' -e 's./ /\/g' "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
#filters out periods, commas and
#linefeed then change characters
#to lowercase and prefix occurrence
#count and sorts numerically
#
#cut
#extracts fields from files
#
cut -d ' ' -f1,2 /etc/mtab #extracts the first and second
#fields, delimited by space
#-d is delimeter
#
cut -d: -f1 /etc/passwd #extracts all users from the
#passwd file
#
#paste
#merges different files together
#into a single, multi-column file
#
paste list-1.lst list-2.lst prices.lst
#merges list-1 with list-2
#into the single file prices.lst
#
#join
#joins the contents of two files
#together but only content with
#the same tag
#
join list-1.lst list-2.lst
#joins the contents of list-1
#with list-2
#
#list-1:
#100 shoes
#200 laces
#
#list-2:
#100 $40.00
#200 $15.00
#
#result:
#100 shoes $40.00
#200 laces $15.00
#
#################
#######GREP######
#################
#grep is a multi-purpose file search tool
#grep can use regular expressions to match
#patterns inside the files
#
grep '[rst]ystem.$' osinfo.txt
#searches occurrences of the word
#"system" inside osinfo.txt
#
ps ax | grep clock #filters the output of 'ps ax'
#to output instances of the word "clock"
#
grep -n Linux osinfo.txt #lists the line numbers where
#the words are matched
#
grep word1 *.txt | grep -v word2
#matches all lines in text files
#that contain word1 but not word2
#
grep -c text *.sgml #numbers of occurrences of "text"
#in all .sgml files
#
#egrep = extended grep [grep -E]
#fgrep = fast grep [grep -F]
#agrep = approximate grep
#
#wc
#prints word count to output
#
wc -w /path/to/file/filename.ext
#outputs word count
#
wc -l /path/to/file/filename.ext
#outputs line count
#
wc -c /path/to/file/filename.ext
#outputs byte count
#
wc -m /path/to/file/filename.ext
#outputs character count
#
wc -L /path/to/fiel/filename.ext
#outputs lenght of the longest line
#
ls *.txt | wc -l
#wc -l will print the number
#of lines of the first command
#
wc [a-c]* | grep total | awk '{print $3}'
#prints the total of the files
#whose names begin with the
#letter range a - b
#
#tr
#character translation
tr A-Z '[**]' <filename.ext #converts all uppercase letters
#into asterisks
#
echo "abcdef" | tr -d b-d #echoes aef
#
#the -d option removes a range
#of characters
#
tr -d 0-9 <filename.ext
#deletes all digits
#from the filename
#
echo "abccccc" | tr -s 'c'
#echoes abc
#
#--squeeze-repeats/-s deletes repeated
#instances of a character in a string
#
echo "abcde12345" | tr -c b-d +
#replaces every character with "+"
#except the elements inside the "b-d"
#range of characters
#
echo "abcde12345" | tr '[:alpha:]' -
#using POSIX classes replaces all
#letters inside the string with "-"
#
#iconv
#converts files from one encoding
#to another
myString="this string"
echo -n "$myString" | iconv -f UTF8 -t UTF16
#converts the contents of
#$myString from UTF8 to UTF16
#
######################
##archiving commands##
######################
#RPM
#RedHat Package Manager
#
rpm -qf /bin/
#shows the origins of a file
#
rpm -qa
#shows a list of installed RPM
#packages in a given system
#
rpm -i packagename.rpm
#installs a RPM package
#
#pax
#portable archive exchange
#makes periodic file back-ups
#
pax -wf daily_bkup.pax ~/server/files
#makes a back-up file called
#"daily_bkup.pax" of "~/server/files"
#
pax -f daily_bkup.pax
#lists the files in the archive
#
pax -rf daily_bkup.pax
#restores from linux machine to a BSD one
#
#diff
#file comparisson utility
#returns 0 if files are identical
#and 1 if the files are different
#
diff file1.ext file2.ext
#compares file1 with file2
#
diff -r ~/dir1 ~/dir1
#compares two directories
#recursively for the filenames
#
#zdiff
#compares zipped files
#
#cmp
#compares two files and
#and shows where they differ
#
cmp file1.ext file2.ext #comparing two files
#cmp will retunr 0 if they are
#identical and 1 if they differ
#
#split/csplit
#utilities for splitting files
#into smaller chunks
#
csplit file1.ext 4
#splits file1 into four chunks
#
#########################
#ENCODING AND ENCRYPTION#
#########################
#cksum
#
cksum /path/to/file/filename.ext
#prints a sum based on the contents
#of the file
#
echo -n "word" | cksum
#prints a sum of the word piped
#by the echo command
#
#uuencode/uudecode
#uuencodes files into ASCII characters
#uudecode decodes uuencoded files
#
uuencode filename.ext filename.ext > uuencodedfile.ext
#encodes filename.ext file and
#saves the output into the uuencodedfile.ext file
#
uuencode -m filename.ext filename.ext > uuencodedfile64
#encodes filename.ext file with base64
#format and saves the output into uuencodedfile64
#
uudecode -o filename.ext uuencodedfile.ext
#decodes uuencodedfile.ext and saves the
#output into filename.ext
#
#mimencode
#
#openssl
#Open source Secure Sockets Layer
#
openssl aes-128-ecb -salt -in filename.ext -out encrypted.filename -pass pass:p@sSw0rD
#encrypts filename.ext using the AES 128 bytes
#Eliptic Curve encryption algorhythm and
#saves it as encrypted.filename
#
openssl aes-128-ecb -d -salt -in encrypted.filename -out filename.ext -pass pass:p@sSw0rD
#decrypts encrypted.filename using the AES ECB 128 bytes
#algorythm and saves the output into filename.ext
#
tar czvf - ~/mydirectory/ | openssl des3 -salt -out encrypted_dir.tar.gz -pass pass:p@sSw0rD
#creates an encrypted copy of ~/mydirectory/ and
#saves it into encrypted_dir.tar.gz
#
#shred
#removes files and overwrites their content
#mktemp
#creates a temporary file with a unique filename
mktemp #creates a file with a random
#name tmp + chars long string
#
mktemp $var_name.XXXXXX #creates a file with the name
#stored in $var_name + six chars
#needs at least four place holders (XXXX)
#dos2unix
#converts DOS-formatted text files (CR-LF terminated)
#into UNIX format files (LF terminated)
#ptx
#outputs a permuted index
##########################
#####NETWORK COMMANDS#####
##########################
#host
#finds the IP address of a host
#using DNS
host mysite.com #"mysite.com has address 10.11.11.10"
#ipcalc
#does a reverse DNS lookup to find
#the hostname of an IP address
#
ipcalc -h 11.10.10.11
#HOSTNAME=yoursite.com
#
#nslookup
#does an internet name server lookup
#by ip address
nslookup -sil 11.10.10.11
#nslookup mysite.com
#server: 12.12.21.21
#address: 12.21.12.21
#
#dig
#makes an internet name server
#lookup on a host
dig -x 12.21.12.21
#10.11.11.10.in.ternet.host
#11.10.01.11.the.isp.hostname.net
#
#traceroute
#traces the route taken by packet
#sent to a remote host
traceroute 45.54.45.54
#lists the IP hops made by the
#packets sent to a host
#
#ping
#broadcast an ICMP ECHO_REQUEST
#packet to another machine
ping localhost
#64 bytes from localhost.localdomain
#
#finger
#retrieves information about users
#connected to a network
finger username
#shows general user information
#shell, home directory and address(?)
#ftp
#transfer files from/to a remote host(s)
#
#uucp (UNIX to UNIX copy)
#tranfers files between UNIX servers
#
#uux (UNIX to UNIX execute)
#execute commands on a remote system
#
#cu (Call Up)
#remote system to system connection
#
#wget
#downloads files or retrieves websites
wget -p http://www.website.com/file.html
#the -p parameter or --page-requisite tells
#wget to fetch all required files to display
#the specified website
#
wget -r ftp://ftp.server.net/file_folder/ -O $FOLDERNAME
#the -r parameter tells wget to retrieve all
#files recursively
#
wget -c ftp://ftp.server.net/file_folder/bigzippedfile.zip
#the -c parameter tells wget to resume
#a download if it gets interrupted
#
#lynx
#web and file browser
#
lynx -dump http://www.website.com/filename.html > $FILENAME
#the -dump option tells lynx to retrieve
#a file from a web server
#
#-transversal tells lynx to start crawling
#all links in a specified URL
#-crawl tells lynx to output the website
#to a log file
#
#rlogin (has security issues)
#initiates a session on a remote host
#
#rsh (has security issues)
#execute command on a remote host
#
#rcp
#copies files between different networked machines
#
#rsync
#synchronizes files between two different networked machines
#
rsync -a ~/pathto/thedirectory/*fileextension /host-1/pathto/thedirectory/
#ssh
#allows login into a remote host
#
ssh username@hostip
#scp
#copies files between two different networked machines
#
#write
#allows terminal-to-terminal communication in the local network
#
#netconfig
#allows configuration of a network adapter
#
#mail
#allows sending/reading e-mail messages
#
#mailto
#allows sending e-mail messages from the CLI also allows MIME messages
#
##########################
#####terminal control#####
##########################
#tput
#initialize/fetch terminal information
#
tput clear
#clears the screen
#
tput reset
#resets the terminal
#
tput cup 15 25
#moves the mouse to coordinates X:125 Y:250
#
#infocmp
#prints extended information about the current terminal
#
infocmp
#clear/reset
#clear the text out of a terminal screen
#reset terminal parameters and clears the screen
#script
#records all user keystrokes in the terminal
#
script keystrokes_log.txt
#saves pressed keystrokes into
#keystrokes_log.txt
#
script -a keystrokes_log.txt
#appends the new keystrokes to
#an existing file
#
script -c 'command' keystrokes_log.txt
#saves the results of a single
#command instead a shell session
#
script -c 'command' -q keystrokes_log.txt
#-q tells script to hide its output
#
script --timing=time.txt keystrokes_log.txt
#saves time execution information
#into the 'time.txt' file
#
scriptreplay --timing=time.txt keystrokes_log.txt
#scriptreplay replays the commands
#stored in keystrokes_log.txt file
#
exit
#stops keystroke recording
#stty
#displays or changes the characteristics
#of the terminal
#
stty -g
#saves terminal settings
#
stty -F /dev/tty7
#uses tty7 instead of stdout
#
stty -icanon -echo
#disables canonical/local echo modes
#
#######################
#####math commands#####
#######################
#factor
#decomposes an integer into prime factors
#
factor 25417
#27417: 3 13 19 37
#
#bc
#(extensible) basic calculator
#
#dc
#(extensible) desk calculator
#uses Reverse Polish Notation
#
#seq
#prints a sequence of integers
#
seq 10
#1
#2
#3
#4
#5
#6
#7
#8
#9
#10
seq -s : 10
#1:2:3:4:5:6:7:8:9:10
#
seq 50 100
#prints all numbers between 50 and 100
#
seq 50 5 100
#prints all numbers between 50 and 100
#in increments of 5
#
#yes
#prints the "y" character continuously
#it also allows to run command non-interactively
#
yes | fsck /dev/sda
#the command expecting user input
#will get it from yes instead
#
yes | rm -r directory
#removes 'directory' without waiting
#for user input
#
yes $PATH > verybigfile.txt
#parsing the variable $PATH the command
#creates a very big ASCII file
#
#printenv
#prints environment variables set for a user
#
printenv | grep SHELL
#shows the shell assigned to the user
#
#mkfifo
#creates a temporary FIFO buffer for
#date transfers between processes
#
mkfifo pipe1
#creates the named pipe "pipe1"
#
#dd
#data duplicator command
#
dd if=filename conv=ucase > filename.ext
#converts "filename" to all uppercase
#
dd if=$0 of=$0.copyname 2> /dev/null
#allows the script to copy itself
#
keypresses=100
dd bs=1 count=$keypresses 2> /dev/null
#captures 100 keystrokes
#
dd if=/dev/urandom of=filename bs=1 count=7
sync
rm -f filename
#securely deletes a file (filename)
#fills the file with random data with a blocksize of '1'
#seven times (count=7)
#flushes the buffers (sync) then remove the file (rm -f filename)
#
#od
#octal dump converts input and files
#to octal or other bases
#
head -c4 /dev/urandom | od -N4 -tu4
#hexdump
#dumps files in hexadecimal, octal or ASCII format
#
hexdump -C /bin/ls | less
#-C formats output in tabular form
#
#objdump
#shows information about binary/object files
#in hexadecimal or disassembled format
#
objdump -d /bin/ls
#-d parameter shows file information
#in disassembled mode
#
#mcookie
#generates a random magic cookie
#of 128-bit (32 characters) length
#
mcookie
#f3a4383d5751d693c8800fadcd71a660
#
#m4
#macro processing filter
#combines eval, tr and awk functionalities
#
#xmessage
#X-based pop-up message windows
#
#zenity
#GTK+ dialog widget displaying utility
#
#doexec
#allows to pass arguments to an executable
#
#dialog
#tools that allow displaying interactive
#dialog boxes from a script
#
#sox
#plays/performs sound files conversion
#
sox soundfilename.midi soundfilename.wav
#converts soundfilename.midi into
#a WAV file
#
#readelf
#shows information and statistics
#about an ELF binary file
#
readelf -h filename
#shows header file information
#about filename
#
#size
#shows segment sizes of an executable file
#
size filename
#text data bss dec hex filename
#7080 0080 680 678 98 filename
#logger
#allows to append a user generated message
#to the system log
#
logger This is a long ass message.
#adds the user suplied message
#to /var/log/messages
#
logger -t $0 -i Error after line 25.
#-t parameter specifies the logger entry tag
#-i records process ID
#
#logrotate
#allows rotation, compression, deleting or emailing
#system log files
#
#####################
#####JOB CONTROL#####
#####################
#ps (process statistics)
#list currently executing processes
#
#pgrep (ps + grep)
#pkill (ps + kill)
#
pgrep processname
#pstree
#lists running processes in tree format
#
pstree
#top
#display a list of processes
#
top -b
#-b tells top to display the list in
#text mode so it can be parsed or accessed
#from a script
#
#nice
#allows to manipulate a process' priority
#from 19 [lowest] to -20 [highest]
#
#nohup
#keep a process running even after
#user logs off
#
#pidof
#displays the Process ID of a running job
#
pidof processname
#fuser
#shows the processes accessing a file, set of
#files or direvtory
#
fuser -u /usr/bin/vim
#-u parameter shows the username
#
fuser -un tcp 25
#-n parameter shows processes accessing a port
#
#cron (crond)
#program scheduler
#
#########################
#####PROCESS CONTROL#####
#########################
#init
#determines the runlevel of the system
#
#runlevel
#shows the current and last runlevel
#
#halt/shutdown
#shut down a system
#
#reboot
#reboots a system
#
#service
#starts/stops a system service
#
##########################
#####NETWORK COMMANDS#####
##########################
#ifconfig
#network interface configuration tool
#
ifconfig -a
#-a tells ifconfig to show
#all network interfaces even if
#they're down
#
#netstat
#show network statistics
#
netstat -r
#-r tells netstat to show the
#routes
#
netstat -lptu
#-lptu tells netstat to show
#port listening shockets and the
#processes used by these sockets
#
#iwconfig
#command used to configure
#wireless networks
#
#ip
#general IP network configuration tool
#
#route
#shows information or modifies kernel
#routing table
#
#iptables
#packet filtering tool mostly used
#to set up firewalls
#
#chkconfig
#check network and system configuration
#
chkconfig --list
#tcpdump
#allows to analyze network traffic
#
####################
#####FILESYSTEM#####
####################
#mount
#allows a filesystem to be mounted
#
#umount
#allows to unmount filesystem
#
umount /mnt/cdrom
#sync
#forces a write of updated buffers
#to the hard drive
#
#losetup
#configures loopback devices
#
#mkswap
#creates a swap partition/file
#
#swapon/swapoff
#enable and disable swap partitions/files
#
#mkdosfs
#creates a DOS FAT filesystem
#
#hdparm
#lists or change hard disk parameters
#
#fdisk
#creates or changes the partition table
#
#fsck
#allows to check a UNIX filesystem
#
#badblocks
#checks for bad blocks on a device
#
#lsusb
#shows all active USB buses
#
#usbmodules
#shows information about USB driver
#modules
#
#lspci
#shows present PCI buses
#
#mkbootdisk
#mkisofs
#chroot
#lockfile
#flock
#mknod
#makedev
#tmpwatch
#dump
#restore
##################
#SYSTEM RESOURCES#
##################
#quota
#setquota
#umask
#rdev
#################
#####MODULES#####
#################
#lsmod
#insmod
#rmmod
#modprobe
#depmod
#modinfo
######################
#####MISCELLANEOUS#####
######################
#env
#ldd
#watch
#strip
#nm
#xrand
#rdist
'-o-o-o'
#
stat --format=%A file #prints permissions
stat --format=%U file #prints file/filesystem owner
stat --format=%s file #prints file size
stat --format=%F file #prints file type
#options combined
stat --format="%A %U %s %F" file
#byte order mark manipulation
sed '1s/^\xEF\xBB\xBF//' < orig.txt > new.txt
sed -i '1s/^\xEF\xBB\xBF//' orig.txt
# Deletes files containing these characters: + { ; " \ = ? ~ ( ) < > & * | $
badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p` \
rm $badname 2>/dev/null
EOF