In folder I have text files. I want to find them and copy to created folder. Everything works correctly. The code creates a folder and copy files, but console displays:
cp: './02_2017/lks07.txt' and '02_2017/lks07.txt' are the same file02_2017 is created folder.
Something is wrong in code but I don't know what
This is the code:
date1=$(date '+%Y-%m-%d' -d "-0 month -$(($(date +%d)-1)) days")
date2=$(date '+%Y-%m-%d' -d "-$(date +%d) days +1 month")
date=$(date +'%m_%Y')
mkdir -m 777 $date
find ./ -type f -name '*.txt' -newermt $date1 ! -newermt $date2 -exec cp {} $date \;Can someone please help me whit this ?
12 Answers
This problem occurs because find will look for files sequentially. For example, consider this directory layout:
$ tree
.
├── 02_2017
│ └── foo.txt
└── bar.txtWhen you run your find command, it will first find any matching files in the top level directory, so it finds bar.txt and moves it into 02_2017. Then, it will go into 02_2017 and look for files there. There is now a 02_2017/bar.txt file, so it tries to copy it to itself, fails, and prints that error message.
This is not really a problem. Your script is working fine and is doing its job correctly. You can safely ignore the error.
If it really bothers you, you can fix by adding -maxdepth 1 to your find command so it doesn't descend into subdirectories:
find ./ -type f -name '*.txt' -newermt $date1 ! -newermt $date2 -exec cp {} $date \;Or, even better, by excluding the target directory from find's search path:
find ./ -type f -not -path './02_2017/*' -name '*.txt' -newermt $date1 ! -newermt $date2 -exec cp {} $date \; I tried to accomplish the same task and I received the same error some time ago. The idea was to find all files that were recently modified in a period and back them up separately. Although it is worth mentioning that despite the error from cp the files were being copied. Still I did not like to have an error in the script so I adjusted like this:
find /mnt/net_shares/common/ -type f -mtime -15 >> /tmp/common_mod15.$$.txtThis will find all files modified the past 15 days and create a list of them in a text file. The $$ operator is the process ID of the script running. This ensures if I have overlapping processes they will not use the same file, makes it unique to the process ID.
rsync -azv --files-from=/tmp/common_mod15.$$.txt / /mnt/hd2/backups/local/mod15/common/The second step is to use rsync instead of cp. You can define a text list with --files-from switch, and also it will compare files and only copy what has been modified from the files in my backup location. Whereas cp would copy and replace everything.
Note that the 'extra' slash in /tmp/common_mod15.$$.txt **/** /mnt... is not a typo but I do not remember exactly why I needed it. Had to do with relative vs absolute path. Depending on your generated list you might need it, you might not. But I see that you are working with relative paths also (./ operator) so probably you do need it.
Goes without say that to test it in terminal you need to get rid of the $$ operator. It only belongs in a script which creates a process ID. Also remember to delete your temporary created file to keep a clean /tmp folder.