LinuxQuestions.org
Visit Jeremy's Blog.
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 01-02-2010, 07:52 PM   #1
ThirtySixBelow
Member
 
Registered: Sep 2004
Posts: 48

Rep: Reputation: 15
cd then ls new directory in same command


I would like to use something like a 'cdl' alias that would cd into the directory i choose and then ls the contents automatically. I find myself using ls after i cd into a directory all the time. Something like:

alias='cdl=cd $1;ls'

usage: cdl /local/dir

(not very good at scripting up commands but hopefully that makes sense)
 
Old 01-02-2010, 07:56 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
you have the right idea, but your alias syntax is wrong. Also, rather than using an alias for this idea, you would likely be better off writing a function and putting it into your ~/.bash_profile or /etc/bash_profile.

A simple function like this (which I named "cdl"):

Code:
cdl () {
cd "$1" && ls
}
and would work like you suggested:

shell$ cdl /some/dir

would go to that dir, and ls the contents.

NOTE: this is untested, but should work, or very nearly so.

Sasha
 
Old 01-02-2010, 08:33 PM   #3
ThirtySixBelow
Member
 
Registered: Sep 2004
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GrapefruiTgirl View Post
you have the right idea, but your alias syntax is wrong. Also, rather than using an alias for this idea, you would likely be better off writing a function and putting it into your ~/.bash_profile or /etc/bash_profile.

A simple function like this (which I named "cdl"):

Code:
cdl () {
cd "$1" && ls
}
and would work like you suggested:

shell$ cdl /some/dir

would go to that dir, and ls the contents.

NOTE: this is untested, but should work, or very nearly so.

Sasha
This works like a charm! I didn't know you could directly call scripted functions like this.
 
  


Reply


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
Print Directory command catholiclibertarian Linux - Newbie 8 12-22-2008 06:41 PM
command to copy a directory ceantuco Linux - Newbie 6 09-17-2008 05:09 AM
How can I run a command in some directory? allan_bg Linux - Newbie 8 02-05-2008 12:37 PM
Directory Summary Command canuck_barlow Linux - Newbie 8 03-01-2005 02:16 PM
cp directory command mark001 Linux - Newbie 5 09-19-2004 02:00 AM

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

All times are GMT -5. The time now is 09:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration