6

We need a command that copy files from current directory to another with given date.

-rw-r----- 1 schmidt schmidt 276423 set 03 10:31 'event01.csv'
-rw-r----- 1 schmidt schmidt 283861 set 03 10:34 'event02.csv'
-rw-r----- 1 schmidt schmidt 280644 set 03 10:40 'event03.csv'
-rw-r----- 1 schmidt schmidt 270234 set 03 03:26 'event04.csv'
-rw-r----- 1 schmidt schmidt 293488 set 12 06:43 'event05.csv'
-rw-r----- 1 schmidt schmidt 287417 set 12 07:48 'event06.csv'
-rw-r----- 1 schmidt schmidt 280519 set 12 10:19 'event07.csv'
-rw-r----- 1 schmidt schmidt 279631 set 12 19:43 'event08.csv'
-rw-r----- 1 schmidt schmidt 286578 set 12 15:15 'event09.csv'
-rw-r----- 1 schmidt schmidt 289830 set 12 18:41 'event10.csv'
-rw-r----- 1 schmidt schmidt 305209 set 19 05:58 'event11.csv'
-rw-r----- 1 schmidt schmidt 308275 set 19 06:20 'event12.csv'
-rw-r----- 1 schmidt schmidt 275015 set 19 14:40 'event13.csv'
-rw-r----- 1 schmidt schmidt 310610 set 19 15:40 'event14.csv'
-rw-r----- 1 schmidt schmidt 311466 set 19 15:41 'event15.csv'
-rw-r----- 1 schmidt schmidt 305241 set 19 15:51 'event16.csv'
-rw-r----- 1 schmidt schmidt 291699 set 19 15:56 'event17.csv'

For example, command should copy all files with "set 12" timestamp to directory /home/12thsep/.

Any ideas?

4 Answers 4

8

You could be selecting by the date option in find

find . -maxdepth 1 -type f -newerat '2019-09-19' ! -newerat '2019-09-20' -exec mv '{}' /home/12thsep \;
6

You may use find with correct mtime arguments. For example:

mkdir <dest_path> && find <source path> -mtime -X -a -mtime +Y -exec mv {} <dest_path>/ \;

Where X represent the "day after" and Y the "day before". Note the use of -a to makes find use a AND between the two conditions specified. Refer to find manpage for more details regarding syntax to use.

3
  • @Fixman answer looks better than mine. I didn't knew about -newerat...
    – binarym
    Commented Oct 21, 2019 at 15:54
  • 1
    FYI -a for AND is the default with find, so isn't needed. you only need to specify a logical operator when you want to use OR (-o). in other words, all predicates are AND-ed together unless you specify otherwise with -o. You'll need parentheses (quoted or escaped on the shell command line, e.g. \( and \)) for nested or more complicated logic operators.
    – cas
    Commented Oct 22, 2019 at 3:51
  • Thx for precisions @cas
    – binarym
    Commented Oct 22, 2019 at 8:17
5

It may be done in following way:

ls -alF|grep "set 12" >> logxa00.txt

cat logxa00.txt| awk '{print $9}' > logxa01.txt

xargs -a logxa01.txt cp -p -t /home/12thsep/
1
  • Be careful with spaces and tabs in filenames. awk might print half a name only.
    – FelixJN
    Commented Oct 23, 2019 at 9:28
0

Try this one out. Just list directory, grep the date stamp, and copy from each filename (argument $9) to your destination directory. The extra quotes in there catch files that have spaces in their name.

ls -al | grep -i "set 12" | awk -F' ' '{ system("cp \"" $9 "\" /home/12thsep") }'

Should be easy to modify in the future for other applications.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .