Unix Shell - cd to last directory in the listing

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > cd to last directory in the listing





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author cd to last directory in the listing
julia

2007-01-19, 7:29 pm

I have a directory that includes several sub-directories all with names
of the form: TR_YYYY-MM-DD_HHMMSS (format of the subdirectory names).

I frequently need to cd to the sub-directory that has the latest date
in its name. I'd like to find a way to do that with some type of
shortcut or alias so that I don't have to always type in the directory
name as the "cd" command argument. (within a tcsh shell)

Although the information in the directory name is a date, because of
the order in which it is specified, an alphanumeric listing actually
lists them in order with the latest date last. So, no actual
interpretation of a date is required.

All I really need is a way to pull off the last directory that is
listed with ls and feed that into the cd, but I don't know what I could
use to extract the last directory from the ls list.

I could use PERL to read the list of directory name, but then how do I
pass the directory name back to the shell to be used as the cd
argument?

I have very little experience with awk, so I don't know if it offers
something useful to this.

The directory with the latest date in its name is not necessarily the
directory with the latest creation date, but that will frequently be
the case. So, if using the latest creation date is the easiest way to
do this, that would be OK.

The directory with the latest date in its name is also not necessarily
the directory that was modified last, but that will (less) often be the
case. So, using the latest modified directory is also option if that's
the easiest way.

I'd appreciate any suggestions for how to approach this.

Julia Bell

Steffen Schuler

2007-01-19, 7:29 pm

julia wrote:
> I have a directory that includes several sub-directories all with names
> of the form: TR_YYYY-MM-DD_HHMMSS (format of the subdirectory names).
>
> I frequently need to cd to the sub-directory that has the latest date
> in its name. I'd like to find a way to do that with some type of
> shortcut or alias so that I don't have to always type in the directory
> name as the "cd" command argument. (within a tcsh shell)
>
> Although the information in the directory name is a date, because of
> the order in which it is specified, an alphanumeric listing actually
> lists them in order with the latest date last. So, no actual
> interpretation of a date is required.
>
> All I really need is a way to pull off the last directory that is
> listed with ls and feed that into the cd, but I don't know what I could
> use to extract the last directory from the ls list.
>
> I could use PERL to read the list of directory name, but then how do I
> pass the directory name back to the shell to be used as the cd
> argument?
>
> I have very little experience with awk, so I don't know if it offers
> something useful to this.
>
> The directory with the latest date in its name is not necessarily the
> directory with the latest creation date, but that will frequently be
> the case. So, if using the latest creation date is the easiest way to
> do this, that would be OK.
>
> The directory with the latest date in its name is also not necessarily
> the directory that was modified last, but that will (less) often be the
> case. So, using the latest modified directory is also option if that's
> the easiest way.
>
> I'd appreciate any suggestions for how to approach this.
>
> Julia Bell
>

try (works with bash, should be one line):

alias cdl='cd $(ls -dp * | egrep
'''^TR_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}/$''' | tail -n 1)'

call it with cdl in the base directory.

Greetings from Munich,

Steffen Schuler
julia

2007-01-19, 7:29 pm

I forgot about tail - that will do it.

But, now of course I'm excited to get this fancy version to work.

I'm having trouble with the egrep command. I can't get it to work.

What is the purpose of the '\ in the beginning and end of the egrep
argument?
Also, please confirm that the " prior to the ^ and just before the |
tail ... are double quotes.

Julia

Steffen Schuler wrote:

> try (works with bash, should be one line):
>
> alias cdl='cd $(ls -dp * | egrep
> '''^TR_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}/$''' | tail -n 1)'
>
> call it with cdl in the base directory.
>
> Greetings from Munich,
>
> Steffen Schuler


Steffen Schuler

2007-01-19, 7:29 pm

julia wrote:
> I have a directory that includes several sub-directories all with names
> of the form: TR_YYYY-MM-DD_HHMMSS (format of the subdirectory names).
>
> I frequently need to cd to the sub-directory that has the latest date
> in its name. I'd like to find a way to do that with some type of
> shortcut or alias so that I don't have to always type in the directory
> name as the "cd" command argument. (within a tcsh shell)
>
> Although the information in the directory name is a date, because of
> the order in which it is specified, an alphanumeric listing actually
> lists them in order with the latest date last. So, no actual
> interpretation of a date is required.
>
> All I really need is a way to pull off the last directory that is
> listed with ls and feed that into the cd, but I don't know what I could
> use to extract the last directory from the ls list.
>
> I could use PERL to read the list of directory name, but then how do I
> pass the directory name back to the shell to be used as the cd
> argument?
>
> I have very little experience with awk, so I don't know if it offers
> something useful to this.
>
> The directory with the latest date in its name is not necessarily the
> directory with the latest creation date, but that will frequently be
> the case. So, if using the latest creation date is the easiest way to
> do this, that would be OK.
>
> The directory with the latest date in its name is also not necessarily
> the directory that was modified last, but that will (less) often be the
> case. So, using the latest modified directory is also option if that's
> the easiest way.
>
> I'd appreciate any suggestions for how to approach this.
>
> Julia Bell
>

in tcsh use in one line:

alias cdl 'cd `ls -dp * |
egrep '''^TR_[0-9]{4}(-[0-9]{2}){2}_[0-9]{6}/$''' |
tail -n 1`'

Greetings from Munich,

Steffen Schuler
Steffen Schuler

2007-01-19, 7:29 pm

julia wrote:
> I forgot about tail - that will do it.
>
> But, now of course I'm excited to get this fancy version to work.
>
> I'm having trouble with the egrep command. I can't get it to work.
>
> What is the purpose of the '\ in the beginning and end of the egrep
> argument?
> Also, please confirm that the " prior to the ^ and just before the |
> tail ... are double quotes.
>
> Julia
>
> Steffen Schuler wrote:
>
>

no the "double quotes before and after egrep are indeed two single
quotes. ''' are: apos backslash apos apos
So one embeds an apos is an apos delemited string.

Greetings from Munich,

Steffen Schuler
Steffen Schuler

2007-01-19, 7:29 pm

julia wrote:
> I forgot about tail - that will do it.
>
> But, now of course I'm excited to get this fancy version to work.
>
> I'm having trouble with the egrep command. I can't get it to work.
>
> What is the purpose of the '\ in the beginning and end of the egrep
> argument?
> Also, please confirm that the " prior to the ^ and just before the |
> tail ... are double quotes.
>
> Julia
>
> Steffen Schuler wrote:
>
>

instead of tail -n 1 you could also use: sed '$!d'

Greetings from Munich,

Steffen Schuler
Chris F.A. Johnson

2007-01-19, 7:29 pm

On 2007-01-19, Steffen Schuler wrote:
> julia wrote:
> try (works with bash, should be one line):
>
> alias cdl='cd $(ls -dp * | egrep
> '''^TR_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{6}/$''' | tail -n 1)'
>
> call it with cdl in the base directory.


In bash you don't need any external commands, let alone three:

cdl()
{
set -- ./TR_*/ ## adjust pattern if necessary
shift "$(( $# - 1 ))"
cd "$1"
}

However, the OP is using tcsh.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Chris F.A. Johnson

2007-01-19, 7:29 pm

On 2007-01-19, Chris F.A. Johnson wrote:
> On 2007-01-19, Steffen Schuler wrote:

....
[vbcol=seagreen]
> cdl()
> {
> set -- ./TR_*/ ## adjust pattern if necessary
> shift "$(( $# - 1 ))"
> cd "$1"
> }


Even simpler:

cdl()
{
: ./*/ ## adjust pattern if necessary
cd "$_"
}


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Michal Nazarewicz

2007-01-19, 7:29 pm

"julia" <juliabell@worldnet.att.net> writes:

> I have a directory that includes several sub-directories all with names
> of the form: TR_YYYY-MM-DD_HHMMSS (format of the subdirectory names).
>
> I frequently need to cd to the sub-directory that has the latest date
> in its name. I'd like to find a way to do that with some type of
> shortcut or alias so that I don't have to always type in the directory
> name as the "cd" command argument. (within a tcsh shell)


> All I really need is a way to pull off the last directory that is
> listed with ls and feed that into the cd, but I don't know what I could
> use to extract the last directory from the ls list.



#v+
last_dir () ( # paren for local variables
[ -n "$1" ] || set -- .
answer=
for dir in "$1/"*/; do answer=$dir; done
if ! [ -d "$answer" ]; then
echo no directory found >&2
return 1
fi
printf '%s\n' "$answer"
)

cdld () {
set -- $(last_dir "$@")
[ -z "$1" ] || cd -- "$1"
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Patrick

2007-01-22, 1:16 pm


julia wrote:
> I have a directory that includes several sub-directories all with names
> of the form: TR_YYYY-MM-DD_HHMMSS (format of the subdirectory names).
>
> I frequently need to cd to the sub-directory that has the latest date
> in its name. I'd like to find a way to do that with some type of
> shortcut or alias so that I don't have to always type in the directory
> name as the "cd" command argument. (within a tcsh shell)
>
> Although the information in the directory name is a date, because of
> the order in which it is specified, an alphanumeric listing actually
> lists them in order with the latest date last. So, no actual
> interpretation of a date is required.
>
> All I really need is a way to pull off the last directory that is
> listed with ls and feed that into the cd, but I don't know what I could
> use to extract the last directory from the ls list.
>
> I could use PERL to read the list of directory name, but then how do I
> pass the directory name back to the shell to be used as the cd
> argument?
>
> I have very little experience with awk, so I don't know if it offers
> something useful to this.
>
> The directory with the latest date in its name is not necessarily the
> directory with the latest creation date, but that will frequently be
> the case. So, if using the latest creation date is the easiest way to
> do this, that would be OK.
>
> The directory with the latest date in its name is also not necessarily
> the directory that was modified last, but that will (less) often be the
> case. So, using the latest modified directory is also option if that's
> the easiest way.
>
> I'd appreciate any suggestions for how to approach this.
>
> Julia Bell


cd `ls -1 | sort -r | head -1`

Stephane CHAZELAS

2007-01-22, 1:16 pm

2007-01-19, 12:49(-08), julia:
> I have a directory that includes several sub-directories all with names
> of the form: TR_YYYY-MM-DD_HHMMSS (format of the subdirectory names).
>
> I frequently need to cd to the sub-directory that has the latest date
> in its name. I'd like to find a way to do that with some type of
> shortcut or alias so that I don't have to always type in the directory
> name as the "cd" command argument. (within a tcsh shell)
>
> Although the information in the directory name is a date, because of
> the order in which it is specified, an alphanumeric listing actually
> lists them in order with the latest date last. So, no actual
> interpretation of a date is required.

[...]

with zsh:

cd TR*(/[-1])

--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com