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
#!/bin/bash
: tells your computer to use thebash
interpreter.if [ ! -d "$2" ]; then
: If the second variable you supplied does not already exist...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
└── threeIf you don't supply the
-p
tag then you'll get an error if directoriesone
andtwo
don't exist:$ mkdir one/two/three
mkdir: cannot create directory ‘one/two/three’: No such file or directoryfi
: Closes the if statement.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
) andpluto
would be the second variable ($2
).The
-R
flag means it does this recursively, so thecp
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
What Does an Asterisk at The End of a Mv Command Do
Command to Check Status of Message Queue and Shared Memory in Linux
How to Get The Bash Date Script to Return a Day of The Week Relative to a Non-Current Time
How to Run a Script After User Login Authentication in Linux
Linux: Disable Using Loopback and Send Data via Wire Between 2 Eth Cards of One Comp
Qemu on Raspberry Pi Arch Linux Latest Sd Image
Why Linux Always Output "^C" Upon Pressing of Ctrl+C
How to Convert Fixed Size Dimension to Unlimited in a Netcdf File
How to Diagnose a Python Process Chewing CPU in Linux
Why Having to Use Non-Blocking Fd in a Edge Triggered Epoll Function
Understanding /Boot/Config File
Autoconf Complains "C Compiler Cannot Create Executables" on Linux Mint
How to Create a Real Thread with Clone() on Linux
Linux: How to Put a Load on System Memory
Deploying War in Jboss 7.0.1 Through Commandline
How to Get The Percent of Packets Received from Ping in Bash