Breako Breako
Breako Breako

Reputation: 6461

Find the directory alphanumerically

I have a bunch of directories that I are named with the following time stamp format:

2013-12-04_18_12_40
2013-12-04_19_12_22
2013-12-04_19_46_45
2013-12-04_20_31_04
2013-12-04_21_04_54
2013-12-05_11_15_24

i.e. YYYY-MM-DD-HH-MM-SS

I want a bash script to CD into the latest directory. So in this case it would be

 2013-12-05_11_15_24

i.e. the latest directory will always be the biggest number.

How do I determine the latest directory in a nice way that can be used in a script?

Thanks

Upvotes: 2

Views: 38

Answers (2)

perreal
perreal

Reputation: 98088

cd "$(find . -print0 | sort -z | tr '\0' '\n' | tail -1)"

Upvotes: 3

anubhava
anubhava

Reputation: 785721

Though generally it is not recommended to parse ls's output but in this case you can probably do:

cd "$(ls -rd [0-9]* | head -1)"

Upvotes: 1

Related Questions