The option you're looking for is -R.
cp -R path_to_source path_to_destination/
- If
destinationdoesn't exist, it will be created. -Rmeanscopy directories recursively. You can also use-rsince it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-aflag along with trailing/.in the source (as per@muni764's /@Anton Krug's comment):
cp -a path_to_source/. path_to_destination/
Answer from Pierre Salagnac on Stack OverflowThe option you're looking for is -R.
cp -R path_to_source path_to_destination/
- If
destinationdoesn't exist, it will be created. -Rmeanscopy directories recursively. You can also use-rsince it's case-insensitive.- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use
-aflag along with trailing/.in the source (as per@muni764's /@Anton Krug's comment):
cp -a path_to_source/. path_to_destination/
You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you're copying is called dir1 and you want to copy it to your /home/Pictures folder:
cp -r dir1/ ~/Pictures/
Linux is case-sensitive and also needs the / after each directory to know that it isn't a file. ~ is a special character in the terminal that automatically evaluates to the current user's home directory. If you need to know what directory you are in, use the command pwd.
When you don't know how to use a Linux command, there is a manual page that you can refer to by typing:
man [insert command here]
at a terminal prompt.
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you've started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R, -r, and --recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r. It's important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).
You can copy the contents of a folder /source to another existing folder /dest with the command:
cp -a /source/. /dest/
The -a option is an improved recursive option, that preserves all file attributes and symlinks.
The . at end of the source path is a specific cp syntax that copies all files and folders, including hidden ones.
An alternate is rsync:
rsync -a source/ destination
The advantages of rsync are:
- After the initial sync, it will then copy only the files that have changed.
- You can use it over a network, convenient for files in $HOME, especially config files.
Copy folder and files inside directory to another directory, which are modified within the last 30 minutes
cp - How can I copy a directory and rename it in the same command? - Unix & Linux Stack Exchange
copying all file
How do I copy an entire directory and include only 1 type of file extension?
Videos
cp -r /path/to/someDirA/folderTwo /path/to/someDirB/
The -r option to cp tells it to recurse on directories, copying its contents.
You can use cp to copy files and directories:
cp -r /path/to/someDirA/folderTwo /path/to/someDirB
The -r option is needed when copying directories.
Alternatively, you can use rsync:
rsync -a /path/to/someDirA/folderTwo /path/to/someDirB
You can use cp with the -r (copy recursively) flag and specify the new directory name:
cp -r /path/to/directory /path/to/location/new-name
In the above command replace the given paths with the real ones.
For example, to copy stuff from my home directory to an existing directory named backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it), you run:
cp -r ~/stuff ~/backup/stuff-backup
~ is a shortcut for your home directory /home/$USER or /home/zanna in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt
zanna@monster:~$
^--the ~ here is where I am - home!
You can add the -v (verbose) flag to make cp report each copy being performed:
$ cp -vr stuff backup/stuff-backup
'stuff/thing1' -> 'backup/stuff-backup/thing1'
'stuff/thing2' -> 'backup/stuff-backup/thing2
...
The command you need is simply cp which stands for "copy".
You can use it for example with one of these syntaxes:
cp SOURCEFILE TARGETFILE
cp SOURCEFILE TARGETDIRECTORY
The first variant allows you to specify a new file name for the target file, while the second variant creates a copy with the same name in the target directory. You must of course substitute the place holders in capital letters with valid paths first.
However, cp by default operates on files only, not on directories. To get it to copy a complete directory with all its content recursively, you must add the -r option:
cp -r SOURCEDIRECTORY TARGETDIRECTORY
You can learn more about the cp command by typing man cp in your terminal.
Hi guys,
I have two folders : /home/user/abc & /home/user/xyz
I run a script that generates a folder with files inside abc, so end result would be /home/user/abc/generatedFolder1/ with a bunch of files in it.
Is there a command I could use to copy this generatedFolder1 and the files in it to xyz, so the folder structure would remain the same? I did some googling around but most commands just would either omit the directory or copy the files too, to the xyz directory directly.
Stuff like :
find /home/user/abc/ -type d -mmin -30 -exec cp -pf {} /home/user/xyz/ \;
did not work, idk why. Can someone help me, Im a little new to the find command and am finding it a little confusing. I tried the above command with -r after the cp, and it just creates the abc folder recursively without stopping?
You should be able to do just
cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21
However, if the target directory already exists, this would append the final part of the source path to the destination path, creating
/tf/Custom_App_backups/Custom_App_2017-12-21/Custom_App, and then copy the rest of the tree within that.
To prevent this, use /tf/Custom_App/. as the source. Of course, in that case you might want to rm -r /tf/Custom_App_backups/Custom_App_2017-12-21 first, if you don't want older files lying around there after the copy.
The difference between /some/dir and /some/dir/. was discussed a while back in cp behaves weirdly when . (dot) or .. (dot dot) are the source directory
Alternatively, you can do it like so:
mkdir /tf/Custom_App_backups/Custom_App_2017-12-21 # prepare the target location
cp -R /tf/Custom_app/. /tf/Custom_App_backups/Custom_App_2017-12-21 # copy only the contents
This will allow you to specify your custom location beforehand. Also, notice that it uses the suffix /. This allows you to only copy the contents and exclude its containing folder -- in this case it is the Custom_app folder.