Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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
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`'
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.