I have use sudo find all files which match the file type and copy it to a file directory then tee the report. I would like to include function to check file existence so that file from different folder will not overwrite the wrong file.

Please suggest an idea to fix my function also. Thank you.

sudo find / -iname "*[email protected]" -cp -v {} $directory \; | tee report.txt

function checkFile(){
    if [ -f #filename];
        # add number

1 Answers

Mathieu On

You can do this using the -exec option of find:

find / -type f -iname "*[email protected]" -exec cp --backup=numbered {} $directory \; -exec echo {}  >> report.txt \;


-exec cp --backup=numbered Will execute cp for each found file, with option --backup=numbered, which will prevent overwritting

-exec {} >> report.txt Which will add the copied file name to the report file.