Question

I have a parent folder where every day a new subdirectory is automatically created. The subdirectory naming format is '2013-10-02T1430+0000'. Later on today the new subdirectory created will be named ''2013-10-03T1430+0000'.

I need a script which will on a schedule automatically copy only the contents of the latest subdirectory created to another folder.

I have the cp command down ok but I need to somehow subsitute in the cp source the latest subdirectory name. After doing some research I found the following command will return the latest or newest subdirectory:

ls -tr ParentFolder | tail -1

How do I get the result of this command to be placed into the cp command? Is there an easier way of doing this?

Many thanks to all in advance.

Was it helpful?

Solution

To save the result of a command you can use the var=$(command) expression.

In your case,

last_dir=$(ls -tr ParentFolder | tail -1)

So then you can cp $last_dir /another/path, etc.

Note by the way that parsing ls is not very optimal, so if you know that the dir name will always have today's date, you can get it with the date command:

$ date
Thu Oct  3 11:02:28 CEST 2013

And its options:

$ date "+%Y-%m-%dT%H%M%z"
2013-10-03T1108+0200
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top