LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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



Reply
 
Search this Thread
Old 07-04-2011, 04:49 PM   #1
PasBern
Member
 
Registered: Dec 2009
Location: Berlin, Germany
Distribution: Sabayon 14, Debian Testing, Bodhi Linux, AntiX M11
Posts: 65

Rep: Reputation: 1
Question 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 05:57 PM. Reason: Spelling error
 
Old 07-04-2011, 05:32 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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

Last edited by Tinkster; 07-04-2011 at 05:38 PM.
 
1 members found this post helpful.
Old 07-04-2011, 05:51 PM   #3
PasBern
Member
 
Registered: Dec 2009
Location: Berlin, Germany
Distribution: Sabayon 14, Debian Testing, Bodhi Linux, AntiX M11
Posts: 65

Original Poster
Rep: Reputation: 1
Thumbs up 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.
 
Old 07-04-2011, 07:04 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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
 
Old 07-04-2011, 07:27 PM   #5
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware64 14.1
Posts: 397

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by Tinkster View Post
aliases don't handle variables/parameters.
Hmm... I think aliases can have parameters. Check this one out:

Code:
alias cdl='cd $1 && ls -al'
 
Old 07-04-2011, 07:40 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 07-05-2011, 04:59 AM   #7
PasBern
Member
 
Registered: Dec 2009
Location: Berlin, Germany
Distribution: Sabayon 14, Debian Testing, Bodhi Linux, AntiX M11
Posts: 65

Original Poster
Rep: Reputation: 1
Question 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?
 
Old 07-05-2011, 10:39 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by Diantre View Post
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.
 
1 members found this post helpful.
Old 07-05-2011, 11:11 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by PasBern View Post
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.
 
1 members found this post helpful.
Old 07-06-2011, 08:19 AM   #10
PasBern
Member
 
Registered: Dec 2009
Location: Berlin, Germany
Distribution: Sabayon 14, Debian Testing, Bodhi Linux, AntiX M11
Posts: 65

Original Poster
Rep: Reputation: 1
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.
 
  


Reply

Tags
bashrc, terminal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ls command and displaying the number of files in your current directory? revsarah Linux - General 5 10-15-2010 07:40 PM
How to Copy files from local directory to a particular directory using alias dynamics Linux - Newbie 7 09-11-2009 05:19 PM
Changing File Permission in a FTP directory to prevent deleting of files shawnbishop Linux - Software 3 01-10-2006 06:41 AM
changing alias of wifi card darreng23 Linux - Wireless Networking 2 10-06-2005 06:56 AM
Changing file permissions within a directory for all files. cajunaggie Linux - Newbie 2 02-06-2005 02:53 AM


All times are GMT -5. The time now is 03:19 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration