QuickTip: command concatenation

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

Advertisements

6 Responses to QuickTip: command concatenation

  1. cd /home/usr/Desktop;ls|grep tips

  2. SublimePorte says:

    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.

  3. georg says:

    ls /home/usr/Desktop | grep tips

  4. p1ip1i says:

    Excellent stuff- thank you, both!

    Neil

  5. CheShA says:

    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)

  6. p1ip1i says:

    Thanks for the feedback 🙂

    Neil

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: