 |
|
 |
|
|
 |
cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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]
23;6}/$''' | tail -n 1)'
>
> call it with cdl in the base directory.
>
> Greetings from Munich,
>
> Steffen Schuler
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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]
23;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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-20-07 12:29 AM
"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--
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: cd to last directory in the listing |
 |
 |
|
|
01-22-07 06: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`
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 01:55 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|