Overwriting files in linux

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.

Источник

How to Force cp Command to Overwrite without Confirmation

The cp command (which stands for a copy) is one of the commonly used commands on Linux and other UNIX-like operating systems, for copying files and directories. In this guide, we will show how to force the cp command to overwrite a copy operation without confirmation in Linux.

Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown.

# cp bin/git_pull_frontend.sh test/git_pull_frontend.sh

To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.

# cp -i bin/git_pull_frontend.sh project1/git_pull_frontend.sh

By default, modern Linux distributions especially those in the Red Hat Enterprise Linux (RHEL) family come with an alias for the cp command which makes a user run the cp command in interactive mode. This may not be the case on Debian and Ubuntu derivatives.

To check all your default aliases, run the alias command as shown.

Читайте также:  Установка ключа ssh linux

View All Linux Aliases

The highlighted alias in the above screenshot implies that when you run the command, by default it will run in interactive mode. Even when you use the yes command, the shell will still prompt you to confirm the overwrite.

Run Copy Command With Confirmation

The best way to force the overwrite is to use a backward slash before the cp command as shown in the following example. Here, we are copying contents of the bin directory to test directory.

Force cp Command to Overwrite Files without Confirmation

Alternatively, you can unalias the cp alias for the current session, then run your cp command in the non-interactive mode.

# unalias cp # cp -r bin test

Unalias cp Command Alias

For more information, see the cp command man page.

If you have any questions, ask us via the feedback form below.

Источник

This website needs your consent to use cookies in order to customize ads and content.

If you give us your consent, data may be shared with Google.

Overwrite or Truncate Files in Linux

These are the easiest ways to overwrite and truncate files in Linux.

d

One of the easiest-to-remember ways to overwrite a file completely, emptying its contents, is to just > /path/to/file.txt – but you can also use echo to write an empty string to the file. E.g: echo «» > /path/to/file.txt.

The shortest way to overwrite file:

Note. Some shells may use : > /path/to/file.txt instead – the : is a shell builtin that does not return anything.

truncate -s 0 /path/to/file.txt 

Using a single greater than sign will completely overwrite the file:

Using (>>) double greater than will append the output to the end of the file:

echo "Added to the end of file." >> /path/to/file.txt 

Safety nets and noclobber

If noclobber is set, bash will attempt to prevent you from accidentally overwriting files and potentially loosing data, in that case you will get an error message like this in the console:

-bash: test.php: cannot overwrite existing file.

noclobber is a safety feature that helps to prevent you from doing something potentially catastrophic. To get around that, you may use the following syntax instead:

Similarly you can create aliases for commands like mv and cp to run them interactively -i – this is also known as «prompt before overwrite»:

set -o noclobber alias copy='cp --interactive' alias move='mv --interactive' 

If you then try to copy or move a file to a directory that already has a file with the same name, an error message like this will be shown:

If you want this extra safety net, just include it in your ~/.bashrc or ~/.zshrc file.

Truncate vs echo

Truncating a file is going to be better when someone might be working on the file, as they will not have to reopen the file, and can just continue to use their file pointer.

truncate -s 0 /path/to/file.txt 

Источник

How to move (and overwrite) all files from one directory to another?

I know of the mv command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?

I know this is old question, but what about «rsync -az src/ dest/» it will copy all files which do not exist in dest/ directory from src/, and then just remove destination directory with «rm -rf dest/»

Читайте также:  Копирование в архив linux

6 Answers 6

-f, --force do not prompt before overwriting 

mv -f /a/b /c/d moves b inside d (so I have /c/d/a ) instead of overwriting d with a (I want /c/a ). How can I do such overwrite?

@Xenos you would need to delete the file d before you can create a directory d . That would require a couple of commands, there’s no force overwrite of a directory with a file that I know of. You could script that with just a few lines, but it would be better taken up as a separate question since it deviates from question the OPasked.

It’s just mv srcdir/* targetdir/ .

If there are too many files in srcdir you might want to try something like the following approach:

cd srcdir find -exec mv <> targetdir/ + 

In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.

just a note: on directories with a couple of thousands files, you wont be able to use the * since the argument list will be too long for the shell to handle, so you would need to use «find» instead or similar solution

I’ve tried the following command «find . -exec mv <> ../clean-copy/Ressources/img/ +» but got the following error : «find: -exec: no terminating «;» or «+»» — Any idea why ?

@musiKk Thanks for the tip. Although my comment is a question, it’s here because I feel the answer is 99% helping but missing the 1% that would make it perfect.

It’s also possible by using rsync , for example:

rsync -va --delete-after src/ dst/ 
  • -v , —verbose : increase verbosity
  • -a , —archive : archive mode; equals -rlptgoD (no -H,-A,-X )
  • —delete-after : delete files on the receiving side be done after the transfer has completed

If you’ve root privileges, prefix with sudo to override potential permission issues.

yup, just lost all my files. Shame on me for thinking people online know what they are talking about.

For moving and overwriting files, it doesn’t look like there is the -R option (when in doubt check your options by typing [your_cmd] —help . Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.

When you type in mv —help it returns the description of all options.

For mv, the syntax is mv [option] [file_source] [file_destination]

To move simple files: mv image.jpg folder/image.jpg

To move as folder into destination mv folder home/folder

To move all files in source to destination mv folder/* home/folder/

Use -v if you want to see what is being done: mv -v

Use -i to prompt before overwriting: mv -i

Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn’t exist yet: mv -u

Tie options together like mv -viu , etc.

Источник

Bash Overwrite File

Bash Overwrite File

In this bash article, we will learn how to overwrite a file in Linux. To do that, we will learn different methods and Linux commands to overwrite a file in bash using Linux operating system.

Читайте также:  Astra linux crypto pro csp

Before we start, we must understand what overwriting a file means in Linux.

Different Ways to Overwrite a File in Linux

Overwriting refers to the act of totally replacing one implementation with another. To overwrite anything is to substitute it with something else, obliterating the original.

Using the Linux system, we often need to overwrite and delete file contents. So, let’s learn various approaches to that.

Use the > Symbol to Overwrite a File

Remember that > and >> are used for two different operations. The single greater than the > operator empties and overwrites the specified file, whereas the >> operator adds lines to the end of the provided file.

So, we will be using > for overwriting our file. In the example below, we have used echo with the > operator to overwrite the existing file with abid .

echo "abid" > 'Users/Name/Desktop/Namefile.txt' 

Use Force cp Command to Overwrite a File Without Confirmation

One of the often used commands on Linux and other Linux operating systems for copying files and directories is the cp command, which stands for a copy.

Here, we will see how to use Linux’s cp command to force an unconfirmed overwrite of a copy operation. When we use the cp command, it usually overwrites the target file(s) or directory, as illustrated.

Below is the example of the cp command typically overwriting the targeted directories and files.

Use the -i Flag to Overwrite a File to Add Interactive Prompt

We can use the -i option and click y if we want to overwrite and add an interactive prompt. Examine the example below:

This line of code brings an interactive prompt while overwriting the file:

We can also overwrite the file without an interactive prompt. See the example below:

Use the chmod Command to Overwrite a Read-Only File

We can overwrite any file in two situations: when you have administrative access to the document’s properties or when you don’t. Take into account the following fixes for the issue.

In Linux, we can use the chmod command to change a file’s properties, and it has the following short command:

$ chmod [refrence] [operator] [mode] file.txt 

Use the shred Command to Overwrite a File

The shred command is used to erase data and devices securely.

This command overwrites a file to hide its contents and, optionally, deletes it, making it impossible for any program in the Linux/Unix system to retrieve the file.

We use the rm command in the terminal to delete files from the system. Files removed with the rm command may be recovered using the software.

However, files removed using the shred command are unrecoverable since the shred command overwrites the files three times with various patterns.

In the Linux/Unix system, by using the terminal, as demonstrated below, we can use the shred command to overwrite the file’s entries and declare them unrecoverable.

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

Related Article — Bash File

Источник

Оцените статью
Adblock
detector