LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Alias for Changing to Directory and Displaying all Files (http://www.linuxquestions.org/questions/linux-newbie-8/alias-for-changing-to-directory-and-displaying-all-files-889890/)

PasBern 07-04-2011 03:49 PM

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.

Tinkster 07-04-2011 04:32 PM

You'll need to make that a function; aliases don't handle
variables/parameters. And the asterisk wasn't a good choice,
either ;}

Code:

function cdl(){
  cd $1;
  ls -al;
}

Slap that into your bashrc.


Cheers,
Tink

PasBern 07-04-2011 04:51 PM

Thanks
 
Thanks a lot tinkster

It works great, exactly how I wanted it to. I wasn't aware that I would have to define a function for that. :cool:

chrism01 07-04-2011 06:04 PM

Actually, if you skip the wildcard, an alias will work for a specific dir
Code:

alias cdl='cd /dir && ls -al'
As Tinkster said, to be able to specify any dir, you need a fn

Diantre 07-04-2011 06:27 PM

Quote:

Originally Posted by Tinkster (Post 4404837)
aliases don't handle variables/parameters.

Hmm... I think aliases can have parameters. Check this one out:

Code:

alias cdl='cd $1 && ls -al'

Tinkster 07-04-2011 06:40 PM

Yep, sorry, my bad: aliases (in bash 3.x) don't have features
to do more fancy stuff (e.g., replacements) with parameters.

A simple use like above will work; sorry about that.


Cheers,
Tink

PasBern 07-05-2011 03:59 AM

What about pipes?
 
Would I also have to use rather a function in ~/.bashrc than setting an alias when using a command string together with a pipe, say
Code:

mount | column -t
for exampe?

If so would it look like:
Code:

function newmount(){
  mount;
  | column -t ;
}

??????????????????????????????????????


As the pipe is not a command by itself (Or do I understand that wrong?), it should different as a function, shouldn't it?

David the H. 07-05-2011 09:39 AM

Quote:

Originally Posted by Diantre (Post 4404919)
Code:

alias cdl='cd $1 && ls -al'

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.


David the H. 07-05-2011 10:11 AM

Quote:

Originally Posted by PasBern (Post 4405400)
If so would it look like:
Code:

function newmount(){
  mount;
  | column -t ;
}


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.

Read here for more on functions and aliases, among others:
http://mywiki.wooledge.org/BashGuide/CompoundCommands

Then go to the beginning of the guide and read the whole thing. :twocents:

PasBern 07-06-2011 07:19 AM

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.


All times are GMT -5. The time now is 12:43 AM.