If you are using terminal / bash, and you want to perform multiple operations, you can consider concatenating them. For example, if you wanted to listthe contents of a directory (Desktop, in the example), you would want to do the following:
cd /home/user/Desktop
This will set your shell location to Desktop. Then, run the application “ls” to list the contents of the folder:
ls
Your output will be whatever folders you have in your Desktop directory (in my case, the reference to my Home directory, and my text file “tips.txt”:
Home.desktop tips
However, you can concatenate the commands by using the | symbol (which is obtained by pressing Shift + Function + Z (it must be in that order)) . As such, the two commands above become:
cd /home/user/Desktop | ls
The output is the same.
You can perform multiple concatenations as well – take, for example, the search function “grep”. If you have a lot of files in a directory, simply listing them may not be particularly useful, as you then have to look for the particular file name – and a computer is much better at matching text strings than you are
As such, why not search the output of the listing?
cd /home/user/Desktop | ls | grep tips
(note that you do not have to use the complete name- you could grep “ti” or even “ip”. It simply looks for a matching of the order of the letters, anywhere in the strings)
The output will be all the matches- if there is no output, you will see nothing.
Edit: Claude has been very quick off the mark which an excellent tip, pointing out that you could also concatenate as follows:
cd /home/user/Desktop;ls / grep tips
Thanks, Claude!
Neil
November 25, 2007 at 7:58 pm |
cd /home/usr/Desktop;ls|grep tips
November 25, 2007 at 8:09 pm |
Command piping is still an excellent little trick to know, but just wanna point out, the commands you piped (concatencatated, that word is soooo long) in your example do not really need to be piped. You coul;d just supply the directory name as an argment to “ls”.
so typing: ls /home/user/Desktop
will produce the same result.
November 25, 2007 at 8:58 pm |
ls /home/usr/Desktop | grep tips
November 25, 2007 at 9:03 pm |
Excellent stuff- thank you, both!
Neil
December 10, 2007 at 1:10 am |
What you’re doing there is called piping, not concatenating. You’re sending the output of the first command into the second command. Concatenation would look more like this:
cd /home/user/desktop && ls
which is subtly different from
cd /home/user/desktop ; ls
in that it would only execute the second command if the first one had completed successfully (returned exit code 0)
Similarly, the following construct can be used:
cd /home/user/desktop || ls
which would only run the ls command if the first command had failed (returned non-zero exit code)
December 10, 2007 at 6:38 am |
Thanks for the feedback
Neil