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 -i or –interactive option, you can prompt before overwriting if the destination file/directory already exists.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## 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 -i or --interactive option cat mydir/mydir1/myfile1 ## returns three cp -i myfile1 mydir/mydir1/ ## -i causes one prompt before overwrite ## cp: overwrite ‘mydir/mydir1/myfile1’? y cat mydir/mydir1/myfile1 ## returns one rm -rf my* |