LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cd'ing into last created directory (https://www.linuxquestions.org/questions/linux-newbie-8/cding-into-last-created-directory-749310/)

seycyrus 08-21-2009 10:05 AM

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.

rizhun 08-21-2009 10:09 AM

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'.

David the H. 08-21-2009 10:25 AM

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.

i92guboj 08-21-2009 01:03 PM

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.

David the H. 08-21-2009 08:29 PM

Quote:

Originally Posted by i92guboj (Post 3652703)
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.

i92guboj 08-21-2009 08:44 PM

Quote:

Originally Posted by David the H. (Post 3653104)
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 :)

seycyrus 08-24-2009 01:38 PM

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!" :)

colucix 08-24-2009 02:21 PM

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.


All times are GMT -5. The time now is 12:22 PM.