LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to change directory into the most recent folder? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-change-directory-into-the-most-recent-folder-4175612212/)

joshjoshjosh 08-18-2017 12:55 PM

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!!!!

jsbjsb001 08-18-2017 12:57 PM

Quote:

Originally Posted by joshjoshjosh (Post 5749684)
I am trying to use a command that changes directory into the most recent folder.

I know how to find and list it but I am unable to change directory into it.

Thank you!!!!

If your using the command-line, you can use the up and down arrows on your keyboard.

schneidz 08-18-2017 12:58 PM

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.

joshjoshjosh 08-18-2017 01:15 PM

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?

Emerson 08-18-2017 01:21 PM

Code:

cd -
This will take you back to most recent directory.

schneidz 08-18-2017 01:26 PM

isnt the most recent directory pwd ?

rtmistler 08-18-2017 01:29 PM

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.

joshjoshjosh 08-18-2017 01:44 PM

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

rknichols 08-18-2017 01:45 PM

If I'm understanding you correctly, you might try this:
Code:

cd "$(ls -td --literal --color=never */ | head -1)"
Much as I hate trying to use the output from "ls" for anything except human viewing, I don't know of another simple command the produces a listing sorted by timestamp. The "--literal" and "--color=never" options should avoid the most common problems.

rtmistler 08-18-2017 03:03 PM

Quote:

Originally Posted by joshjoshjosh (Post 5749709)
'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...

rknichols advice is one option.

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.

joshjoshjosh 08-18-2017 03:09 PM

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

schneidz 08-18-2017 03:10 PM

Code:

ll -tr
?

rknichols 08-18-2017 03:23 PM

Quote:

Originally Posted by joshjoshjosh (Post 5749747)
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...

Works fine for me. Perhaps your "cd" is aliased. Try it with a backslash to bypass the alias:
Code:

\cd "$(ls -td --literal --color=never */ | head -1)"

Emerson 08-22-2017 08:21 PM

Code:

type cd
This will tell you if it is aliased.


All times are GMT -5. The time now is 04:04 AM.