LinuxQuestions.org
Review your favorite Linux distribution.
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, 03:49 PM   #1
PasBern
Member
 
Registered: Dec 2009
Location: Near Frankfurt, Germany
Distribution: OpenSUSE Leap 15.5, MacOS
Posts: 122

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

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 04:38 PM.
 
1 members found this post helpful.
Old 07-04-2011, 04:51 PM   #3
PasBern
Member
 
Registered: Dec 2009
Location: Near Frankfurt, Germany
Distribution: OpenSUSE Leap 15.5, MacOS
Posts: 122

Original Poster
Rep: Reputation: 2
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, 06:04 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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, 06:27 PM   #5
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
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, 06:40 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 03:59 AM   #7
PasBern
Member
 
Registered: Dec 2009
Location: Near Frankfurt, Germany
Distribution: OpenSUSE Leap 15.5, MacOS
Posts: 122

Original Poster
Rep: Reputation: 2
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, 09:39 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 10:11 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 07:19 AM   #10
PasBern
Member
 
Registered: Dec 2009
Location: Near Frankfurt, Germany
Distribution: OpenSUSE Leap 15.5, MacOS
Posts: 122

Original Poster
Rep: Reputation: 2
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 06:40 PM
How to Copy files from local directory to a particular directory using alias dynamics Linux - Newbie 7 09-11-2009 04:19 PM
Changing File Permission in a FTP directory to prevent deleting of files shawnbishop Linux - Software 3 01-10-2006 05:41 AM
changing alias of wifi card darreng23 Linux - Wireless Networking 2 10-06-2005 05:56 AM
Changing file permissions within a directory for all files. cajunaggie Linux - Newbie 2 02-06-2005 01:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:20 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