I am trying to create a directory based on the timestamp of the 2 newest files in a directory and then copy those 2 files in to the newly created directory. SO for example

-rw-r--r-- 1 root root 0 Sep 24 12:01 a

-rw-r--r-- 1 root root 0 Sep 24 12:01 b

I want to create a directory called 20190924 and copy a and b into the directory which are the newest files in the current directory

  • 4
    What if those two files have different dates in their timestamp? – sudodus Sep 24 '19 at 11:25
  • 1
    The scenerio i'm working on is that only 2 files are generated everyday – Rhus Sep 24 '19 at 13:43
  • How does that answer @sudodus' question? – Jim L. Sep 24 '19 at 21:42

This zsh script will pick up the two newest (plain) files in the current directory, gather the modification timestamp of the most recent one, convert that timestamp to YYYYmmdd format, create the directory, then copy those two newest files into that directory:


newest2=( *(.om[1,2]) )
dsec=$( stat -c %Y "${newest2[1]}" )
dnam=$( date -d @"$dsec" +%Y%m%d )
mkdir "$dnam"
cp -- "${newest2[@]}" "$dnam"

The first line, assigning newest2, expands the * wildcard with a qualifier (in parenthesis). The qualifier asks for:

  • . -- plain files
  • om -- ordered by modification time (newest to oldeset)
  • [1,2] -- slice the list to include only elements 1 through 2

We then ask stat for the modification time in seconds of the newest file; that date is passed in to GNU date, who returns the directory name in the format we want. After creating the directory, the cp command copies the two files into that directory.

| improve this answer | |

Here we are assuming that you want the name of the folder extracted from the first file:

Create the directory

dir="$(mkdir -p $(ls -ltp --full-time | grep -v / | awk '{print $6}' | sed -n '2p' | sed 's/-//g'))"

List only files grep -v /, sorted by time lt, show the full time (so we have the date) --full-time.

ls -ltp --full-time | grep -v /

Choose the column of dates

awk '{print $6}'

Retrieve the second line (the first line shows the number of files) that correspond to the first file and delete the dashes:

sed -n '2p' | sed 's/-//g'

Copy the files

cp $(ls -ltp --full-time | grep -v / | awk '{print $9}' | sed -n '2,3p' | tr '\n' ' ') "${dir}/"

List only files grep -v /, sorted by time lt, show the full time (so we have the date) --full-time.

ls -ltp --full-time | grep -v /

Retrieve the names of the files:

awk '{print $9}'

Choose the first and the second file

sed -n '2,3p'

Substitute the the new line for a space, so we can perform a copy in one line.

tr '\n' ' '

The command would look like this

cp x z 20190924/
| improve this answer | |
  • Thanks for this, however the copy part does not work. The directory gets created but its empty – Rhus Sep 24 '19 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.