简体   繁体   中英

find all files with .sh .cpp .c extension and copy them to a directory in my desktop, if there is a file with the same name, rename it

I have to copy all files with .sh .cpp .c extensions to a directory at my desktop and rename the files with the same name. If I have two files named hello.cpp, change the name to hello1.cpp.

I already tried this:

find /root \ (-name *.cpp -o -name *.sh \) -exec cp {} /root/Desktop/myNewDir \; 

but I keep getting this error:

cp: '/root/Desktop/mynewDir/hello.cpp' and '/root/Desktop/myNewDir/hello.cpp' are the same file

This is happening because you are traversing recursively on /root and find -ing the same file to copy over it.

If you want to stick to recursion, then -prune /root/Desktop/myNewDir/ from find 's recursion:

find /root/ -type d -name /root/Desktop/myNewDir/ -prune -o -type f \
     \( -name '*.cpp' -o -name '*.sh' \) -exec cp --backup=numbered 
        -t /root/Desktop/myNewDir/ {} +

If you don't want recursion, this gets very easy:

find /root/ -maxdepth 1 -type f \( -name '*.cpp' -o -name '*.sh' \) \
    -exec cp -t /root/Desktop/myNewDir/ {} +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM