LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-18-2017, 12:55 PM   #1
joshjoshjosh
LQ Newbie
 
Registered: Aug 2017
Posts: 6

Rep: Reputation: Disabled
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!!!!

Last edited by joshjoshjosh; 08-18-2017 at 01:16 PM.
 
Old 08-18-2017, 12:57 PM   #2
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Quote:
Originally Posted by joshjoshjosh View Post
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.
 
Old 08-18-2017, 12:58 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

Last edited by schneidz; 08-18-2017 at 12:59 PM.
 
Old 08-18-2017, 01:15 PM   #4
joshjoshjosh
LQ Newbie
 
Registered: Aug 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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?
 
Old 08-18-2017, 01:21 PM   #5
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
Code:
cd -
This will take you back to most recent directory.
 
3 members found this post helpful.
Old 08-18-2017, 01:26 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
isnt the most recent directory pwd ?
 
Old 08-18-2017, 01:29 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 08-18-2017, 01:44 PM   #8
joshjoshjosh
LQ Newbie
 
Registered: Aug 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
'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...
 
Old 08-18-2017, 01:45 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.
 
Old 08-18-2017, 03:03 PM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by joshjoshjosh View Post
'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.
 
Old 08-18-2017, 03:09 PM   #11
joshjoshjosh
LQ Newbie
 
Registered: Aug 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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...
 
Old 08-18-2017, 03:10 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Code:
ll -tr
?
 
Old 08-18-2017, 03:23 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by joshjoshjosh View Post
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)"
 
Old 08-22-2017, 08:21 PM   #14
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,661

Rep: Reputation: Disabled
Code:
type cd
This will tell you if it is aliased.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me into how to change directory into a non-alphanumeric named folder jheengut Linux - Software 10 04-26-2016 08:01 AM
Cannot change directory/folder in SCRIPT GONMAN1 Linux - Newbie 3 09-10-2015 01:39 AM
Incrontab Directory Change (watched Folder = Mounted FTP Account) kunalks Linux - Software 2 05-25-2015 11:18 AM
Recent Mailcap Change? lopid Slackware 2 01-09-2011 07:49 AM
how to grab most recent directory toddcurry Programming 2 05-02-2005 10:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration