Linux cp command by default overwrites the destination file/directory with source file/directory and if the destination file/directory does not exist then it simply copies the source file/directory to the destination file/directory.
Using cp -n or –no-clobber option, you can disable overwriting of destination file/directory if already exists.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## cp command without any option mkdir -p mydir1 mydir/mydir1 ## create some files and directories echo "one" > myfile1 echo "two" > mydir/myfile1 echo "three" > mydir1/myfile1 echo "four" > mydir/mydir1/myfile1 cat mydir/mydir1/myfile1 ## returns four cp -r mydir1 mydir ## overwrites mydir1 and its content inside mydir with mydir1 and its content cat mydir/mydir1/myfile1 ## returns three cat mydir/myfile1 ## returns two cp myfile1 mydir ## overwrites myfile1 inside mydir with source myfile1 cat mydir/myfile1 ## returns one ## cp command with -n or --no-clobber option cat mydir1/myfile1 ## returns three cp -n myfile1 mydir1/myfile1 ## does nothing since myfile1 already exist inside mydir1 cat mydir1/myfile1 ## returns three rm -r my* |