- Copy and overwrite a file in shell script
- 4 Answers 4
- Explaining this script a little bit
- Write to file, but overwrite it if it exists
- 8 Answers 8
- Not the answer you’re looking for? Browse other questions tagged bash unix or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- How to move (and overwrite) all files from one directory to another?
- 6 Answers 6
- How to move and overwrite subdirectories (and files) to parent directory?
- 7 Answers 7
- mv Command in Linux: 7 Essential Examples
- 7 practical examples of the mv command
- 1. How to move a file to different directory
- 2. How to move multiple files
- 3. How to rename a file
- 4. How to move a directory in Linux with mv command
- 5. How to rename a directory
- 6. How to deal with overwriting a file while moving
- 7. How to forcefully move the files
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.
Источник
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
Related
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) 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
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 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
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.)
Источник
mv Command in Linux: 7 Essential Examples
mv is one of the must known commands in Linux. mv stands for move and is essentially used for moving files or directories from one location to another.
The syntax is similar to the cp command in Linux however there is one fundamental difference between these two commands.
You can think of the cp command as a copy-paste operation. Whereas the mv command can be equated with the cut-paste operation.
Which means that when you use the mv command on a file or directory, the file or directory is moved to a new place and the source file/directory doesn’t exist anymore. That’s what a cut-paste operation, isn’t it?
cp command = copy and paste
mv command = cut and paste
mv command can also be used for renaming a file. Using mv command is fairly simple and if you learn a few options, it will become even better.
7 practical examples of the mv command
Let’s see some of the useful examples of the mv command.
1. How to move a file to different directory
The first and the simplest example is to move a file. To do that, you just have to specify the source file and the destination directory or file.
This command will move the source_file and put it in the target_directory.
2. How to move multiple files
If you want to move multiple files at once, just provide all the files to the move command followed by the destination directory.
You can also use glob to move multiple files matching a pattern.
For example in the above example, instead of providing all the files individually, you can also use the glob that matches all the files with the extension .txt and moves them to the target directory.
3. How to rename a file
One essential use of mv command is in renaming of files. If you use mv command and specify a file name in the destination, the source file will be renamed to the target_file.
In the above example, if the target_fille doesn’t exist in the target_directory, it will create the target_file.
However, if the target_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.
I’ll show you how to deal with overwriting of files with mv command later in this tutorial.
You are not obliged to provide a target directory. If you don’t specify the target directory, the file will be renamed and kept in the same directory.
Keep in mind: By default, mv command overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.
4. How to move a directory in Linux with mv command
You can use mv command to move directories as well. The command is the same as what we saw in moving files.
In the above example, if the target_directory exists, the entire source_directory will be moved inside the target_directory. Which means that the source_directory will become a sub-directory of the target_directory.
5. How to rename a directory
Renaming a directory is the same as moving a directory. The only difference is that the target directory must not already exist. Otherwise, the entire directory will be moved inside it as we saw in the previous directory.
6. How to deal with overwriting a file while moving
If you are moving a file and there is already a file with the same name, the contents of the existing file will be overwritten immediately.
This may not be ideal in all the situations. You have a few options to deal with the overwrite scenario.
To prevent overwriting existing files, you can use the -n option. This way, mv won’t overwrite existing file.
But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).
You can enter y for overwriting the existing file or n for not overwriting it.
There is also an option for making automatic backups. If you use -b option with the mv command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.
By default, the backup of the file ends with
. You can change it by using the -S option and specifying the suffix:
You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be moved to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.
- -i : Confirm before overwriting
- -n : No overwriting
- -b : Overwriting with backup
- -u : Overwrite if the target file is old or doesn’t exist
7. How to forcefully move the files
If the target file is write protected, you’ll be asked to confirm before overwriting the target file.
To avoid this prompt and overwrite the file straightaway, you can use the force option -f.
If you do not know what’s write protection, please read about file permissions in Linux.
You can further learn about mv command by browsing its man page. However, you are more likely to use only these mv commands examples I showed here. FYI, you may also use rename command for renaming multiple files at once.
I hope you like this article. If you have questions or suggestions, please feel free to ask in the comment section below.
Источник