Copy files in order of creation date

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Copy files in order of creation date
# 1  
Old 09-30-2017
Copy files in order of creation date

Hi everyone :-)

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
Code:
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 :-)

Thank you!
# 2  
Old 09-30-2017
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'.
# 3  
Old 09-30-2017
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.
# 4  
Old 09-30-2017
Note that -t switch does not sort by creation time, it perform sort by modification time
Code:
       -t     sort by modification time

You can use stat to perform this task:-
Code:
stat -c '%Y %n' * | sort -n | while read mtime file; do cp "$file" ./destination/; done

# 5  
Old 09-30-2017
Quote:
Originally Posted by Yoda
You can use stat to perform this task:-
Code:
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.
# 6  
Old 09-30-2017
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:-
Code:
cp "$file" ./destination/; sleep 3;

# 7  
Old 09-30-2017
Use quotation marks around the source file name to copy files with spaces in the name.
ie:
Code:
cp "$file" destination

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash directory loop and order by creation date?

Hello, how in bash i can get directory loop and order by creation date? THX! :) #!/bin/bash for folder in /home/test/* do if ; then echo $folder; fi (12 Replies)
Discussion started by: ZerO13
12 Replies

2. Shell Programming and Scripting

Script to copy creation date over top of modified date?

Can someone draw up a script that for every file, folder and subfolder and files that will copy the creation date over top of the modified date?? I know how to touch every file recursively, but no idea how to read a files creation date then use that to touch the modification date of that file,... (3 Replies)
Discussion started by: toysareforboys
3 Replies

3. Shell Programming and Scripting

copy files based on creation timestamp

Dear friends.. I have the below listing of files under a directory in unix -rw-r--r-- 1 abc abc 263349631 Jun 1 11:18 CDLD_20110603032055.xml -rw-r--r-- 1 abc abc 267918241 Jun 1 11:21 CDLD_20110603032104.xml -rw-r--r-- 1 abc abc 257672513 Jun 3 10:41... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

4. UNIX for Dummies Questions & Answers

Renaming files by changing date order

I'm looking for a simple solution to rename a batch of files. All of the files in this directory start with a date in the format mm.dd.yy followed by a space and then additional descriptive text. Example: 01.21.10 742 P.xlsx 02.24.09 730 Smith.xlsx The information following the date can... (3 Replies)
Discussion started by: kreisel
3 Replies

5. Shell Programming and Scripting

Sorting Files by date and moving files in date order

I need to build a k shell script that will sort files in a directory where files appear like this "XXXX_2008021213.DAT. I need to sort by date in the filename and then move files by individual date to a working folder. concatenate the files in the working folder then start a process once... (2 Replies)
Discussion started by: rebel64
2 Replies

6. Shell Programming and Scripting

Order files by create date

hi Is there a way to sort files in the order they were created ,and move them to another directory one by one ,oldest being the first to be moved. Thanks Arif (4 Replies)
Discussion started by: mab_arif16
4 Replies

7. UNIX for Dummies Questions & Answers

How do I organize a series of files in date order?

I'd like to ls a group of files in date order but I'm not sure what the commands would be. Can anyone help with this? (1 Reply)
Discussion started by: hedgehog001
1 Replies

8. UNIX for Dummies Questions & Answers

list of files in date order

Im on HP/UX and am trying to find the command like an ll but that will sort showing the most currently modified programs first. Can anyone help me with that? :cool: (2 Replies)
Discussion started by: Jeannine
2 Replies

9. UNIX for Dummies Questions & Answers

Moving files based on creation date

Howdy, I'm trying to figure out how to move multiple files based on their creation date. If anyone can enlighten me it would be most appreciated!! Thanks! :D (1 Reply)
Discussion started by: dgoyea
1 Replies
Login or Register to Ask a Question