Copy files from one directory into an existing directory
How do I do this? I tried cp -r t1 t2 (both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don’t want that, I need the files in t1 to go directly inside t2. How do I do this?
Why was this closed? It is arbitrary if a bash command is a built-in or external command (e.g. printf exists as both on most systems), so cp questions can well be seen as bash questions, which is a programming language. I have never seen a Python question talking about file copy be closed.
I arrived in search of a reminder about the syntax of the Bash shell copy command, and I am happy to report that these commands seem also to work against the underlying NTFS filesystem on my Windows installation.
10 Answers 10
The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.
@CiroSantilli六四事件法轮功包卓轩 If you copy a directory, cp will create a directory and copy all the files into it. If you use the pretend folder called «.», which is the same as the directory holding it, the copy behaves this way. Let’s say t1 contains a file called «file». cp will perform the operation equivalent to «cp t1/./file t2/./». It is copying the folder «.», but copying files into t2’s «.» folder strips the «./» because «t2/./» is the same as «t2/». Technically, this means it’s POSIX built in behavior. but probably not in the way you might have been expecting!
once I tested using the source path trailing a dot (t1/.) it copied the entire t1 folder with its content into the t2 folder. So I got a t1 folder inside the t2 folder. But once I used * instead of dot it did work and copied only the content of t1 into t2. So I think the best answer to this question is the following script -> cp -R t1/* t2/
Or if you have directories inside dir1 that you’d want to copy as well
Depending on how your shell is configured, you’ll probably need to use «dir1/* dir1/.*» instead of «dir1/*» if you want to also catch hidden files.
Copying dir1/.* is not a good idea, as it copies dir1/.. (i.e. the parent of the directory you’re actually trying to copy). It also copies dir1/. which is fine, except that it’s already (mostly) been copied, so you’re doing the work twice.
You can get around the dir1/.* /hidden files problem by cd-ing into the directory you want to copy from, and then referring to it as . . So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir] , cp . [path to destination dir, with no trailing slash] .
If you want to copy something from one directory into the current directory, do this:
This assumes you’re not trying to copy hidden files.
Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:
Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:
Or you can navigate to the folder with files (t1) and do this:
Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).
The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand’s answer has (copying t1/* misses invisible files, copying `t1/* t1/.*’ copies t1/. and t1/. which you don’t want).
Your solution does not work, at least not on my installation (ubuntu 12.10) $ mkdir t1 $ mkdir t2 $ touch t1/one $ touch t1/two $ touch t1/.three $ cp -R t1/ t2 $ ls t2/ t1 (sorry no codeformat in comments, readable version at pastebin.com/yszSxV6G)
For inside some directory, this will be use full as it copy all contents from «folder1» to new directory «folder2» inside some directory.
$(pwd) will get path for current directory.
Notice the dot (.) after folder1 to get all contents inside folder1
cp -r $(pwd)/folder1/. $(pwd)/folder2
Nov, 2021 Update:
This code with Flag «-R» copies perfectly all the contents of «folder1» to existing «folder2»:
Flag «-R» copies symbolic links as well but Flag «-r» skips symbolic links so Flag «-R» is better than Flag «-r».
-R, --dereference-recursive For each directory operand, read and process all files in that directory, recursively, following all symbolic links.
-r, --recursive For each directory operand, read and process all files in that directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. Note that if no file operand is given, grep searches the working directory. This is the same as the ‘--directories=recurse’ option.
bash script for copying files between directories
Thing is that those directories exsist, I do have right to access them. Any help would be nice. Please and thank you.
3 Answers 3
Variable names on the left side of an assignment should be bare.
Here are some more improvements to your script:
#!/bin/bash #This script copies NZB files from Downloads folder to HellaNZB queue folder. down="/home/myusuf3/Downloads/" queue="/home/myusuf3/.hellanzb/nzb/daemon.queue/" find "$" -name "*.nzb" | while read -r file do mv "$" "$" done
Using while instead of for and quoting variables that contain filenames protects against filenames that contain spaces from being interpreted as more than one filename. Removing the rm keeps it from repeatedly producing errors and failing to copy any but the first file. The file glob for -name needs to be quoted. Habitually using lowercase variable names reduces the chances of name collisions with shell variables.
If all your files are in one directory (and not in multiple subdirectories) your whole script could be reduced to the following, by the way:
mv /home/myusuf3/Downloads/*.nzb /home/myusuf3/.hellanzb/nzb/daemon.queue/
If you do have files in multiple subdirectories:
find /home/myusuf3/Downloads/ -name "*.nzb" -exec mv <> /home/myusuf3/.hellanzb/nzb/daemon.queue/ +
As you can see, there’s no need for a loop.
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
Linux bash script to copy files
I need script to copy on cron basis a list of files. Files selected on name/datetime pattern and to name of file destination must by appended data like ddmmyyy. It is not problem copy files or directory, but problem to change name of each file according to its data. May be exists some open source solution? Thanks.
1 Answer 1
You haven’t provided enough information for me to give you real working code; but you can do something like this:
file=dated_log.log ddmmyyyy=$(read -r < "$file" ; echo "$") cp "$file" "$file.$ddmmyyyy"
The above will copy dated_log.log to data_log.log.30102011 , assuming that the first line of dated_log.log starts with 30102011 .
The Bash Reference Manual will hopefully help you adjust the above to suit your needs.
@gniourf_gniourf: Yes, good point. But I think my answer is better the way it is, because I rather doubt that the OP's file format actually starts with the exact eight right characters, and I think the code that I posted is likely to be more easily tweaked into whatever form the OP needs. (Suppose that the OP actually needs characters 30 through 37. How would you adjust read -r -n 8 ddmmyyy to retrieve them?)
for characters 30 through 37: read -r -n 37 ddmmyyyy < "$file"; ddmmyyyy=$