Find the answer to your Linux question:
Results 1 to 4 of 4
hi all, may i know how to combine two command in one line For example, i would like to change directory and then list it...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2005
    Location
    Singapore
    Posts
    234

    combine commands


    hi all,
    may i know how to combine two command in one line
    For example, i would like to change directory and then list it
    Proper Planing is a way of success

  2. #2
    Linux User
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    326
    You can chain copmmands together using &&

    cd /var/log && ls -al

    this switches to the /var/log directory if the directory change was sucessfuly it will then list the contents of hte directory.



    You can also list the contents of a directory without leaving your current location..

    ls -al /var/log
    Would list the contents of /var/log no matter your current location in the directory tree.




    You can also use | to send the output of one command to the input of another..

    mount | column -t
    Show mount points in a column format

    ls -al | grep *.jpg
    List the contents of a directory buit only show files ending in .jpg

    ps aux | grep X11
    List processes but only show processes containing X11



    > and < can also be used to direct information between commands..

    ls -al /var/log > list.txt
    Will list the contents of the /var/log directory and save it in the file list.txt

    hope this helps
    far...out

  3. #3
    Linux Newbie jpalfree's Avatar
    Join Date
    Jul 2005
    Location
    Montreal, CA
    Posts
    198
    Quote Originally Posted by farslayer
    You can chain copmmands together using &&

    cd /var/log && ls -al
    this swithces to the /var/log directory if the directory change was sucessfuly it will then list the contents o fhte directory.
    just to add to that, you can likewise chain commands together with the || operator (OR operator, not to be confused with a pipe | )
    Code:
    $ cd /mnt || cd /var
    this would attempt to change to the /mnt directory. If it could not it would then switch to the /var directory. If the cd /mnt command was successfull then it would stop. Similarly,
    Code:
    $ cd /blah || cd /home
    it's probably safe to say there is no /blah directory on your computer so typing this command would attempt to change to a non-existant directory, fail, and consequently change to /home
    have fun!
    Avatar from xkcd.com, a hilarious computer related webcomic.

  4. #4
    Linux Newbie
    Join Date
    Dec 2005
    Location
    Singapore
    Posts
    234
    thks
    Proper Planing is a way of success

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •