mirror of https://github.com/Idnan/bash-guide.git
minor updates
This commit is contained in:
parent
dd2818f77a
commit
2749a3154e
46
README.md
46
README.md
|
@ -24,7 +24,6 @@
|
||||||
|
|
||||||
### a. `export`
|
### a. `export`
|
||||||
Displays all environment variables and if you want to get detail of specific variable then use `echo $VARIABLE_NAME`
|
Displays all environment variables and if you want to get detail of specific variable then use `echo $VARIABLE_NAME`
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
export
|
export
|
||||||
```
|
```
|
||||||
|
@ -43,7 +42,6 @@ $ echo $SHELL
|
||||||
|
|
||||||
### b. `whereis`
|
### b. `whereis`
|
||||||
Finds out where a specific binary is on your system.
|
Finds out where a specific binary is on your system.
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
whereis name
|
whereis name
|
||||||
```
|
```
|
||||||
|
@ -55,7 +53,6 @@ $ whereis php
|
||||||
|
|
||||||
### c. `which`
|
### c. `which`
|
||||||
Locate a program file in the user's path. This command will prints full path of the executable(s). It does this by searching the directories listed in the environment variable PATH.
|
Locate a program file in the user's path. This command will prints full path of the executable(s). It does this by searching the directories listed in the environment variable PATH.
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
which program_name
|
which program_name
|
||||||
```
|
```
|
||||||
|
@ -95,7 +92,6 @@ Clears content on window
|
||||||
|
|
||||||
### a. `ls`
|
### a. `ls`
|
||||||
Lists your files. It has a lot of options like `-l` lists files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. `-a` lists all files, including hidden files. For more information on this command check this [link](https://ss64.com/bash/ls.html)
|
Lists your files. It has a lot of options like `-l` lists files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. `-a` lists all files, including hidden files. For more information on this command check this [link](https://ss64.com/bash/ls.html)
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
ls option
|
ls option
|
||||||
```
|
```
|
||||||
|
@ -114,7 +110,6 @@ drwxr-xr-x 17 adnan staff 578 Mar 27 23:36 .git
|
||||||
|
|
||||||
### b. `touch`
|
### b. `touch`
|
||||||
Creates or updates your file
|
Creates or updates your file
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
touch filename
|
touch filename
|
||||||
```
|
```
|
||||||
|
@ -125,28 +120,24 @@ $ touch trick.md
|
||||||
|
|
||||||
### c. `cat`
|
### c. `cat`
|
||||||
Places standard input into file. Means that it opens the file in terminal for you to edit
|
Places standard input into file. Means that it opens the file in terminal for you to edit
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
cat > filename
|
cat > filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### d. `more`
|
### d. `more`
|
||||||
Shows the first part of a file (move with space and type q to quit)
|
Shows the first part of a file (move with space and type q to quit)
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
more filename
|
more filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### e. `head`
|
### e. `head`
|
||||||
Outputs the first 10 lines of file
|
Outputs the first 10 lines of file
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
head filename
|
head filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### f. `tail`
|
### f. `tail`
|
||||||
Outputs the last 10 lines of file. Use `-f` to output appended data as the file grows
|
Outputs the last 10 lines of file. Use `-f` to output appended data as the file grows
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
tail filename
|
tail filename
|
||||||
```
|
```
|
||||||
|
@ -154,7 +145,6 @@ tail filename
|
||||||
|
|
||||||
### g. `mv`
|
### g. `mv`
|
||||||
Moves a file from one location to other
|
Moves a file from one location to other
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
mv filename1 filename2
|
mv filename1 filename2
|
||||||
```
|
```
|
||||||
|
@ -162,7 +152,6 @@ Where `filename1` is the source path to the file and `filename2` is the destinat
|
||||||
|
|
||||||
### h. `cp`
|
### h. `cp`
|
||||||
Copies a file from one location to other
|
Copies a file from one location to other
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
cp filename1 filename2
|
cp filename1 filename2
|
||||||
```
|
```
|
||||||
|
@ -172,56 +161,48 @@ Where `filename1` is the source path to the file and `filename2` is the destinat
|
||||||
Removes a file. But if you will apply this command on a directory directory, it will gives you an error
|
Removes a file. But if you will apply this command on a directory directory, it will gives you an error
|
||||||
`rm: directory: is a directory`
|
`rm: directory: is a directory`
|
||||||
So in order to remove directory you have to pass `-rf` to remove all the content of the directory recursively
|
So in order to remove directory you have to pass `-rf` to remove all the content of the directory recursively
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
rm filename
|
rm filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### j. `diff`
|
### j. `diff`
|
||||||
Compares files, and shows where they differ
|
Compares files, and shows where they differ
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
diff filename1 filename2
|
diff filename1 filename2
|
||||||
```
|
```
|
||||||
|
|
||||||
### k. `chmod`
|
### k. `chmod`
|
||||||
Lets you change the read, write, and execute permissions on your files
|
Lets you change the read, write, and execute permissions on your files
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
chmod -options filename
|
chmod -options filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### l. `gzip`
|
### l. `gzip`
|
||||||
Compresses files
|
Compresses files
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
gzip filename
|
gzip filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### m. `gunzip`
|
### m. `gunzip`
|
||||||
Un-compresses files compressed by gzip
|
Un-compresses files compressed by gzip
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
gunzip filename
|
gunzip filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### n. `gzcat`
|
### n. `gzcat`
|
||||||
Lets you look at gzipped file without actually having to gunzip it
|
Lets you look at gzipped file without actually having to gunzip it
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
gzcat filename
|
gzcat filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### o. `lpr`
|
### o. `lpr`
|
||||||
Print the file
|
Print the file
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
lpr filename
|
lpr filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### p. `lpq`
|
### p. `lpq`
|
||||||
Check out the printer queue
|
Check out the printer queue
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
lpq
|
lpq
|
||||||
```
|
```
|
||||||
|
@ -235,7 +216,6 @@ active adnanad 59 demo 399360 bytes
|
||||||
|
|
||||||
### q. `lprm`
|
### q. `lprm`
|
||||||
Remove something from the printer queue
|
Remove something from the printer queue
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
lprm jobnumber
|
lprm jobnumber
|
||||||
```
|
```
|
||||||
|
@ -266,7 +246,6 @@ lprm jobnumber
|
||||||
### a. `awk`
|
### a. `awk`
|
||||||
Awk is most usefull command for handling text files. It operates on entire file line by line. By default it uses whitespace to separate the fields. The most common syntax for awk command is
|
Awk is most usefull command for handling text files. It operates on entire file line by line. By default it uses whitespace to separate the fields. The most common syntax for awk command is
|
||||||
|
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse
|
awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse
|
||||||
```
|
```
|
||||||
|
@ -295,7 +274,6 @@ For more detail on how to use `awk`, check following [link](https://www.cybercit
|
||||||
|
|
||||||
### b. `grep`
|
### b. `grep`
|
||||||
Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.
|
Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
grep pattern filename
|
grep pattern filename
|
||||||
```
|
```
|
||||||
|
@ -314,7 +292,6 @@ And `-w` to search for words only. For more detail on `grep`, check following [l
|
||||||
|
|
||||||
### c. `wc`
|
### c. `wc`
|
||||||
Tells you how many lines, words and characters there are in a file
|
Tells you how many lines, words and characters there are in a file
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
wc filename
|
wc filename
|
||||||
```
|
```
|
||||||
|
@ -370,7 +347,6 @@ TODO
|
||||||
|
|
||||||
### a. `mkdir`
|
### a. `mkdir`
|
||||||
Makes a new directory
|
Makes a new directory
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
mkdir dirname
|
mkdir dirname
|
||||||
```
|
```
|
||||||
|
@ -387,7 +363,6 @@ cd dirname
|
||||||
|
|
||||||
### c. `pwd`
|
### c. `pwd`
|
||||||
Tells you in which directory you currently are
|
Tells you in which directory you currently are
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
pwd
|
pwd
|
||||||
```
|
```
|
||||||
|
@ -429,12 +404,10 @@ pwd
|
||||||
|
|
||||||
### a. `ssh`
|
### a. `ssh`
|
||||||
ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.
|
ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine.
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
ssh user@host
|
ssh user@host
|
||||||
```
|
```
|
||||||
This command also accepts an option `-p` that can to used to connect to specific port.
|
This command also accepts an option `-p` that can to used to connect to specific port.
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
ssh -p port user@host
|
ssh -p port user@host
|
||||||
```
|
```
|
||||||
|
@ -446,8 +419,7 @@ Return current logged in username
|
||||||
Allows the current logged user to change his password
|
Allows the current logged user to change his password
|
||||||
|
|
||||||
### d. `quota`
|
### d. `quota`
|
||||||
Shows what your disk quota is
|
Shows what your disk quota is
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
quota -v
|
quota -v
|
||||||
```
|
```
|
||||||
|
@ -466,21 +438,18 @@ Displays who is online
|
||||||
|
|
||||||
### i. `finger`
|
### i. `finger`
|
||||||
Displays information about user
|
Displays information about user
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
finger username
|
finger username
|
||||||
```
|
```
|
||||||
|
|
||||||
### j. `uname`
|
### j. `uname`
|
||||||
Shows kernel information
|
Shows kernel information
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
uname -a
|
uname -a
|
||||||
```
|
```
|
||||||
|
|
||||||
### k. `man`
|
### k. `man`
|
||||||
Shows the manual for specified command
|
Shows the manual for specified command
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
man command
|
man command
|
||||||
```
|
```
|
||||||
|
@ -490,35 +459,30 @@ Shows disk usage
|
||||||
|
|
||||||
### m. `du`
|
### m. `du`
|
||||||
Shows the disk usage of the files and directories in filename (du -s give only a total)
|
Shows the disk usage of the files and directories in filename (du -s give only a total)
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
du filename
|
du filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### n. `last`
|
### n. `last`
|
||||||
Lists your last logins of specified user
|
Lists your last logins of specified user
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
last yourUsername
|
last yourUsername
|
||||||
```
|
```
|
||||||
|
|
||||||
### o. `ps`
|
### o. `ps`
|
||||||
Lists your processes
|
Lists your processes
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
ps -u yourusername
|
ps -u yourusername
|
||||||
```
|
```
|
||||||
|
|
||||||
### p. `kill`
|
### p. `kill`
|
||||||
Kills (ends) the processes with the ID you gave
|
Kills (ends) the processes with the ID you gave
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
kill PID
|
kill PID
|
||||||
```
|
```
|
||||||
|
|
||||||
### q. `killall`
|
### q. `killall`
|
||||||
Kill all processes with the name
|
Kill all processes with the name
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
killall processname
|
killall processname
|
||||||
```
|
```
|
||||||
|
@ -534,28 +498,24 @@ Brings the most recent job in the foreground.
|
||||||
|
|
||||||
### u. `ping`
|
### u. `ping`
|
||||||
Pings host and outputs results
|
Pings host and outputs results
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
ping host
|
ping host
|
||||||
```
|
```
|
||||||
|
|
||||||
### v. `whois`
|
### v. `whois`
|
||||||
Gets whois information for domain
|
Gets whois information for domain
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
whois domain
|
whois domain
|
||||||
```
|
```
|
||||||
|
|
||||||
### w. `dig`
|
### w. `dig`
|
||||||
Gets DNS information for domain
|
Gets DNS information for domain
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
dig domain
|
dig domain
|
||||||
```
|
```
|
||||||
|
|
||||||
### x. `wget`
|
### x. `wget`
|
||||||
Downloads file
|
Downloads file
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
wget file
|
wget file
|
||||||
```
|
```
|
||||||
|
@ -634,7 +594,6 @@ ${#varname} # returns the length of the value of the variable as a character
|
||||||
## 2.3. Functions
|
## 2.3. Functions
|
||||||
As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name.
|
As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name.
|
||||||
|
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
functname() {
|
functname() {
|
||||||
shell commands
|
shell commands
|
||||||
|
@ -661,7 +620,6 @@ When you will run above example the `hello` function will output "world!". The a
|
||||||
|
|
||||||
The conditional statement in bash is similar to other programming languages. Conditions have many form like the most basic form is `if` expression `then` statement where statement is only executed if expression is true.
|
The conditional statement in bash is similar to other programming languages. Conditions have many form like the most basic form is `if` expression `then` statement where statement is only executed if expression is true.
|
||||||
|
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
if [expression]; then
|
if [expression]; then
|
||||||
will execute only if expression is true
|
will execute only if expression is true
|
||||||
|
@ -672,7 +630,6 @@ fi
|
||||||
|
|
||||||
Sometime if conditions becoming confusing so you can write the same condition using the `case statements`.
|
Sometime if conditions becoming confusing so you can write the same condition using the `case statements`.
|
||||||
|
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
case expression in
|
case expression in
|
||||||
pattern1 )
|
pattern1 )
|
||||||
|
@ -772,7 +729,6 @@ cd hotellogs
|
||||||
# 4. Debugging
|
# 4. Debugging
|
||||||
You can easily debug the bash script by passing different options to `bash` command. For example `-n` will not run commands and check for syntax errors only. `-v` echo commands before running them. `-x` echo commands after command-line processing.
|
You can easily debug the bash script by passing different options to `bash` command. For example `-n` will not run commands and check for syntax errors only. `-v` echo commands before running them. `-x` echo commands after command-line processing.
|
||||||
|
|
||||||
Syntax:
|
|
||||||
```bash
|
```bash
|
||||||
bash -n scriptname
|
bash -n scriptname
|
||||||
bash -v scriptname
|
bash -v scriptname
|
||||||
|
|
Loading…
Reference in New Issue