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. |
How about:
cdlast: Code:
#!/bin/bash |
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}')"; } |
You are gotta love simplicity :)
Code:
ls -td */ | head -n1 Code:
function cdlast() { |
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. |
Quote:
Quote:
|
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!" :) |
You don't really need the environment variable, since you can use command substitution in aliases:
Code:
alias cdlast 'cd `\ls -td */ | head -n1`' Code:
setenv LATEST '\ls -td */ \174 head -n1' |
All times are GMT -5. The time now is 12:22 PM. |