Overwrite all files linux

Write to file, but overwrite it if it exists

How do I make it so it creates the file if it doesn’t exist, but overwrites it if it already exists. Right now this script just appends.

8 Answers 8

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.

In Bash, if you have set noclobber a la set -o noclobber , then you use the syntax >|

This also works if the file doesn’t exist yet

Check if noclobber is set with: set -o | grep noclobber

For a more detailed explanation on this special type of operator, see this post

For a more exhaustive list of redirection operators, refer to this post

Despite NylonSmile ‘s answer, which is «sort of» correct.. I was unable to overwrite files, in this manner..

echo «i know about Pipes, girlfriend» > thatAnswer

to solve my issues.. I had to use. >! , á la..

Obviously, be careful with this.

If your environment doesn’t allow overwriting with > , use pipe | and tee instead as follows:

Note this will also print to the stdout. In case this is unwanted, you can redirect the output to /dev/null as follows:

Just noting that if you wish to redirect both stderr and stdout to a file while you have noclobber set (i.e. set -o noclobber ), you can use the code:

More information about this can be seen at https://stackoverflow.com/a/876242.

Also this answer’s @TuBui’s question on the answer @BrDaHa provided above at Aug 9 ’18 at 9:34.

To overwrite one file’s content to another file. use cat eg.

Now to Append foobar we can use a cat command as below

If you have output that can have errors, you may want to use an ampersand and a greater than, as follows:

my_task &> ‘Users/Name/Desktop/task_output.log’ this will redirect both stderr and stdout to the log file (instead of stdout only).

Not the answer you’re looking for? Browse other questions tagged bash unix or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to move and overwrite subdirectories (and files) to parent directory?

I have a ton of files and dirs in a subdirectory I want to move to the parent directory. There are already some files and dirs in the target directory which need to be overwritten. Files that are only present in the target should be left untouched. Can I force mv to do that? It ( mv * .. ) complains

Читайте также:  Как обновляется защитник windows

What am I missing?

7 Answers 7

You will have to copy them to the destination and then delete the source, using the commands cp -r * .. followed by rm -rf * .

I don’t think you can «merge» directories using mv .

rsync would probably be a better option here. It’s as simple as rsync -a subdir/ ./ .

My test tree in filename : contents format:

And then, to emulate mv , you probably want to remove the source directory:

If this is wrong, can you please provide a similar example (e.g. using my test tree from near the top of this answer) with the desired result?

rsync can delete the source after copying with the —remove-source-files parameter.

From the rsync man page:

You can do this with cp and rm , but without copying the massive amount of data you are (presumably) trying to avoid transferring. @mattdm alluded to this in his comment, and an answer for another question has a more complete discussion about various options.

Essentially, the -l option for the cp command creates hard links to files rather than copying their data to new files.

Here’s a script that moves files from under /path/to/source/root to the corresponding path under /path/to/destination/root .

  • If a directory exists in both the source and the destination, the contents are moved-and-merged recursively.
  • If a file or directory exists in the source but not in the destination, it is moved.
  • Any file or directory that already exists in the destination is left behind. (In particular merged directories are left behind in the source. This is not easy to fix.)

Источник

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?

6 Answers 6

From the man page:

It’s just mv srcdir/* targetdir/ .

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

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

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

  • -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.

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

Читайте также:  Где лежат приложения windows store

Tie options together like mv -viu , etc.

If you simply need to answer «y» to all the overwrite prompts, try this:

In linux shell, many commands accept multiple parameters and therefore could be used with wild cards. So, for example if you want to move all files from folder A to folder B, you write:

If you want to move all files with a certain «look» to it, you could do like this:

Which copies all files that are blablabla.txt to folder B

Star (*) can substitute any number of characters or letters while ? can substitute one. For example if you have many files in the shape file_number.ext and you want to move only the ones that have two digit numbers, you could use a command like this:

Or more complicated examples:

For files that look like fi _ .e

Unlike many commands in shell that require -R to (for example) copy or remove subfolders, mv does that itself.

Remember that mv overwrites without asking (unless the files being overwritten are read only or you don’t have permission) so make sure you don’t lose anything in the process.

For your future information, if you have subfolders that you want to copy, you could use the -R option, saying you want to do the command recursively. So it would look something like this:

By the way, all I said works with rm (remove, delete) and cp (copy) too and beware, because once you delete, there is no turning back! Avoid commands like rm * -R unless you are sure what you are doing.

Источник

How to move all files including hidden files into parent directory via *

Its must be a popular question but I could not find an answer.

How to move all files via * including hidden files as well to parent directory like this:

This will move all files to parent directory like expected but will not move hidden files. How to do that?

7 Answers 7

You can find a comprehensive set of solutions on this in UNIX & Linux’s answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.

You can use these two commands together:

Which expands to:

(example: echo a<.,>b expands to a.b ab )

Note this will show a couple of warnings:

Just ignore them: this happens because /path/subfolder/<.,>* also expands to /path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).

If you want to just copy, you can use a mere:

This will copy all files, both normal and hidden ones, since /path/subfolder/. expands to «everything from this directory» (Source: How to copy with cp to include hidden files and hidden directories and their contents?)

I think this is the most elegant, as it also does not try to move .. :

This will move all files to parent directory like expected but will not move hidden files. How to do that?

You could turn on dotglob :

In order to turn off dotglob , you’d need to say:

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (e.g. .. and . ) and subdirectories. Here’s one option:

Читайте также:  Windows 10 home рутор

There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:

Also, 2) includes the . and .. files and 3) misses files like ..foobar, . barfoo, etc.

You could use, mv /source/path/<.[!.]. >* /destination/path , which would include the files missed by 3), but it would still try to move subdirectories. Using the find command with the mv command as I describe above eliminates all these problems.

Alternative simpler solution is to use rsync utility:

Note: Above command will show what is going to be changed. To execute the actual changes, remove —dry-run .

The advantage is that the original folder ( subfolder ) would be removed as well as part of the command, and when using mv examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.

In addition rsync provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were ( -a ).

The used -u parameter would skip existing newer files, -r recurse into directories and -v would increase verbosity.

Источник

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.

4 Answers 4

this should probably solve the problem.

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

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

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:

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

fi : 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 ) 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.

Источник

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