- How do I copy folder with files to another folder in Unix/Linux? [closed]
- 3 Answers 3
- Linux – How To Copy a Folder [ Command Line Option ]
- How To Copy a Folder with cp Command
- Linux cp command examples
- More examples of cp command to copy folders on Linux
- Use Linux rsync Command to copy a folder
- Conclusion
- How can I copy the contents of a folder to another folder in a different directory using terminal?
- 8 Answers 8
- Simple example.
- Copy folder structure (without files) from one location to another
- 17 Answers 17
- How to use ‘cp’ command to exclude a specific directory?
- 19 Answers 19
- fish new!
- Bonus pro-tip
How do I copy folder with files to another folder in Unix/Linux? [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 8 years ago .
I am having some issues to copy a folder with files in that folder into another folder. Command cp -r doesn’t copy files in the folder.
3 Answers 3
The option you’re looking for is -R .
- If destination doesn’t exist, it will be created.
- -R means copy directories recursively . You can also use -r since it’s case-insensitive.
- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 ‘s / @Anton Krug ‘s comment):
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:
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:
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.
Источник
Linux – How To Copy a Folder [ Command Line Option ]
How To Copy a Folder with cp Command
The cp command is a Linux command for copying files and directories. The syntax is as follows:
Linux cp command examples
In this example copy /home/vivek/letters/ folder and all its files to /usb/backup/ directory:
cp -avr /home/vivek/letters /usb/backup
Where,
- -a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
- -v : Verbose output.
- -r : Copy directories recursively.
More examples of cp command to copy folders on Linux
Copy a folder called /tmp/conf/ to /tmp/backup/:
$ cp -avr /tmp/conf/ /tmp/backup/
Sample outputs:
Fig.01: cp command in action
Use Linux rsync Command to copy a folder
You can also use rsync command which is a fast and extraordinarily versatile file copying tool. It can make copies across the network. The syntax is as follows for the rsync command
To backup my home directory, which consists of large files and mail folders to /media/backup, enter:
$ rsync -avz /home/vivek /media/backup
I can copy a folder to remote machine called server1.cyberciti.biz as follows:
$ rsync -avz /home/vivek/ server1.cyberciti.biz:/home/backups/vivek/
Where,
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -a : Archive mode i.e. copy a folder with all its permission and other information including recursive copy.
- -v : Verbose mode.
- -z : With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted something that is useful over a slow connection.
Fig.02: rsync command in action
Conclusion
You just learned how to copy a folder on a Linux like operating system using the cp command and rsync command. In conclusion, use rsync for a network folder transfer and cp for a local disk transfer.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How can I copy the contents of a folder to another folder in a different directory using terminal?
I am trying to copy the contents of a folder to another folder in a different directory using terminal.
Would somebody be able to provide me an example of the command line syntax required to achieve this?
8 Answers 8
You can copy the content of a folder /source to another existing folder /dest with the command
The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.
The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.
An alternate is rsync :
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.
Lets say you have a folder called folder1 in your
, inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.
To copy all the contents of
/new_folder1 you would use
new_folder1 would then contain all the files and folders from folder1 .
cp is the command to copy using a terminal, -r makes it recursively (so, current directory + further directories inside current)
/folder1 is the origin folder,
/new_folder1 is the destination folder for the files/folders inside the origin.
sign). Maybe because the destination folder was in /opt, which resides in another file system. And thank you Portablejim to remember the hidden file thing!
/new_folder1/folder1 instead of copying the contents over.
Simple example.
Copy the directory dir_1 and its contents (files) into directory dir_2:
Copy only the contents (files) of dir_1 into directory dir_2:
_files_ is a placeholder for the actual files located in the directory.
Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.
cp is a Linux command for copying files and directories. The syntax is as follows:
In this example copy /home/vivek/letters folder and all its files to /usb/backup directory:
-a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
-v : Explain what is being done.
-r : Copy directories recursively. Example
Copy a folder called /tmp/conf to /tmp/backup:
I like this command
Some of the commonly used options in rsync command are listed below:
- -v, –verbose: Verbose output
- -q, –quiet: suppress message output
- -a, –archive: archive files and directory while synchronizing ( -an equal to following options -rlptgoD)
- -r, –recursive: sync files and directories recursively
- -b, –backup: take the backup during synchronization
- -u, –update: don’t copy the files from source to destination if destination files are newer
- -l, –links: copy symlinks as symlinks during the sync
- -n, –dry-run: perform a trial run without synchronization
- -e, –rsh=COMMAND: mention the remote shell to use in rsync
- -z, –compress: compress file data during the transfer
- -h, –human-readable: display the output numbers in a human-readable format
- –progress: show the sync progress during transfer
If there are two folders: (with write permission)
If you are inside the folder called PORTAL where you want to copy all content of another folder say DATA at the same level then you will do
vimal@vimal-D3H:/var/www/html/PORTAL$ cp -a ../DATA/. .
You have to notice 2 dots. Last dot says copy here in present folder
one following /DATA/. says that all the CONTENTS inside DATA folder to be copied, and not the DATA folder itself.
If you remove this trailing «.» from /DATA/
then whole DATA folder will be copied inside PORTAL(from where you are coping).
Источник
Copy folder structure (without files) from one location to another
I want to create a clone of the structure of our multi-terabyte file server. I know that cp —parents can move a file and it’s parent structure, but is there any way to copy the directory structure intact?
I want to copy to a linux system and our file server is CIFS mounted there.
17 Answers 17
You could do something like:
to create the list of directories, then
to create the directories on the destination.
Here is a simple solution using rsync:
- one line
- no problems with spaces
- preserve permissions
I dunno if you are looking for a solution on Linux. If so, you can try this:
This copy the directories and files attributes, but not the files data:
Then you can delete the files attributes if you are not interested in them:
Just replace SOURCE_DIR and DEST_DIR.
The following solution worked well for me in various environments:
This solves even the problem with whitespaces:
In the original/source dir:
then recreate it in the newly created dir:
1 line solution:
Substitute target_dir and source_dir with the appropriate values:
Tested on OSX+Ubuntu.
If you can get access from a Windows machine, you can use xcopy with /T and /E to copy just the folder structure (the /E includes empty folders)
This one uses rsync to recreate the directory structure but without the files. http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
Might actually be better 🙂
A python script from Sergiy Kolodyazhnyy posted on Copy only folders not files?:
or from the shell:
Another approach is use the tree which is pretty handy and navigating directory trees based on its strong options. There are options for directory only, exclude empty directories, exclude names with pattern, include only names with pattern, etc. Check out man tree
Advantage: you can edit or review the list, or if you do a lot of scripting and create a batch of empty directories frequently
Approach: create a list of directories using tree , use that list as an arguments input to mkdir
-dfi lists only directories, prints full path for each name, makes tree not print the indentation lines,
—noreport Omits printing of the file and directory report at the end of the tree listing, just to make the output file not contain any fluff
Then go to the destination where you want the empty directories and execute
Источник
How to use ‘cp’ command to exclude a specific directory?
I want to copy all files in a directory except some files in a specific sub-directory. I have noticed that cp command didn’t have the —exclude option. So, how can I achieve this?
19 Answers 19
rsync is fast and easy:
You can use —exclude multiples times.
Note that the dir thefoldertoexclude after —exclude option is relative to the sourcefolder , i.e., sourcefolder/thefoldertoexclude .
Also you can add -n for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n from command line.
Well, if exclusion of certain filename patterns had to be performed by every unix-ish file utility (like cp, mv, rm, tar, rsync, scp, . ), an immense duplication of effort would occur. Instead, such things can be done as part of globbing, i.e. by your shell.
man 1 bash , / extglob.
So you just put a pattern inside !() , and it negates the match. The pattern can be arbitrarily complex, starting from enumeration of individual paths (as Vanwaril shows in another answer): !(filename1|path2|etc3) , to regex-like things with stars and character classes. Refer to the manpage for details.
man 1 zshexpn , / filename generation.
You can do setopt KSH_GLOB and use bash-like patterns. Or,
y matches pattern x , but excludes pattern y . Once again, for full details refer to manpage.
fish new!
The fish shell has a much prettier answer to this:
Bonus pro-tip
Type cp * , hit Ctrl X * and just see what happens. it’s not harmful I promise
Why use rsync when you can do:
This assumes the target directory structure being the same as the source’s.
The easiest way I found, where you can copy all the files excluding files and folders just by adding their names in the parentheses:
It’s relative to the source directory.
This will exclude the directory source/.git from being copied.
Expanding on mvds’s comment, this works for me
and first try it with -n option to see what is going to be copied
rsync is actually quite tricky. have to do multiple tests to make it work.
Let’s say you want to copy /var/www/html to /var/www/dev but need to exclude /var/www/html/site/video/ directory maybe due to its size. The command would be:
rsync -av —exclude ‘sites/video’ /var/www/html/ /var/www/dev
- The last slash / in the source is needed, otherwise it will also copy the source directory rather than its content and becomes /var/www/dev/html/xxxx , which maybe is not what you want.
The the —exclude path is relative to the source directly. Even if you put full absolute path, it will not work.
-v is for verbose, -a is for archive mode which means you want recursion and want to preserve almost everything.
I assume you’re using bash or dash. Would this work?
Doing an ls excluding the files you don’t want, and using that as the first argument for cp
Edit: forgot to exclude the target path as well (otherwise it would recursively copy).
Another simpler option is to install and use rsync which has an —exclude-dir option, and can be used for both local and remote files.
Just move it temporally into a hidden directory (and rename it after, if wanted).
This is a modification of Linus Kleen’s answer. His answer didn’t work for me because there would be a . added in front of the file path which cp doesn’t like (the path would look like source/.destination/file).
This command worked for me:
find . -type f -not -path ‘*/exlude-path/*’ -exec cp —parents ‘<>‘ ‘/destination/’ \;
the —parents command preserves the directory structure.
I use a «do while» loop to read the output of the find command. In this example, I am matching (rather than excluding) certain patterns since there are a more limited number of pattern matches that I want than that I don’t want. You could reverse the logic with a -not in front of the -iname flags:
I use the above to copy all music type files that are newer on my server than the files on a Western Digital TV Live Hub that I have mounted at /media/wd. I use the above because I have a lot of DVD files, mpegs, etc. that I want to exclude AND because for some reason rsync looks like it is copying, but after I look at the wd device, the files are not there despite no errors during the rsync with this command:
The first part ls all the files but hidden specific files with flag -I . The output of ls is used as standard input for the second part. xargs build and execute command cp -rf -t destdir from standard input. the flag -r means copy directories recursively, -f means copy files forcibly which will overwrite the files in the destdir , -t specify the destination directory copy to.
10 years late. Credits to Linus Kleen.
I hate rsync ! 😉 So why not use find and cp ? And with this answer also mkdir to create a non-existent folder structure.
It looks like cd ìs necessary to concat relative paths with find .
mkdir -p will create all subfolders and will not complain when a folder already exists.
Housten we have the next problem. What happens when someone creates a new folder with a new file in the middle of it? Exactly: it will fail for these new files. (Solution: just run it again! :)) The solution to put everything into one find command seems difficult.
Источник