[SOLVED] Alias for Changing to Directory and Displaying all Files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
Alias for Changing to Directory and Displaying all Files
I'm trying to setup an alias, that when I change to another directory, any directory, it will also display all its contents like ls -al:
Here is what I tried:
Code:
alias cd*='cd /* && ls -al'
Well, that doesn't work. I guess it's an issue with the use of wildcards like *
Maybe I should define a new, so far unused, name for the alias like cdl for example.
Would be great if someone could help me. I search in several examples for bash aliases but couldn't find the right solution.
Last edited by PasBern; 07-04-2011 at 04:57 PM.
Reason: Spelling error
Yes, simple variable substitutions will be done before execution. This line, for example, depends on the $1 parameter being defined in the shell first. In interactive shells the positional parameters are usually undefined, however.
But variables can be good for predefining options your command will use. I use an array variable for some ls aliases, for example:
Code:
#common options to use in ls commands.
lsopts=( '-v' '--color=always' '--group-directories-first' )
alias lsall="command ls -Al ${lsopts[@]}"
An alias will only tack on command line options to the end of the string, so functions are necessary if you need to insert an argument anywhere but at the end of the alias.
By the way, the cd command can take a couple of command options in addition to the directory, so tinkster's function would better be written this way:
Code:
cdl() {
cd "$@";
ls -al;
}
(PS: don't use "function" and "()" at the same time. )
Quote:
$ help cd
cd: cd [-L|-P] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the HOME shell variable.
The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (. A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
The default is to follow symbolic links, as if `-L' were specified.
Exit Status:
Returns 0 if the directory is changed; non-zero otherwise.
This is not a valid command sequence. A "command" is the first word on a line (or the first word after a chaining character, "|", ";", "&&", or "||"). This is followed by an optional list of options and arguments that will be passed to the command.
Here, you terminate the mount command with both a ";" and a newline, then have an exposed pipe character on the next line with nothing feeding it. This is of course illegal syntax-wise.
Just make the commands in your function the same as you would do them on the command line. Think of them as mini-scripts. The only other important thing you need to know right now is that you can also feed the function arguments, just as with any other script or program, and use them in the code with the special parameters "$1", "$2", ... "$@".
Code:
newmount() {
mount | column -t
}
(Again, you should use "function name" or "name()", but not both. The former is a bash-only built-in, while the latter is posix style.)
Incidentally, aliases are simply command substitutions. If a command name matches a defined alias, the shell will replace it with the contents of the alias definition, then execute the revised string. That's why you can't usually use dynamic options in aliases, as they won't be inserted correctly into the final command.
Thanks for the information David. The link you provided has quite a lot of stuff to learn from, so it might take me a while until I work my way through all of that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.