Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61
Rep:
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?
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61
Original Poster
Rep:
Quote:
Originally Posted by jschiwal
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"
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?
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.
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61
Original Poster
Rep:
Quote:
Originally Posted by i92guboj
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
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:
Distribution: Fedora Core 10 X86_64, and Ubuntu 6.06
Posts: 61
Original Poster
Rep:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.