LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 12-19-2006, 04:21 PM   #1
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 459

Rep: Reputation: 30
alias


is it possible to setup an alias for cd to automatically give me an ls?

for example: alias -x cdls="cd $whatever:ls -la"
 
Old 12-19-2006, 04:39 PM   #2
Dudydoo
Member
 
Registered: Sep 2003
Location: UK
Distribution: I use 'em all ;-)
Posts: 275

Rep: Reputation: 38
alias cdls='cd $whatever; ls -la'
 
Old 12-19-2006, 07:27 PM   #3
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 459

Original Poster
Rep: Reputation: 30
that will work for any directory? so now if I put cdls /home its the same as
$cd /home
$ls -la

correcT?
 
Old 12-20-2006, 02:30 PM   #4
Dudydoo
Member
 
Registered: Sep 2003
Location: UK
Distribution: I use 'em all ;-)
Posts: 275

Rep: Reputation: 38
Yes.

To be more precise, if you set your alias like this:

Code:
alias cdls='cd $1; ls -la $1'
the $1 means the first argument given to `cdls` (which is the path)

so if you type:

Code:
cdls /tmp
the shell interprets it as:

Code:
cd /tmp; ls -la /tmp
also to make this permanent add the alias line to your ~/.bashrc file.

Last edited by Dudydoo; 12-20-2006 at 02:31 PM.
 
Old 12-20-2006, 04:30 PM   #5
Dudydoo
Member
 
Registered: Sep 2003
Location: UK
Distribution: I use 'em all ;-)
Posts: 275

Rep: Reputation: 38
I have since found that the above doesn't actually work as intended, apparently aliases don't work with arguments inside the command string; therefore you must use shell functions instead.

Type this in your ~/.bashrc instead:

Code:
cdls() {

    cd $1;
    ls -la $1

}
 
Old 12-21-2006, 05:24 PM   #6
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 459

Original Poster
Rep: Reputation: 30
great, thank you
 
  


Reply



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
IP alias? Machine name alias? JohnLocke Linux - Newbie 4 01-16-2008 12:21 PM
Alias question (2 commands for one alias) gflores Linux - Newbie 3 01-21-2006 12:40 AM
alias zetsui Linux - Software 1 07-31-2003 03:42 AM
alias booyah Slackware 3 05-06-2003 03:17 PM
IP Alias slacknut Slackware 9 03-31-2003 09:21 PM

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

All times are GMT -5. The time now is 02:41 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
Open Source Consulting | Domain Registration