Copy and Overwrite a File in Shell Script

Copy and overwrite a file in shell script

This question has been already discussed, however you can write a little script like this:

#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"

Explaining this script a little bit

  1. #!/bin/bash: tells your computer to use the bash interpreter.

  2. if [ ! -d "$2" ]; then: If the second variable you supplied does not already exist...

  3. mkdir -p "$2": make that directory, including any parent directories supplied in the path.

    Running mkdir -p one/two/three will make:

    $ mkdir -p one/two/three
    $ tree one
    one/
    └── two
    └── three

    If you don't supply the -p tag then you'll get an error if directories one and two don't exist:

    $ mkdir one/two/three
    mkdir: cannot create directory ‘one/two/three’: No such file or directory
  4. fi: Closes the if statement.

  5. cp -R "$1" "$2": copies files from the first variable you supplied to the directory of the second variable you supplied.

    So if you ran script.sh mars pluto, mars would be the first variable ($1) and pluto would be the second variable ($2).

    The -R flag means it does this recursively, so the cp command will go through all the files and folders from your first variable, and copy them to the directory of your second variable.

Write to file, but overwrite it if it exists

The >> redirection operator will append lines to the end of the specified file, where-as the single greater than > will empty and overwrite the file.

echo "text" > 'Users/Name/Desktop/TheAccount.txt'

Bash script to copy a folder's contents and overwrite destination?

Here's how I ended up doing it:

#!/bin/bash
while IFS= read -r line || [[ $line ]]; do
if [[ -d "$line" ]]; then
rsync -rv --exclude-from "exclude.txt" /home/source/public_html/ "$line"
else
echo "Directory $line does not exist!"
fi
done < "accounts.txt"

This reads from a file called "accounts.txt" that has all the destination account directories in it, one per line. Ex:

/home/user1/public_html
/home/myblog/public_html

It loops through each one, and if the directory exists, it copies the files from /home/source/public_html to the destination. It also reads from a file called "exclude.txt" that includes, one per line, any files or directories to exclude. Ex:

.git
/uploads/custom
/config/conf.php

Shell Script (Linux): Copy and overwrite only files that have changed in destination?

rsync can do this:

rsync -av source/ destination/ 

This command will also print out the list of files that have been replaced.

XCOPY: Overwrite all without prompt in BATCH

The solution is the /Y switch:

xcopy "C:\Users\ADMIN\Desktop\*.*" "D:\Backup\" /K /D /H /Y

Copy does not prompt for overwriting existing file

Check out this document

Apparently, in windows 2000 and above, the default action is to prompt on overwrite unless the command is being executed from within a batch file.

Also mentioned here

Try this

/-Y 

instead of

/Y

/-Y Causes prompting to confirm you want to overwrite an existing destination file.

replace a whole file with another file in bash

cp -f [original file] [new file]

Copies the original file and overwrites the target file (hence -f which stands for "force").



Related Topics



Leave a reply



Submit