Copy a file and overwrite the existing file
I have two images (image1.png and image2.png) in the same folder on my server, I want to run a command that overwrites image2.png to image1.png. I prefer to overwrite the image instead of deleting it and then replace it by the other one. How can I achieve that via ssh command line?
3 Answers 3
ssh host "cd path/to/directory && cp image1.png image2.png"
The && is safer than ; in case the cd fails, e.g. because of a typo: in such a case, the cp won’t be executed instead of possibly copying a wrong file.
@1_CR The OP didn’t say that he wanted to remove image1.png . But he can use mv if this gives the behavior he wants.
@Minalsaac — you have to elevate permissions — if you can. ssh -t . ‘cd $path && sudo cp ./file1 ./file2’ — but it might be better if you just logged on to a priveleged account initially when connecting with ssh in the first place.
Without sudo the command ssh server «cd path/to/directory && cp image1.png image2.png» doesn’t have privileges to chmod the permissions.
But with sudo it would, but being run after ssh , it never gets password input for it on the remote server, so the solution is use -S and pipe a password for sudo as follows:
ssh server » cd path/to/directory && echo sudo_password | sudo -S chmod 600 image2.png && cp image1.png image2.png»
However, how @terdon marked, we don’t need to change permissions here, but use sudo cp ,so it comes to:
ssh server » cd path/to/directory && echo sudo_password | sudo -S cp image1.png image2.png»
or (in case you think that your password can be read)
ssh server -t » cd path/to/directory && sudo cp image1.png image2.png»
UPDATE: also @terdon warned and I added this, because I think it’s important to stress and make an accent on such possible realization:
I really would remove this suggestion of using -S, it is not needed and very dangerous (see @mikserv’s comments and my answer). It is also pointless. The only «advantage» you mention, that of being able to pipe is a fringe case and in most situations you could just pipe on the server instead. You can also use sshpass as you suggested or set up passwordless sudo. All sorts of ways that don’t store a server’s password as plaintext. – terdon
On the one hand this is good for the automation with no getting sudo password promts and, to have completely automated code/script you would add sshpass -p password ssh. .
However on the server where others can easily read your sudo password provided as open text during the ssh session that’s not recommended from the security perspectives. So, to have a sudo with ssh and be safe use ssh -t
ssh -t server «cd path/to/directory && sudo chmod 600 image2.png && cp image1.png image2.png»
Nevertheless, with -t it’s impossible to pipe ssh «sudo command»| command for example ssh -t server «cd path/to/directory && sudo»|grep «text» but it IS possible with ussage of -S and echoing password, e.g. ssh server ‘echo password | sudo -S ls -l’| grep ‘a’
replace a whole file with another file in bash
Ive learned how to replace a line using bash script but I am wanting to learn how to replace a whole file with another file in a different folder with the same name. Is this possible??
4 Answers 4
cp -f [original file] [new file]
Copies the original file and overwrites the target file (hence -f which stands for «force»).
Looks like you switched up the arguments, It should be the other way around. cp -f [new file] [original file]
In case you are attempting to copy just the content of the file try
cat /first/file/same_name > /second/file/same_name
This will overwrite all the content of the second file with the content from the first. However, your owner, group, and permissions of the second file will be unchanged.
«overwrite content» and «copy» are kinda different operations if the destination file is a symlink 🙂
This is exactly what I needed to deal with hardlinked files updated in a way the caused the link to be broken (saved as a separate file.)
If file2 does not exist, then file1 is renamed file2. If file2 exists, its contents are replaced with the contents of file1.
Like above however, since the «-i» (interactive) option is specified, if file2 exists, the user is prompted before it is overwritten with the contents of file1.
mv file1 file2 file3 dir1
The files file1, file2, file3 are moved to directory dir1. dir1 must exist or mv will exit with an error.
If dir2 does not exist, then dir1 is renamed dir2. If dir2 exists, the directory dir1 is created within directory dir2.
Copy and overwrite a file in shell script
I want to copy a certain file to a location, irrespective of that file already exists in the destination or not. I’m trying to copy through shell script.But the file is not getting copied. I’m using the following command /bin/cp -rf /source/file /destination but that doesn’t work.
cp overrides the destination unless option -n is given, so your command should succeed. Also you didn’t tell us why you think your command fails.
4 Answers 4
cp -fr /source/file /destination
this should probably solve the problem.
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 the bash 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 └── three
$ mkdir one/two/three mkdir: cannot create directory ‘one/two/three’: No such file or directory
Your problem might be caused by an alias for cp command created in your system by default (you can see al your aliases by typing «alias»). For example, my system has the following alis by default: alias cp=’cp -i’, where -i overrides -f option, i.e. cp will always prompt for overwriting confirmation.
What you need in such case (that’ll actually work even if you don’t have an alias) is to feed «yes» to that confirmation. To do that simply modify your cp command to look like this:
yes | cp /source/file /destination
/bin/cp -rf src dst or /usr/bin/env cp -rf
this is because OP could unknowingly have cp aliased to ‘cp -i’, and I think some (all?) systems will prioritize the -i option. For example, on my system: > which cp alias cp=’cp -i’ /bin/cp > cp abc.txt efg.txt cp: overwrite efg.txt’? n > cp -f abc.txt efg.txt cp: overwrite efg.txt’? n
How to replace all the contents from one folder with another one
I have folders old and new . I want to replace all the contents in old/* to new/* . There can be many subdirectories in those folders. But there can be few files which are not in new/* but are in old/* , so I want them to stay as they are. How can I do that from a Linux shell?
3 Answers 3
rsync would probably be a better option here. It’s as simple as rsync -a subdir/ ./. check this unix.stackexchage answer for better solutions
use -f with the cp command
suppress cp to overwrite» prompt..
To override cp’s alias you can simply enclose it in quotes:
for more information follow these links:
Don’t use rsync for a one time copy, the overhead of hashing all dir contents is substantial, it only pays when keeping directories synchronized. For a gigantic one time copy, use a tarpipe.
Use rsync . It will synchronize the directories in one direction. So, if you want to update your old folder with everything from new , but keep what’s in there, just use:
rsync -avh --dry-run /path/to/new/ /path/to/old/
This will, in a first instance, just output the list of files that would be transferred. In that case: Everything found in new will be copied to old , unless it’s already there. Everything in old stays as it is.
If it looks fine to you, remove the —dry-run argument to transmit them for real.
The -avh flags just enable archive mode (which will preserve timestamps, etc.), verbosity and human-readable file-sizes. Nothing will be deleted from the destination unless you specify the —delete flag. Consult man rsync for more information.