LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 01: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 02:16 PM.
 
Old 08-18-2017, 01:57 PM   #2
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,416

Rep: Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762Reputation: 1762
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, 01:58 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,267

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
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 01:59 PM.
 
Old 08-18-2017, 02: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, 02:21 PM   #5
Emerson
LQ Guru
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~arch
Posts: 6,316

Rep: Reputation: Disabled
Code:
cd -
This will take you back to most recent directory.
 
3 members found this post helpful.
Old 08-18-2017, 02:26 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,267

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

Rep: Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696
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, 02: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, 02:45 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,328

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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, 04:03 PM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,299
Blog Entries: 13

Rep: Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696Reputation: 3696
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, 04: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, 04:10 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,267

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
Code:
ll -tr
?
 
Old 08-18-2017, 04:23 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,328

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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, 09:21 PM   #14
Emerson
LQ Guru
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~arch
Posts: 6,316

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:01 AM
Cannot change directory/folder in SCRIPT GONMAN1 Linux - Newbie 3 09-10-2015 02:39 AM
Incrontab Directory Change (watched Folder = Mounted FTP Account) kunalks Linux - Software 2 05-25-2015 12:18 PM
Recent Mailcap Change? lopid Slackware 2 01-09-2011 08:49 AM
how to grab most recent directory toddcurry Programming 2 05-02-2005 11:33 PM

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

All times are GMT -5. The time now is 04:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration