---------------------------------------------------------------------
`` Redirection in Linux
` Input / Output
- Standard Input (stdin) device is the keyboard.
- Standard Output (stdout) device is the Screen.
` Output Redirection
- The '
>' symbol is used for output (stdout) redirection.
- Output Redirection >
- ">" is the output redirection operator. ">>" appends output to an existing file.
` Input redirection
- The '
<' symbol is used for input (stdin) redirection.
- < Input Redirection
- ex) mail -s "News Today" abc@mail.com < NewsFlash
` Error Redirection
- Standard Input STDIN : FD0
- Standard Output STDOUT : FD1
- Standard Error STDERR : FD2
` Why Error Redirection?
- Searching for files typically gets permission denied errors.
- Error messages clutter up program output while executing shell scripts.
- Solution = Redirect error messages
` ls Documents ABC > dirlist 2>&1
- "
>&" which writes the output from one file to the input of another file.
- Error output is redirected to standard output which in turn is being re-directed to file dirlist.
- ">&" re-directs output of one file to another.
---------------------------------------------------------------------
`` Pipes
- The symbol '|' denotes a pipe.
- Use Pipes to run two commands consecutively.
- Helps in creating powerful commands
ex) cat aaa.txt |
less
` '
pg' and '
more' commands
- cat Filename | pg
- cat Filename | more
``
grep
- Scan a document
- Present the result in a format you want
- grep
ex) cat aaa.txt | grep han
` The '
grep' command
-
-v : Shows all the lines that do not match the searched string.
-
-c : Displays only the count of matching lines.
-
-n : Shows the matching line and its number.
-
-i : Match both (upper and lower) case, all lines that match the character.
-
-l : Shows just the name of the file with the string.
`` The '
sort' command
- Sorting the contents of a file
- sort Filename
` The 'sort' Option
-
-r : Reverses sorting
-
-n : Sorts numerically
-
-f : Case insensitive sorting
` Pipes '
|' help combine 2 or more commands.
` A filter in a pipe is an output of one command which serves as input to the next.
` The
grep command can be used to find strings and values in a text document.
` '
sort' command sorts out the content of a file alphabetically.
`
less,
pg and
more commands are used for dividing a long file into readable bits.
---------------------------------------------------------------------
`` Regular Expressions
`
tr,
sed,
vi,
grep
-
. : replaces any character
-
^ : matches start of string
-
$ : matches end of string
-
* : matches up zero or more times the preceding character
-
\ : Represent special characters
-
() : Groups regular expressions
-
? : Matches up exactly one character
` Interval Regular Expressions
- Number of occurrences of a character in a string.
-
{n} : Matches the preceding character appearing 'n' times exactly
-
{n,m} : Matches the preceding character appearing 'n' times but not more than m
-
{n,} : Matches the preceding character only when it appears 'n' times or more
` Extended regular Expressions
-
\+ : Matches one or more occurrence of the previous character
-
\? : Matches zero or one occurrence of the previous character
` Brace Expansion
- Sequence : {0..10}, {a..z}
- Comma Separated List : {aa, bb, cc, dd}
` Summary
- Regular expressions are a set of characters used to check patterns in strings.
- They are also called '
regexp' and '
regex'.
- It is important to learn regular expressions for writing scripts.
` Some basic regular expressions are:
-
. : replaces any character
-
^ : matches start of string
-
$ : matches end of string
` Some extended regular expressions are :
-
\+ : Matches one or more occurrence of the previous character
-
\? : Matches zero or one occurrence of the previous character
` Some interval regular expressions are :
-
{n} : Matches the preceding character appearing 'n' times exactly
-
{n,m} : Matches the preceding character appearing 'n' times but not more than m
-
{n, } : Matches the preceding character only when it appears 'n' times or more
- The brace expansion is used to generate strings. It helps in creating multiple strings out of one.
---------------------------------------------------------------------
`` Managing Processes
` Process?
- An instance of a program is called a Process.
- Any command that you give to your Linux machine starts a new process.
` Types of Processes
- Foreground Processes
- Background Processes
` Background Process
1. Start the program
2. Press Ctrl + Z
3. Type bg to send process to background
` FG Command
- fg (jobname)
` Top
- Displays all the running processes.
- top
` process Status
- D : Uninterruptible sleep
- R : Running
- S : Sleeping
- T : Traced or Stopped
- Z : Zombie
` PS
- Process Status
- ps ux
- ps PID
` kill
- Terminates running processes
- kill PID
- niceness : -20 to 19 : Lower the niceness index, higher would be the priority
` NICE
- Default value of all the processes is 0
- nice -n 'Nice value' process name
- renice 'nice value' -p 'PID'
` DF
- Reports the free disk space
- df
` FREE
- Shows free and used memory (RAM) on the Linux system
- free -m
- free -g
`` Summary
- Any running program or a command given to a Linux system is called a process.
- A process could run in foreground or background.
- The priority index of a process is called Nice in Linux. Its default value is 0 and it can vary between 20 to -19.
- The lower the Niceness index the higher would be priority given to that task.
-
bg : To send a process to background
-
fg : To run a stopped process in foreground
-
top : Details on all Active Processes
-
ps : Give the status of processes running for a user
-
ps PID : Gives the status of a particular process
-
pidof : Gives the Process ID(PID) of a process
-
kill PID : kills a process
-
nice : Starts a process with a given priority
-
renice : Changes priority of an already running process
-
df : Gives free hard disk space on your system
-
free : Tells the free RAM on your system
---------------------------------------------------------------------