Terminal Tips: Useful command-line tricks for Linux Users
Most linux users are aware of the basic Linux terminal commands such as cd, ls, rm, rmdir, mkdir etc. Now let us take a look at some other commands which will help you make the best of the linux shell/terminal.
1. How to run a command in the background
Majority of us will be using the mouse to start a program. To run a command in the background use '&' after typing the command. Consider you want to open 'Gimp', For opening the 'Gimp' window you just have to type command 'Gimp' in the terminal but the 'Gimp' won’t be running the background, to avoid as mentioned above use '&' sign after the command. That is 'Gimp &', Now this will run the command in the background. To understand this have a try. Open a terminal and run a command to open something which you like and try both the mentioned things ie use only the command and also use the command followed by the '&' sign to know the difference.
2. Conditional execution
We have discussed about using a single '&' sign after the command now we will see what happens when we use a double '&&' sign. The '&&' sign is used to execute two commands in order. When we want to execute a command only when the other command is successfully executed. When we use two commands ie command1 and command2, and we want to execute command2 only if command1 has been executed successfully. For this we use 'command1 && command 2'. Consider an example if you want to open a workrave window if the screen sleeps for 10 seconds. Then you can set this by keeping 'sleep' as the first window and 'workrave' as the second window.
sleep 10 && workrave
3. Output Redirection
The term itself 'output redirection' gives its meaning is to redirect the output. The output of one command can be redirected to another file using this output redirection. This is denoted by using ' > ' sign. Consider an example, to have a file with all the directory name which are located inside the home directory of the user. We know that to list the directory contents we use ' ls ' command. And to copy the output to a file say 'file 1' we use output redirection. That is
ls > file 1
Now to file named 'file 1' will have all the directory contents of the home folder, that is the output of the ls command will be written to the file named 'file 1'
4. Pipe and grep commands
If you are executing the command and wants to give the commands output as the input of another command which is going to be executed, pipe will help you. Pipe is denoted by ' | '. Consider you want to list the directory contents and want to search for a directory named "x" grep comes into place to help you. Now we can see that both the commands, that is pipe and grep together is very useful, for example
Consider you want to search for a directory named "x" from hundreds of sub-directories from directory named "y", then just simply type the command to search for the directory
Command 1: "cd y" to open then the directory y Command 2: ls | grep x
In the second command "ls" will list the sub-directories in "y" and "pipe" will send the output of ls as input to grep, and grep x will search for the directory named "x".