Copy files in order of creation date

Hi everyone :slight_smile:

I ran into a small issue. I would like to copy some files in the precise order they were created.

So the oldest files should be copied first and the newest ones last.

I tried

cp -r $(ls -1t) ./destination

but the files are still not sorted properly. I was thinking, that maybe the system cannot tell them apart properly as the copy process is so fast, that they basically copy simulatiously. Sounds stupid, but for some reason when I use this command, they are not created at the destination in the order they are stored in the source folder.

I was thinking that maybe a sleep or something is needed in between each copy process. So copy the oldest file, sleep one second, copy the next one....

But I am not sure how to get this done properly.

It would be ideal if this could be solved without a script as I want to run this in an Android terminal.

I hope you guys can help me out :slight_smile:

Thank you!

Were the original files created substantially at the same time?
The output of 'ls -lt' is not a simple file list, I would have thought that it should be 'ls -t'.

There are some files that were created within seconds of each other. But most were created hours, days or months apart.
However, this does not seem to matter during the copying process because if I repeat the same command twice, the time of creation of the new files will differ.

So the same command gives different destination file order despite the same original files.

First run:
1
2
5
3
4
6

Second run:
2
3
1
5
6
4

But unlike the cp command, the ls always gives the same result for the original files.

That's why I was thinking of adding some kind of sleep command after each copied file because I'm guessing the problem is not the original sorting, but the speed of file creation. I just don't know how to do it.

Note that -t switch does not sort by creation time, it perform sort by modification time

       -t     sort by modification time

You can use stat to perform this task:-

stat -c '%Y %n' * | sort -n | while read mtime file; do cp "$file" ./destination/; done

I will try this, thank you!
But do you know how to implement a wait period after each copied file? Say a wait time of 3 seconds?

Plus, I was having trouble making the command include files and directories that include spaces.

I don't think a delay is required after each copy. But if you want to put a delay of 3 seconds, use sleep command:-

cp "$file" ./destination/; sleep 3;

Use quotation marks around the source file name to copy files with spaces in the name.
ie:

cp "$file" destination

Hi,

OK, I was have trouble getting anywhere. Android itself won't accept all the commands and will simply ignore the sleep command.
So I tried hooking my phone up to a Ubuntu VirtualBox. Unfortunately the VirtualBox is unable to properly mount the phone.

So I now created a live USB stick with Ubuntu on it. I am able to access the phone and all my files.

However, when I execute the commands, I get an error message, that the the "regular file" cannot be created. It creates all the folders/subfolders but is unable to write any files.

I can open the drive manually, create and delete folders etc., so access seems to be there, but why can't it write any files?

Does th system have to be a properly installed Linux? Won't it work on a LiveCD-type system?

can you post the exact commands that you used.

Sorry for the delay. I made a new USB Live Distro. This time with Mint instead of Ubuntu. Just to make sure.

But I get the same errors.

mint@mint /media/mint/C/-= D =-/S6edge Backup $ stat -c '%Y %n' * | sort -n | while read mtime file; do cp -R "$file" /run/user/999/gvfs/mtp:host=%5Busb%3A002%2C005%5D/Phone/test/; sleep 1; done
cp: cannot create regular file �/run/user/999/gvfs/mtp:host=%5Busb%3A002%2C005%5D/Phone/test/apps/2017-09-19_23-53.novabackup': Operation not supported
cp: cannot create regular file �/run/user/999/gvfs/mtp:host=%5Busb%3A002%2C005%5D/Phone/test/apps/2017-09-19_23-54 - foldersync.db': Operation not supported
.
.
.

Any ideas? :confused:

Folders were created but no files.

---------- Post updated at 10:34 AM ---------- Previous update was at 07:47 AM ----------

Solved it switching to ptp instead of mtp. But then sleep was ignored and some files failed

Stupid question, but why not use the -p option to cp ? This preserves modification timestamp and attributes.

Andrew

Because Android Nougat only allows root users to modify/write timestamps.
It's a known bug in Android 7.0 and won't be fixed until 8.0, which will not be made available to the Samsung S6 edge :frowning: