Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Is there a way to chain commands?

  1. #1
    Join Date
    May 2006
    Beans
    223

    Is there a way to chain commands?

    For example, most of the time when I change directories on the command like with cd I want to immediatly follow it with ls so I can see what I'm working with. Is there a way to make it so that executing the command cl mydir/, for example, would do execute cd mydir and then ls?

  2. #2
    Join Date
    Jul 2005
    Location
    Ohio, USA
    Beans
    665
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Is there a way to chain commands?

    Put a ; (semicolon) between commands.

    cd mydir ; ls

  3. #3
    Join Date
    Jul 2007
    Location
    Atlanta, GA
    Beans
    668
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Is there a way to chain commands?

    You can use the && notation to chain commands if you like such as
    Code:
    cd mydir && ls && chown root.root filename

  4. #4
    Join Date
    Jan 2009
    Location
    Stockholm, Sweden
    Beans
    593
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Is there a way to chain commands?

    You can also write your own script:

    Code:
    cd /usr/bin
    sudo emacs cl
    Add something like this to the file:

    Code:
    #!/bin/bash
    
    cd $1  # $1 refers to the first argument given to your script.
    ls
    
    exit 0
    Then you give everyone permission to run it:

    Code:
    sudo chmod 777 cl
    Since it is stored in the /usr/bin, it will automatically be activated once you type it in the terminal. Hence, you can use it just the way you described, i.e:

    Code:
    cl name-of-dir

    EDIT: This tip is essentially useless. Running cd inside the script won't have any effect outside the script (in the terminal). But there are a lot of other fun stuff to do with scripting (just google "bash scripting tutorial" if you wanna know more.)
    Last edited by tom4everitt; October 16th, 2009 at 07:55 PM.

  5. #5
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: Is there a way to chain commands?

    From your question - why not
    Code:
    ls /directory/you/want/to/be/in/
    eg
    Code:
    ls /media/disk/music/

  6. #6
    Join Date
    May 2006
    Beans
    223

    Re: Is there a way to chain commands?

    Quote Originally Posted by nothingspecial View Post
    From your question - why not
    Code:
    ls /directory/you/want/to/be/in/
    eg
    Code:
    ls /media/disk/music/
    I often want to change my working directory and immediately see what's in there, and I like to save myself those two or three keystroke

  7. #7
    Join Date
    Oct 2007
    Location
    Chennai, India
    Beans
    3,804
    Distro
    Ubuntu Development Release

    Re: Is there a way to chain commands?

    Quote Originally Posted by Amablue View Post
    I often want to change my working directory and immediately see what's in there, and I like to save myself those two or three keystroke
    Spoken like a true power user.
    Cheers,PRShah
    Make your own: Ubuntu, Kubuntu, Xubuntu, Mythbuntu All-in-One Live DVD
    "I never make mistakes; I thought I did, once.. but I was wrong."

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Is there a way to chain commands?

    you can use a bash function:
    Code:
    function xs() { cd $@ && ls; }
    add the line to the ~/.bashrc file.

    Code:
    xs path/to/dir
    EDIT2:

    you can define an alias:
    Code:
    alias cd="xs"
    Last edited by sisco311; October 16th, 2009 at 08:37 PM.

  9. #9
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: Is there a way to chain commands?

    There are several ways to string commands.

    command1; command2

    runs command 1 then command2

    And:
    command1 && command2

    command 2 only runs if command 1 is successful

    Or
    command1 || command2

    command 2 runs if command 1 fails


    command1 | command2
    pipes the output of command 1 to command2

    etc, etc ...
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  10. #10
    Join Date
    May 2006
    Beans
    223

    Talking Re: Is there a way to chain commands?

    Quote Originally Posted by sisco311 View Post
    you can use a bash function:
    Code:
    function xs() { cd $@ && ls; }
    add the line to the ~/.bashrc file.

    Code:
    xs path/to/dir
    EDIT2:

    you can define an alias:
    Code:
    alias cd="xs"
    Thanks, this is what I was looking for

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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