LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-27-2009, 11:04 AM   #1
Cassanova
Member
 
Registered: Feb 2009
Location: An Evil city in Indiana
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61

Rep: Reputation: 15
Combining Terminal Commands


Is there a way to combine Terminal commands (via alias or otherwise)? for example, could I combine the cd and ls commands into one so that I could: A: Switch directories THEN B: list it's contents with one convenient, time saving string?
 
Old 03-27-2009, 11:07 AM   #2
emclinux
Member
 
Registered: Sep 2008
Posts: 49

Rep: Reputation: 16
You can use the && operator.

Example:

cd / && ls

or

cd /home/ && ls
 
Old 03-27-2009, 11:11 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could add a function in your ~/.profile startup script:

cdls () { cd "$1" && ls; }

A semicolon could separate the commands, but if the directory doesn't exist, you would fail twice to cd and be listing the wrong directory.

If you used
cdls () { cd "$1" && ls "$1"; }

There would be a problem if you used a relative address.

E.G.
cdls Documents would cd to Documents and try to list Documents/Documents.

Last edited by jschiwal; 03-27-2009 at 11:14 AM.
 
Old 03-27-2009, 11:14 AM   #4
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: Debian 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,766

Rep: Reputation: Disabled
You can use a new name for any complicated boolean combination of commands

http://www.ss64.com/bash/alias.html
 
Old 03-28-2009, 02:55 PM   #5
Cassanova
Member
 
Registered: Feb 2009
Location: An Evil city in Indiana
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
You could add a function in your ~/.profile startup script:

cdls () { cd "$1" && ls; }

A semicolon could separate the commands, but if the directory doesn't exist, you would fail twice to cd and be listing the wrong directory.

If you used
cdls () { cd "$1" && ls "$1"; }

There would be a problem if you used a relative address.

E.G.
cdls Documents would cd to Documents and try to list Documents/Documents.

The goal is ultimately to alias it; but I don't know enough about scripting to give a 'wild card' or variable to essentially tell the terminal "cd [get the directory the user wants and put it here] now ls it"
 
Old 03-28-2009, 03:19 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Cassanova View Post
The goal is ultimately to alias it; but I don't know enough about scripting to give a 'wild card' or variable to essentially tell the terminal "cd [get the directory the user wants and put it here] now ls it"
I am not sure I understand you, jschiwal already told you exactly how to do that, what in his post is the part that you don't understand?
 
Old 03-29-2009, 11:16 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by Cassanova View Post
The goal is ultimately to alias it; but I don't know enough about scripting to give a 'wild card' or variable to essentially tell the terminal "cd [get the directory the user wants and put it here] now ls it"
Aliases don't take arguments. The directory argument needs to occur before the ls command.

I do need to change the function for relative arguments, which makes the function equivalent to the second post.
cdls()
{
cd "$1" && ls
}

Using functions is a very common method to simplify scripts. If everything in the function is a builtin command, you will often see significant speed increases over using an external commands.

Last edited by jschiwal; 03-29-2009 at 11:19 AM.
 
Old 03-29-2009, 06:07 PM   #8
Cassanova
Member
 
Registered: Feb 2009
Location: An Evil city in Indiana
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by i92guboj View Post
I am not sure I understand you, jschiwal already told you exactly how to do that, what in his post is the part that you don't understand?
lol! To be honest, all of it. But then again I'm not just a newb; I'm a crazy annoying newb, who loves to have things broken down and explained to just HOW each part works
 
Old 03-29-2009, 07:12 PM   #9
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Well, you have to create a function to do what you want, and you have to put it on the initialization files for your shell, so the functions gets loaded everytime you open a shell, and you can use it.

So, to reword what jschiwal said and assuming that your shell is bash, you will need to edit ~/.bashrc and/or ~/.bash_profile, and put this function inside that file(s). You can use any text editor to do so, and note that the file names start with a dot.

Code:
function cdls () {
  if [ -d "$1" ]; then
    cd "$1"
    ls
  else
    echo "Directory not found"
  fi
}
After this, you close the file(s), and open a new bash session. If you are already in bash you will need to close it and open a new terminal or something, or alternatively, just run this command which will close the current shell and spawn a new on on the current tty or terminal emulator.

Code:
exec bash
The new function should load, and you can use it like this:

Code:
cdls $HOME
cdls /
cdls /mnt/pendrive
Or whatever you want, which I think is what you wanted.

Note that my function is slightly more complicated because it checks for the existence of the directory before cd'ing into it, which means you don't have to concatenate ls with &&, and that you can use a custom error message as well. I also suggested using ~/.bashrc and ~/.bash_profile instead of ~/.profile (which jschiwal suggested), because of the simple reason that if either ~/.bash_login or ~/.bash_profile exists, then ~/.profile won't be read, which can cause unnecessary pain and time loss. ~/.bash_profile will take precedence.

~/.bashrc will be used for non login interactive shells, for example when you open an xterm or any other terminal emulator.

~/.bash_profile will be used instead when you open a login shell, for example, when you login in text mode instead of using a graphical login manager.

Feel free to ask whatever you don't understand. Though if I were you, I would start by reading this:

http://tldp.org/LDP/abs/html/
 
Old 03-30-2009, 11:25 AM   #10
Cassanova
Member
 
Registered: Feb 2009
Location: An Evil city in Indiana
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61

Original Poster
Rep: Reputation: 15
Yeah, when I make aliases, I use the .bashrc file (because tbh I didn't know the others existed. lol). I've been meaning to study up on bash scripting, I checked out a book @ the library about it. guess it's time to open it up. Thanks for dealing with my annoyances; and thanks for all the help.

Last edited by Cassanova; 03-30-2009 at 11:27 AM.
 
Old 03-30-2009, 11:56 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by Cassanova View Post
Yeah, when I make aliases, I use the .bashrc file (because tbh I didn't know the others existed. lol). I've been meaning to study up on bash scripting, I checked out a book @ the library about it. guess it's time to open it up. Thanks for dealing with my annoyances; and thanks for all the help.
You are welcome.

If you want to know or just feel curious, you can read the whole thing about the different bash rc files in the bash man page, search for a section called "INVOCATION", uppercase.
 
Old 04-03-2009, 12:38 AM   #12
Recursion
LQ Newbie
 
Registered: Apr 2009
Posts: 27

Rep: Reputation: 17
couldn't you just do this


cd directory; ls
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
making a script that opens terminal and enter commands into that terminal Cinematography Linux - General 8 12-16-2008 10:34 AM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - Software 2 07-02-2008 06:26 PM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - General 2 07-01-2008 06:53 AM
Combining two commands into one spes_hominis Linux - Software 5 05-16-2007 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration