Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 310
DᴀʀᴛʜVᴀᴅᴇʀ Avatar asked Aug 30 '12 14:08

DᴀʀᴛʜVᴀᴅᴇʀ


People also ask

How do I replace a file with another file?

Right-click the document file the content of which you want to replace. Press the Alt key and select Operations > Replace with File... from the menu bar. Locate and select the file that you want to use for replacing the original file content. Click OK.

How do I change the content of a file in bash?

To replace content in a file, you must search for the particular file string. The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.


2 Answers

cp -f [original file] [new file]

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

like image 155
Nick Brunt Avatar answered Oct 13 '22 05:10

Nick Brunt


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.

like image 42
Joseph Avatar answered Oct 13 '22 05:10

Joseph