LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-21-2009, 10:05 AM   #1
seycyrus
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
cd'ing into last created directory


Greetings,

I would like to write an alias that would allow me to "cd" into the most recently created directory in the directory i am currently working in.

In other words, suppose I have a program that generates a vast amount of datafiles in their own directories. I would like to able to type "cdlast" (for example) and move into the data directory that was the last one to be spit out.

Any help would be appreciated, and thank you for your time.
 
Old 08-21-2009, 10:09 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
How about:

cdlast:
Code:
#!/bin/bash

latestdir=$(ls -ltr | egrep "^d" | tail -1 | awk '{print $NF}')

cd ${latestdir}
Put 'cdlast' somewhere in your path and make sure you've done a 'chmod +x cdlast'.
 
Old 08-21-2009, 10:25 AM   #3
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
Sorry made a mistake. May post it back if I get it working.

Edit: Well, I guess my original idea wasn't really feasible. But at the very least I can point out that the above command will work just as well as a shell function as a separate script. Just add it to your bashrc:
Code:
function cdlast(){ cd "$("ls" -ltr|egrep "^d"|tail -1|"awk" '{print $NF}')"; }
Note that you may have to escape the individual commands (quote them, like I did with "ls") if they're aliased to something else.

Last edited by David the H.; 08-21-2009 at 11:02 AM.
 
Old 08-21-2009, 01:03 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
You are gotta love simplicity

Code:
ls -td */ | head -n1
However, I'd also add some basic error checking:

Code:
function cdlast() {
  last="$(\ls -dt */ | head -n1)"
  if [ -d "$last" ]; then
    cd "$last"
  else
    echo "There's no last directory to change into."
  fi
}
I also prefer \ to escape aliases, my brain can read sentences better that way, without nested quotation marks which are difficult to follow sometimes.
 
Old 08-21-2009, 08:29 PM   #5
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 i92guboj View Post
Code:
ls -td */ | head -n1
Aargh... That's what I was trying to get working. But I couldn't get it to list only the directories. Figures it'd be something as simple as '*/.

And yes, I usually just backslash also when bypassing aliases on the command-line. I just felt that fully quoting them was a slightly more intuitive way of thinking when using the commands in aliases or functions.
 
Old 08-21-2009, 08:44 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by David the H. View Post
Aargh... That's what I was trying to get working. But I couldn't get it to list only the directories. Figures it'd be something as simple as '*/.
Well, sometimes simple things are the hardest to guess, it happens to everyone from time to time


Quote:
And yes, I usually just backslash also when bypassing aliases on the command-line. I just felt that fully quoting them was a slightly more intuitive way of thinking when using the commands in aliases or functions.
Well, I guess that it depends on everyone's brain. I am sure that there are people that find nested quotes easier to read. Each one can pick whatever s/he can assimilate better
 
Old 08-24-2009, 01:38 PM   #7
seycyrus
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you for your replies. I am still having a bit of difficulty and would appreciate a followup, if possible.

I am using the c shell, and have been exposed to one of its shortcomings, namely that it does not support the use of functions. I am trying to use the commands that you guys showed me in the above function, but am running into problems.

There seems to be a disparity between the generated output when I type the commands directly into the command line compared to when I try to input them in my .cshrc.pers file.

In the command line, i type " ls -td */ | head -n1 ", and I get the latest director as output.

But when I try to set an alias for it in my .cshrc file I get the response "too many argument". My code is,

setenv LATEST \ls -td */ | head -n1
alias cdlast cd $LATEST

In fact if I just try to do the setenv from the command line, I get the same error, i.e. "too many arguments".

Please help me out, (in addition to just saying "too bad, so sad! Use bash!"
 
Old 08-24-2009, 02:21 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You don't really need the environment variable, since you can use command substitution in aliases:
Code:
alias cdlast 'cd `\ls -td */ | head -n1`'
Edit: just to clarify: the too many arguments error is due to the fact that you did not make use of quotes to embed the string that contains blank spaces. In this way every field is seen as an argument, whereas setenv accepts only two of them (the name of the variable and its value). This works, instead:
Code:
setenv LATEST '\ls -td */ \174 head -n1'
Anyway the command cd $LATEST cannot work, due to the lack of command substitution.

Last edited by colucix; 08-24-2009 at 02:34 PM.
 
  


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
useradd - No home directory is created ]SK[ Linux - Newbie 16 07-14-2008 01:35 PM
Can't access newly created directory DaftDave Linux - Newbie 4 06-27-2008 04:00 PM
When was a directory created? thekillerbean Linux - General 4 07-12-2007 04:18 AM
How do I find out when a directory was created? gumpish Linux - Newbie 1 09-29-2004 08:12 PM
WEB-INF directory is not created...? Hai Thanh To Linux - Software 0 06-17-2002 10:55 PM

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

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