Your linux Distro
- 💡
cat etc/*release
that’s the path containing the version of system you are using you can use the comand cat /etc/*release : and this will output all info regardin the re
know more about your computer
find out the space ram and all info you might want
-
💡 free -h Gives the data in human readable
-
💡 cat /proc/cpuinfo Output the file containing cp infos find out the cpu info of your computer :
-
💡 df -h Show space across few different volumes find space on the hardrive on in your computer what we need to see is the / since that’s our main disk
To obtain informations about your hardware use
- 💡 sudo lshw since it give lots of info it might be great if you pass it in the less command
- 💡 sudo lshw | less
Alias command
alias command this is use to change or modify one command to another alias aliasname=command alias c=clear From now on clear is simply c
- 💡 who,whoami These commanges are used to display the user name and sometimes it displays the
login time and the user names
- 💡 pwd: print the present working directory
- 💡 cal: used to show the calendar ,cal 2 2004 this will show the calendar of that exact month and year
- 💡 date: display the current date of today,date ‘+Date:%m-%y%nTime:%h:%mwho
- 💡 touch: used to create textfile touch name name name etc
- 💡 touch -c filename : avoiding creating new file if the file exists
- 💡 mkdir: usecd to create a directory (folder)
- 💡 cd :
$PATH variable displays where all the user’s program can be found
- 💡 echo $PATH
The cat command
who cat used to create or modify a text file with contents inside.
-
💡 Cat > filename
-
💡 cat filename to view the contents In the file , you
-
✅ can also use the cat command to merge different files into one .
-
💡 cat firstfile secondfile >
the_fileyouwant_all_thosethinfgstogo
-
💡 cat -b filename
-
✅ this will print the file content with numbering in the front but will not put number on blank space but cat -n filename will
-
💡 cat -s file name
-
✅ this kinda trim all extra new lines and make them one
-
💡 cat > nameoffile :
-
✅ used to write in a file and use cat >> to append to it
-
💡 mv :
-
✅ used to rename a file or move file,mv filetorename newname
Working with directories
-
💡 mkdir command#command
-
💡 mkdir directory_name
-
✅ used to create or make directories mkdir file_name
-
💡 mkdir -p to create directory structure many direcories at the same time
-
💡 mkdir -p one/thow/three
-
/one
- /Two
- /three
- /Two
if you wanna create many directories inside of one directories simply use
- 💡 mkdir -p one/{one,two,three,four,five}
- /one
- /two /three /four /five
Removing stuffs
-
💡 rmdir -option directoryname
-
✅ used to delete a directory
-
💡 rmdir directoryname
-
✅ delete directories which is empty
-
💡 rmdir -p one/two/three
-
✅ remove a list of direcories use -pv it’s better)
-
💡 rm used to remove files,to remove directories you must use rmdir or use
-
💡 rm -r name_of_folter
-
✅ delete anything on your way
cp command
-
💡 cp fineone filetwo this will compy contents of file one to file two and it will create the file two if it does not exist yet
-
💡 cp -i file_name location
-
✅ will be asking you if you wanna override in case there is a file with the same name on the location you are copying to
-
💡 cp -r folder destinatin
-
✅ copy a folder even if it contains something
-
💡 cp
-
✅ used to copy some files from one location to another
-
💡 cp filetobecopies whereuwantittogo/ifyouwantyoucangivetotheotherfileanewname
-
💡 ln
-
used to create links,soft and hark kind link, ln nameofthefileyouwannamakealinkof nameofthelink, to make a soft link you simply do
-
💡 ln -s
-
✅ whichfile whichname , soft link has a dependency on the linked file, if you delete the master then the soft link is useless
-
✅ you can also make links directly to a direcory ln -s Download Destop/link_to_download
less command
- 💡 less name_of_file this command help you open a part of a file in terminal and allow you to go down with the arrow to see the rest and hit space to see the next page and
press B to fo on the previous page press G to brows to the end Press g to go to the beginning /keyword to search from top to bottom ?keyword search from bottom to up
file permission commands
All files has permissions read and write permission etc..
the file permission read write execute has numeric values , read=4, Write=2, execute=1,
which means that the file at best can have a numerical value of 7 which is read+write+execute when you create an file in the os it has by default the read and write enabled but not the execute , means the file value is 6(read=4+write=2) (user group others) umask: umask is a system variable 0022 the first digit is not important but the 3 last are , it who one that decide a permission to a file or to older ,file=666 folder=777
Change permission
The ls command
#command#ls ls is used to list thins in a directory, ls -l used to list things in a directory with permissions ls -l > filename : you can use the > to same output of a command inside of a file ls directory/directory : used to check contents inside of a directory without opening it
you can use _ while card to locate a specifice file ls Desktop/_.mp3 this will print all mp3 files ls name this will looks for files and document with a name in it , very handy command
ls -d */: this is a command used to list out all the directories the slash makes sure that it does not list sub directories
ls -R used to show direcories structures directories and subdirectories
ls -la :this will do a long listing
ls -lah : long listing and will actually sort the things
permissions (list files with it’s details ). and use ls -a to • -rw-rw-r— indicate that the file is a text or a pic • drwxr-xr-x indicate that the file is a document
sudo command
#command#sudo sudo command is used damn you know what it does
- 💡 sudo -s
- ✅ Enter in super user mode
- 💡 sudo -k
- ✅ exit the supersuer mode
Top command
#command#top
top :this command prints out every process and everything happening in you pc and the usaage of your cpu
when in top just press s to change the refresh seconds
when in top press i to filter processes
when in top press k and enter the process id to kill it
and press q to exit top
kill command and the ps command
-
💡 pidof name_of_the_process
-
✅ The above will give you the id of a process
-
💡 kill pid
-
✅ kill a running process ,
-
💡 kill -KILL pid
- 💡 kill -KILL 4355
-
✅ The above command is much more strong , it force the process to be closed
-
💡 kill -9 pid
-
Also a very powerful command to kill a process
-
💡 ps -ux
-
✅ Command above used to find a list of all runnig processes and their pids
-
💡 ps -aux
-
✅ Is used to see all process runneed by all users
-
💡 ps -U username
-
✅ will give you processes being runned by particular users
-
💡 ps -c processname
-
✅ list all processes of the same name
echo command
#coomand echo is used to send something back to the screen
nameofvariable=“what you want your var to hold” : you can create variables and call them when you wnat
echo $variablename :used to display contents inside of your variables
echo -e “this \t is \n”: this will just allow the use of those super characters
Uname command
#command#uname uname: this is a comand to print different informations of your computer and kernel version uname -a: to print all information
.use shortcut control d to stop modifications on a file
file commands
#command#file#wc counting words lines characters of a file
-file *:command used to display everything with their type
-wc name_of_file: command will display 3things , number of lines,words,alphhabets in your file
or you can use wc -l name_of_file,wc - name_of_file:,wc -c name_of_file:
sort command
#command#sort sort: just type this and you will know sort file_name:used to sort content in file
arguments
- 💡 **sort -r inputfile.txt **
- sort in reverse order
- 💡 **sort -o filename.txt inputfile.txt **
- 💡 **sort filename.txt > inputfile.txt **
- this will send the output of one program to another one
- 💡 **sort -n filename: **
- this is for sorting numerically
- 💡 sort -nr filename:
- numeric reverse sort
- 💡 **sort -c filename: **
- check if filename is sorted
- 💡 sort -u filename.txt :
- sort and remove duplicates
cut through files
#command#cut cut -d”your delimiter here” -f”field you want to extract” example: cut -d”-” -f”1,3” file_name
covert and copy files
- 💡 dd
- ✅ used to convert text form uppercase to lower case example dd if=“File you want to convert” of=“your new file” conv=ucase dd can do a lots of things from converting to extract to .
Get help
#command#man#help man: used to know about a comand man cat: will display everything about the cat command
banner : this command prints things in big using asterics eg: banner this is cool
compress command
-
💡 compress used to compress a documents
-
💡 compress -v filename the -v is just there to show you on how many % it converted the file if you wat to see the converted file you will have to use zcat
-
💡 zcat filename.z if you want to uncompress the file just use
-
💡 uncompress nameofthefile.z
Head and tail command
#command#head#tail head: used to display 10 first lines of a file by default ex: head daniel.txt //this will display 10 first lines you can also so something like head -n13 daniel.txt
tail:used to display 10 last lines of a file ex: tail dan.txt //will display 10 latest lines of a file tail -n3 daniel.txt //show the latest 3 lines
The pipe symbol |
#command#pipe it is used to send the output of one comand to another command
cat daniel.txt | tail -n5 | cat -n the command above will display 5lates lines of my documents numbered from 1 to 5
less command :
#command used for paginating and providing navigation command less fille.txt
zip to compress
Used to zipe files
-
💡 zip newzippedfilename.zip file_you_want_to_zip
-
💡 zip picture.zip
*.png
this will zip all files under the png format to a zip called picture.zip -
💡 unzip picture.zip
The tar command
#command tar stand for tap archive , it also another way of grouping different files together and you can also add compression to it
tar cf mytar.tar *.png :this will group all png files together
tar cfz mytar.tar.gz myfile* : we used z to compress tar file once it is created(you can also use cfzv)
Extration
#command tar xfzv name_of_file_you_want_to_extract the which command which command is used to locate programs or executables in the system which ls which vlc which firefox etc…
Adding, modifying deleting users in Linux
#commadn#useradd#userdel#usermod
adduser command
This is the new way of adding a user, it let’s you :
- Add a user, then
- Adds him to the group
- Allow you to set password
- Set full Name
- room number
- work phone etc..
useradd command
#useradd The user add command is used to add a new user to our system.
Added users are stored in the etc/ file, in the configuration directory
Modify the shell to use bash instead of sh
useradd mark -m -s /bin/bash -g users -c “comment” where -m will create the default home user
where -s is the command for difault shell it allow you so use the /bin… where -g is the default group where -c is used for comment
userdel username: command used to delete a user and you must user
bashrc files .bashrcfile is a script that is executed whenever a new terminal session is started in interactive mode
super handy
view resources command
df : display disk and space and everything you need to know df -h: display them in human readable form
du : estimate disk space used by a folder again use du -h: for human readable du -s : does the job in summary format du -s -h: even better or du -sh
du -hd1 foldername : the d1 specify one level of details and h specify human readable
free -h: used to show memory and swap stuffs
watch command
#command this command runs scripts repeatedly at a given interval watch command namew and if you wanna change the interval then do something like
watch -n 4 command : this will put the refresh to 4 seconds watch -n 1 -d command: the -d will show the thing changin
find command
#command#find used to search for files in a directory hyerarchy
-
💡
find -name filename
will return the path of the file if it finds anything -
💡
find -mtime -2
will find all files created 2days ago -
💡
find -name "*.mp4*" -mtime -4
This will find all files created four days ago -
💡
find directory -name filename
This will look in a particulas directory a particular file name
if you wanna look in the current directory then use . and use / if you wanna start looking from the root directory
-
💡
find -name "[Dd]esk*"
-
✅ this will print everything which has desktop in it either lowercase of capitalcase d
-
💡
find . -name "daniel.txt"
-
✅ will find in the current directory a file with the name daniel
-
💡
find / -name passwd > text.txt
-
✅ this command will send all the output of the found file in the text file
-
💡
find / -name passwd 2> text.txt
-
✅ now what this will do , it will send error messages only to the text file 2 is the numeric calue for std errors
dev null path
#command#find whatever you send in the dev null path you never see them again
find / -name passwd 2> /dev/nul : here all the errors will be sent to dev null and they won’t display anymore
ls > /dev/null :nothing is gonna be displayed
dev urandom
used to generate random number
cat /dev/urandom : this will print bunch of random numbers but in the asci form
To run any application from the terminal
#command to run any app simply type the name of the app and boom firefox vlc
Use the & to run the app in the backgroun
To run the process of app in the back use & to run it in the backgorung vlc &
foreground fg
#command#fg used to get back to the program in the background
SHELL SCRIPTING
A shell scrip should have a .sh extention to run
#
is used for commen in shell scripting
so run a shell file you simply use ./filename.sh of alternatively you can use the : sh filename.sh or bash filename.sh
positional parameter are parameter that are given when you are runing a file and they are stored in 2 ,$3 etc..
set command
set is used to set the positional parameters set paramater1 parameter2 etc…
use echo * to echo all the variables
read command
you read input form users by typing read then where the variable will go
you can save the parameters in a variable then on the next line set other parameters and it will be fine
count the numbers of command line arguments or parameters
echo $# : this is the command which will help you do so
if you run a file and put an _ to the end , shell will set parameter as all the files or direcory in that location sh myscript.sh _
variable
to declare a variable simply type the name= variable
a=30 b=“hello worls”
echo ”a” //will output hello worls i am 30 echo ’a”’ //will output a
Adding attributes to variables
declare -i d=123 # d is an integer declare -r e=345#e is read-only can’t be modified declare -l f=“LOLCats”#f is lolcats converted to lowercase declare -u g=“LoLCats”#g is LOLCATS
Built-in Variables
echo $HOME#return user home directory Mac:/Userc/anonymous Linux:/home/anonymous
echo $PWD#return Current directory Mac:/Userc/anonymous Linux:/home/anonymous
echo $MACHTYPE#return the machine type Mac: x86_64-apple-darwin12 Linux: x86_64-pc-linux-gnu
echo $MACHTYPE#return the system name Mac: anonymous.local Linux: anonymous
echo $BASH_VERSION#return the version of bash Mac: 3.2.48(1)-release Linux: 4.2.25(1)-release
echo $SECONDS#return the Number of seconds the bash session has run
Linux: handy for timing
echo $0#return the name of the bash Mac: 3.2.48(1)-release Linux: daniel.sh
Command substitution
how you can assign a command to a variable
d=d#now d is the same with pwd
Arithmetic
d=2 e=$((d+3))#this should be wrapped inside (()) otherwise it will not work
#you can used to incremental and dec stuff from other programming lang
((e++)) echo $e # will be incremented by one ((e+=45)) ((e*=4)) ((e/=4)) etc…
something to remember is that point of floating values have no straight representation
f=f
Comparison operaion
expression this expression return 1 for false and 0 for true
less than [[b]]
greater than [[b]]
less than or = [[b]]
greater than or = [[b]]
Equal [[b]]
less than [[b]] NOTE THIS COMPARISONS DOES NOT WORK ON NUMBERS , TO USE COMPARISONS ON NUMBERS YOU ARE GONNA USE SOMETHING LIKE THIS:
NUMBER COMPARISON less than [[b]] greater than [[b]] less than or = [[b]] greater than or = [[b]] Equal [[b]] not than [[b]]
Logic operations logic and [[a && b]] logic or [[b]] Logic not ! $b
String null values check is a string or variables is null of not is null ? [[-z a]] example
a="" b=“cat” [[-z a && -n b]] # is a null and b is not null echo $?#the ouput is 0 Which is true
String concatenation
you can do somethig like
a=“Hello” b=“world” c=b echo $c # the output will be helloworld
Size of string
echo ${#a} : you can use the # symbol before a variable name to see how long the variable is
Substring
from the examble below you can also create a substring
d=${c:2}#will start creating the string d from c 3rd ltr
echo $d # the output will be lodanie there is no hel anymore
d=${c:3:5}#choose homany characteryou want to sub to
or d=${c: -4:3} # the space must be there
String replace
replace one instence/occurence ${Yourvariable/text_you_want_to_replace/txt_u_wanna_replace_with}
replace all instences/occurences just use 2// before the one u want to replace ${Yourvariable//text_you_want_to_replace/txt_u_wanna_replace_with}
example
fruit=“apple banana banana cherry” echo ${fruit/banana/mando} output: apple mango banana cherry
echo ${fruit//banana/mando} output: apple mango mango cherry
echo ${fruit/#banana/mando} will replace the banan only if it’s in the front at first string position
echo ${fruit/%banana/mando} will replace the string only if it’s at the end of a variable
echo ${fruit/b*/mando} you can use matchings also,this will replace everything starting with b
Style text in bash
echo -e ‘\033[34;42mColor Text\033[0m’ # the -e enable the styling
-e: tell echo that we are gonna be using the escape string \033[34;42m :escape sequence where m indicate the end of it
color text :the string we wanna print out
Using tput for changin colors
flashred=(tput setab 0; tput setaf 1; tput blink) #mark the erro.. red=(tput setb 0; tput setaf 4)#mar the colos os somet… none=(tput sgr0) #marks the end of the styling echo -e flashred “ERROR:“#red”Something went wrong”$none
printf command
printf is used just as the echo command but with more functionalities
ex
printf “Name:\t%s\nID:\t%04d\n” “Ilunga gisa” “23”
Names: \t:tabulation %s:string_holder \n:startnewline ID: \t:tabulation %04d:digit_holder_with_4dgts
TR COMMAND
Tr simply translate text
tr option1 option2 :where option one is text you want to translate and option2 is text you want to translate it to
echo my foot is great | tr o x#the output will be my fxxt is great it converted all teh o to x
echo my foot is great | tr mo ya :here m—>y and o—>a
echo footbar | tr {:lower:} {:upper:} :will translate all text from lowercase to uppercase
array
to declare an array it’f fairly easy
a=(“apple” “banana” “avocado” “pineaple”) echo ${a[2]}#will output avocado
b+=(“mango”)#this will append at the end of line
echo ${b[@]}#this is used to print out whole data within an array
echo ${[@]: -1} will print the last element of an array
associative arrays only works in bash 3 and above
declare -A myarr myarr[color]=blue myarr[“name”]=“ilunga gisa daniel” echo “My name is ” {myarr[color]}
if statement
can be written in many form :
if [ expression ] or if expression
or can contain in double paranthesis for integer comparison if (( expression ))
or can have no brackets at all if expression
AND whatever the comparison you are doing you can continue the if
either with: if expression;then
or with: if expression then echo “things to be done when expression is true elif expression2;then echo “second condition” else echo “what will be done if the expressions are false fi#this is simply there to close an if statement
Script Example
#!/bin/bash #A program to print lots of info in your computer
color=(tput sgr0) a=5 if [ a is greater than 4! else echo ”color not $end greater than 4!” fi
#we use the for regular expressions b=“we are the face of the world” #this regular expression check if there is an int in the a variable if [[ $a =~ [0-9]+]] then
if it finds a numebr
echo “there is indeed a number in this string” else echo “There is no numebr in this string sir” fi
While loop
i=0 while [ i ((i+=2)) done
ultil loop
j=0 until [ j ((j++)) done
for loop
for i in 1 2 3 do echo $i done //the output will be 1 2 3
it you wanna check from a large range , do something like for i in {1…100} #you can specify intervals also , {1..100..2}, will count by 2 do echo $i done
There is another variation of ForLoop statement which works exactly like that one above
for(( i=1; i<=10; i++ )) do echo $i done
Looping through an array
You can also look through an array also with:
arr=(“apple” “banana” “avocado” “pineaple”) for i in i done
or
ar=(“apple” “banana” “avocado” “pineaple”) for (( i=0; i<=3; i++ )) do echo {ar[i]} done
Looping in an associative array
declare -A arr
arr[“name”]=“ilunga gisa daniel” arr[“age”]=“23”
for i in ”i: {arr[i]}” #above {arr[$i]} done
You can also use command substituion in the for loop
it would look like this : for i in $(ls) …
Case Statement
a=“dog”
case $a in cat) echo “Feline”;; #test if the variable a has cat in it and display feline if true jad) echo “Item”;; #those ;; are there to indicate the ending of the case dog|puppy) echo “Canine”;; #we used the | to sppecify a list of things to match … *) echo “No possible match”;; #the statement above is alike the default in other languages
esac #statement above closes the case statement
Function
To declare a funtion in bash you simply type
function Funtion_name{ echo “whatever you want your funtion to be doing”; }
to call the funtion simply type its name
Funtion_name
Argumented funtion
funtion greetings { echo “Hi, $1” #the one above is an argument , }
#call our funtion with an argument looks like this
greetings Daniel #output will be , Hi, Daniel
#simple example of the if statement echo “Please type a name of a file ” read s t if mv t then echo “You file was renamed fine” fi
if else statemetn
if [ num -lt 10 ]#this state ment check that num variable is less than or equal to 10 if [ num -gt 10 ] for greater than or equal example of program
echo “Enter a number between 10 and 20” read num if [ num -gt 20 ] then echo “Number is greater than 20” else echo “The number is between 10 and 20” fi
Flags in shell
while getopts u:p: option; do case OPTARG;; p) pass=$OPTARG;; esac done
echo “User: pass”
#while executing the program we are gonna call it with those flags #and their values
./file.sh -u Danielerat -p woldword
ouput
“User: Danielerat / Pass: woldword
you can also check wether a flag was added simply do not use the : after the flag name
while getopts :u:p:abc option; do case OPTARG;; p) pass=OPTARG;; esac done
./file.sh -a -b -c
output:
the a flag was added the b flag was added the c flag was added User: anonymous / Pass: we are legion
and notice that we have used : before u , and used ? in our case statement , this is for capturing any other non specified tag
#if we try to call with -z flag it will show the ? echo
./file.sh -az
The a flag was added I don’t think i know about that z flag User: anonymous / Pass: we are legion
Reading user’s data
echo “What is your name? ” read name
echo “What is your password?” read -s pass #use the -s when you want the input to be silent aka not show on the screen while user is typing
read -p “what is your favorite animal ” animal
use -p when you want input to be entered on the same line
#we can also combine -p and -s read -p “what is your favorite animal ” -s animal
echo “Name: pass ,animal: $animal
read -p “[sudo] password fo name, Pass: animal”
Get user’s data using select
select variablename in “list” “of things” “to” “select” “in” do echo “You selected $animal!” break#breaking the loop so that it can not run forever
done
example 0000000
select animal in “cat” “dog” “cow” do echo “you selected $animal” break done
combined with a case
echo “Choose your favorite animal ? ” select option in “cat” “dog” “cow” “snake” “quit” do case $option in cat) echo “Cats like to sleep.”;; dog) echo “Dogs are brest Human friends.”;; cow) echo “Cow gives a good milk for sure”;; snake) echo “Snames are way too annoying.”; break;; quit) break;; *) echo “Can you please make a sane choice ?” esac done
Error tolerance in a script
Check readed data
read -p “What is your name? ” a #read the input of the user while -z $a#-z will check for null status do read -p “I need your name please: ” a #is it’s null then read prompt is printed out again done
echo “You are called $a” # in case the condition is false this execute
There is some case where you want to put a default value in case user does not select anything
read -p “What is your name? [john doe]” a #read the input of the user while -z $a#-z will check for null status do a=“john Doe” #In this case we innitialize the a variable to be john doe
done
echo “You are called $a” # in case the condition is false this execute
while with regular expressions
if [ $# -lt 3 ]; then cat <<- EOM this command required 3 agruments username ,user id, and favorite number EOM
else echo “username: 2” echo “fav num: $3”
fi#closing the if statement
expr tool
the expr tool help us perform mathmatical operations easily and it works
only with integer values
#example of how the expr tool works
a=43
b=17
expr b
expr b
expr b
echo expr $a / $b
echo b=expr $a % $b
when dealing with operators you must put space between the variablea and the operator and again what you have to do when you want to use * or () in you ccoding you have to escape them os course with the back slash
bc command
in alternative you can use the bc command which works well with floating points and all sorts of variable for reference use the code below
a=36.2
b=5.4342
c=echo $a + $b | bc
#not that there is no space between = and `echo…
echo a * a / $b
change the echo printing style
in order to instruct echo we have to use escape sequences
\n : for new line
\r :used to gnore text in front of it echo “WE are not the \r leaders of the toilet” the output will be leaders of the toilets
\t :horizontal tab
\b :ommit a character backward echo “Daniel is\b cool” output will be Daniel i cool
\033[1m :used to put a statement in bold to go back in normal mode you will use \033[0m
you can use 1,2,3,4…9 \033[1m , \033[2m ,\033[3m …\033[9m and they all have different patters
the tput command
tput clear: used to clear the terminal tput lines : display the total # rows on screen
tput cols:show the numbers of columns tput cup 12 30 : put the mouse cursor on row 12 column 20 tput bold: put text in bold
the echo $? command
that command above is used if you want to check that a particular command did execute or not it return 0 if a command was executed and 1 if it was not executed ex: mkdir dfe ; this must return 0 if you run echo $? after executing it
if statement
#simple example of the if statement echo “Please type a name of a file ” read s t if mv t then echo “You file was renamed fine” fi
if else statemetn
if [ num -lt 10 ]#this state ment check that num variable is less than or equal to 10 if [ num -gt 10 ] for greater than or equal example of program
echo “Enter a number between 10 and 20” read num if [ num -gt 20 ] then echo “Number is greater than 20” else echo “The number is between 10 and 20” fi operators
-lt:less than -gt:greater tan -eq:equal -ne: not equal to -le: less than or equal -greater than or equal -f check if file exist [ -f filename]
checking files in shell scripting
we are gonna use the -f to find the files echo “Enter a file name” read name if [ -f $name ] #use -d if you want to find a directory -c for character special files -x execute permission #-b images or video files -r if you have read permissin -w if you have write permission -s if your file is greater than 0 aka empty then echo “we did find the file” else echo “we did not find the file” fi
INFINITE SKILLS LEARN UBUNUT TRAINING
sudo apt-get upgrade
command used to check for packages that are out of dat and update them
sudo apt-get install packagename
used to install softwared or packages
sudo apt-get install emacs vlc postfix : this command will install those different things in the computer
uninstall applications
sudo apt-get remove packagename
or you can use autoremove to remove packages you nolonger use
sudo apt-get autoremove
this remove all packages that you are nolonger using
services management
services are programs that runs in the background
sudo service —status-all :check all the scripts and their status
to start a service simply write: sudo service servicename start
Process management
a process is a running instance of a program
ps -u: list all running process
ps a : used to show list of all processes that are running ps eaf : also works like ps a but it brings some additional info ps aux :show all porcess running no matter the user you are running as ps aux | grep firefox : this is used to narrow the list , it looks for a particular program
top:to bring the list of program which is constantly upgrading we are gonna use top
killing a process:
kill processid:kill a process by it’s id killall processName kill a process by its name
killall -9 firefox:the -9 kill a process no matter what’s happening
Grep and sed command
#command#grep#sed Grep Global Regular Expression Print command is used to search for text. can be used to search for file of string partten
- 💡 grep word_to_search_for your_file
Structure looks like this
- 💡 grep [options] pattern [files]
Options Description
- 💡 c : This prints only a count of the lines that match a pattern
- r : Search in a directory and sub directory recursively
- 💡 h : Display the matched lines, but do not display the filenames.
- 💡 i : Ignores, case for matching
- 💡 l : Displays list of a filenames only.
- 💡 n : Display the matched lines and their line numbers.
- 💡 v : This prints out all the lines that do not matches the pattern
- 💡 e exp : Specifies expression with this option. Can use multiple times.
- 💡 f : Takes patterns from file, one per line.
- 💡 E : Treats pattern as an extended regular expression (ERE)
- 💡 w : Match whole word
- 💡 o : Print only the matched parts of a matching line, with each such part on a separate output line.
example
grep -E :used with regular expressions
SED
sed OPTIONS… [SCRIPT] [INPUTFILE…] we use the sed comman to change one thing to another
sed ‘s/unix/linux/’ geekfile.txt //will replace all the occurence of the word unix by linux form the file geekfile
/n where n=anu number sed ‘s/unix/linux/2’ geekfile.txt // will replace the number of occurrences
Replace from nth occurrence $sed ‘s/unix/linux/3g’ geekfile.txt
echo “Welcome To The Geek Stuff” | sed ‘s/(\b[A-Z])/(\1)/g’ (W)elcome (T)o (T)he (G)eek (S)tuff
sed -n ‘s/unix/linux/p’ geekfile.txt //print only the replaced lines
sed ‘1,3 s/unix/linux/’ geekfile.txt replace strings in a range of line
deleting lines from a particular file sed ‘nd’ filename.txt sed ‘5d’ filename.txt
cat fileNmae | sed ‘s/daniel/gisa/’ : this program will find in the file everywhere it’s written daniel and change it to gisa
sed ‘s/daniel/gisa/g’ use the g to replace globaly instead of once on a line
Add a user to a particulas group
here is the parameters to put a user to a group sudo useradd -d /home/anonymous -s /bin/bash -g staff -m anonymous
use cron to automate tasks
sudo visudo :this is a file that contain settings for our sudo
if you want password to be prompted to you or any other thing regarding sudo then you can find it in there
use sudo rm -RF * : this command will erase everything on your system
Networking in UBuntu
ifconfig :see all interface on your computer ifconfig enp2s0 : show configurations of that particular interface
Looking at DNS informations
host www.google.com : this will give you the informations about that domain , ipaddress included
you can also use:
nslookup www.google.com
also used as host
dig www.mysite.com
also used as nslookup but with lots of details
so all the 3 are used for ipaddresses
changing Nameservers
changing Dns server you are pointing to: typicaly you have a local dns server (airtel server)
cat /etc/resolv.conf :here is the place where name servers are stored
watch video 06_03
Basic troubleshooting
ping 4.2.2.1 or ping www.lynda.com
traceroute 4.2.2.1
address resoution protocol
arp -a :show the arp table rooute netstat -rn netstat -an
netstat -an | less : used to see your web trafic
communicate with a host
step 1: create my own server nc -l -p 8000 step 2:connect to the localhost nc localhost 8000
RECOVER LOST DATA FROM A USB FLAS IN UBUNTU
step one: sudo apt-get install testdisk
step two: sudo photorec
then follow the instruction
Ubuntu Tips
bash expension fills things your you let’s say you want to creat 100files , instead of typing all of em out just type : touch file_{1..100}.txt
mkdir folder_{1..20}/{a..z}#will create 20folders and add a-z folders in ead
Commmand substitution
if you do date , it will print the date
but if you do echo date -hello there , it will not print the date so to substitute one command in another one use
echo $(date) - Hello from script
Alias and Unalias
alias o=echo now o is exaclty the same as echo
Curl
Used to send and recieve data on different protocols means it can send and download data frorm terminal
curl url -o filename
or you can use -O to five file the default download name
WGET
Also used for downloading stuffs but is much more tollerent to errors and much better according to daniel
use the command :wget url let’s say the downlooad it interrupted ,well no trouble for wget you can continue the download with wget url -c , this will continue form where the download broke from
SSH REMOTE ACCESS
first install openssh-server on the computer you want to access remotely
- sudo apt install openssh-server
2.check the status of ssh connections if it’s running or nor systemctl status sshd if it is not running we can use “systemctl start sshd” to start the server
3.The 3rd thing to check is to see if the firewall is allowing trafic to the ssh firewall(by default it runs on port 22 )
Let’s take a loook at the firewall with “sudo ufw status” it is inactive
- we need to add a rule to allow port 22 access we can do that with -sudo ufw allow 22/tcp after allowing we need to restart the firewall with: -sudo systemctl restart ufw
- Now let’s enable the firewall with: -sudo ufw enable Options Description -c : This prints only a count of the lines that match a pattern -h : Display the matched lines, but do not display the filenames. -i : Ignores, case for matching -l : Displays list of a filenames only. -n : Display the matched lines and their line numbers. -v : This prints out all the lines that do not matches the pattern -e exp : Specifies expression with this option. Can use multiple times. -f file : Takes patterns from file, one per line. -E : Treats pattern as an extended regular expression (ERE) -w : Match whole word -o : Print only the matched parts of a matching line, with each such part on a separate output line.