What happens behind the scenes when you execute a command in Linux?
Linux uses 3 standards streams for doing things behind the scenes.
- Stdin (Standard Input)
- Stdout (Standard Output)
- Stderr (Standard Error) Output can be done through a Terminal, File or Pipe which redirects output to another file.
Getting error in a file
Here lg is a wrong command. To get the error in a file use 2> file name. In this 2 is the code for stdout.
When we know we’ll getting an error and we don’t want it on the screen then use 2> /dev/null.
Difference between > and >>
Replaces contents in a file. It is used to override.
Used to append contents in a file. Nothing will be lost here.
In the above image, using > replaces the contents. whereas when we use >> it just adds the contents to the file.
grep
grep stands for Global Regular Expression Print.
Its used to search files by characters. Its one of the most commonly used commands.
Here, we’re searching for the word ‘stalin’ in the file users.txt
-n is used for getting the line no. and -c is used to get the count. ./* will get us all the matches in the current directory.
less
It opens output in a separate window so that the terminal doesn’t get messed up.
Its used when the output will be long.
Pipe
It takes output of 1 command(left side) and gives it as a input to another command on right side.
Eg. ls -la /etc | less
It gives us list of contents(Files, directories) in /etc folder in a long list with hidden files is given as input to less command.
less opens it in a separate window which allows us to manage this long list efficiently.
Environment Variables
It contains useful information that the shell and other processes use.
Values of these variables are Dynamic, which means it can change.
wc
Gives no. of lines, words, bytes in a file.
Here is 7 no. of lines, 25 is no. of words and 118 is no. of bytes of the file, greetings is the file name.
head
It gives us first 10 lines of a file by default. If you want more lines then use head -n(no. of lines) file name.
tail
It gives us last 10 lines of a file by default. If you want more lines then use head -n(no. of lines) file name.
sort
sorts file in a particular order.
tr
Translates / Changes contents of a file.
uniq
Used to check for duplicate values.
Thanks for reading, love you. Wish you the best for your future endeavors.
Trust the Process.