How to change directory into the most recent folder?
I am trying to use a command that changes directory into the most recent directory.
I know how to find and list it but I am unable to change directory into it. I am thinking the ideal solution would be a find command that gets the newest directory and outputs it into xargs which changes directory...however I cannot get this to work. Anyone know? Thank you!!!! |
Quote:
|
not quite sure what you mean by folder (directory ?) ?
the cd command is to change directories and the pwd command is to find out what the present working directory is. the history command will print out the latest commands entered; the bash (most) shells allow you to hit up-arrow to cycle thru commands in the history. |
I know how to use the cd and pwd commands. I think using the find command with xargs to cd into the most recent directory would be possible but I cannot get it to work. Anyone know?
|
Code:
cd - |
isnt the most recent directory pwd ?
|
Without some better qualification about what you call a recent directory, the best answer to me would be what Emerson points out.
Otherwise I feel you'd have to use a shell variable to store a particular directory for later use when you wish to return to it. Are you writing a script? A typical technique I've used when writing a script is assuming first that the script can be run from anywhere, and upon entering the script, if I know I intend to change directories, I'll save my current working directory as a shell variable, typically HOME for me. And then at the correct time for me, such as near the end of the script, I will use that variable to return me to the original starting directory. |
'cd -' only takes me to the last directory I was in.
I'll try to give more details.... I have a directory where new directories are getting created into it on a regular basis. I would like a simple command to be able to change directory into the newest directory without me having to list it out and determine on my own... |
If I'm understanding you correctly, you might try this:
Code:
cd "$(ls -td --literal --color=never */ | head -1)" |
Quote:
I feel it depends how these new directories are being created. If they are being created by a process of some type that is capable of reporting the directory name and location, then this information can be directly used. I do feel that the creation of these new directories is potentially by an application or process where the time for creation and name of the new directory may be only something where your code can "detect" it, after the fact. That brings it back to something along the lines of what rknichols suggested, a command line call which identifies by newest file time and date. |
Thank you for the help thus far. "ls -td --literal --color=never */ | head -1" works fine but "cd "$(ls -td --literal --color=never */ | head -1)"" outputs "Illegal variable name." My directories have no special characters so not sure what is causing this...
|
Code:
ll -tr |
Quote:
Code:
\cd "$(ls -td --literal --color=never */ | head -1)" |
Code:
type cd |
All times are GMT -5. The time now is 04:04 AM. |