Linux copy recursive all

Linux Copy all the files including subdirectories from DVD / Floppy / CD / Other directory (recursive copy)

Q . How do I copy all the file including subdirectories from DVD disk mounted at /mnt/dvd to /home/tom at a shell prompt under CentOS Linux?

A . You need to use cp command to copy SOURCE to DESTINATION or multiple SOURCE(s) to a DIRECTORY. Gnu cp command has -a option which copy directories recursively while preserving the file attributes such as mode,ownership,timestamps and security contexts, if possible additional attributes such as links et all.

cp command syntax

For example copy all files including subdirectories from /mnt/dvd/data to /home/tom/data, enter:
$ cp -a /mnt/dvd/data/* /home/tom/data
OR
$ cp -av /mnt/dvd/data/* /home/tom/data

  • 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

Recursively copy entire directories

Many other command (including cp) support -r or -R option for copying file recursively. For example scp secure network copy command:
$ scp -r *.cpp remote_user@server.nixcraft.in:

OR use cp command locally (instead of -a option):
$ cp -r *.cpp /path/to/dest

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Recursively copy files from one directory to another

I have all my music in a folder /media/kalenpw/MyBook/Music/ABunchOfOtherFoldersWithFilesInside . I want to copy all of the mp3s to /media/kalenpw/HDD/Music so I used:

however this only copied the mp3s in the root music folder and did not open any of the artist subdirectories and copy those files.

I was under the impression -R would recursively copy all the files. How can I achieve said goal?

5 Answers 5

The reason for your command not working is that names containing wildcards ( *.mp3 ) are expanded before the command is run, so if you had three files ( 01.mp3 , 02.mp3 , 03.mp3 ) your effective command was:

As you can see -R has no effect in this case.

Читайте также:  Что такое windows metafile

You have specifically mentioned the files(s)/directory(ies) to be copied as using *.mp3 i.e. any file/directory name ending in .mp3 .

So any file ending in .mp3 in /media/kalenpw/MyBook/Music/ directory and similarly, any directory ending in .mp3 in /media/kalenpw/MyBook/Music/ will be copied, recursively. If there is no such matched file/directory, nothing will be copied.

Now to copy all .mp3 files from /media/kalenpw/MyBook/Music/ recursivley to directory /media/kalenpw/HDD/Music/ :

Suppose that you have a bunch of .docx , .mp3 , .txt and .xlsx files stored in this directory structure:

. and you want to recurse into all such directories in order to copy all found .mp3 files to /home/me/music/ but you do not want to preserve such directory tree in the destination (i.e. you want all found .mp3 files to be copied to /home/me/music/ instead of copied to respective directories such as /home/me/music/dir1/ , /home/me/music/dir1/dir11/ et cetera).

In such case, at the shell terminal (bash) first run this command in order to access the root of your file search:

. and then run this command:

In case you do want to preserve the source’s directory tree in the destination, run this command instead (after running cd /files ):

Источник

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.

Источник

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.

Читайте также:  Windows installer скопировать файл

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:

  1. After the initial sync, it will then copy only the files that have changed.
  2. 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).

Источник

How To Copy Entire Directory In Linux?

Linux provides different tools to copy directories entirely. The most popular and well-known tool is cp . In this tutorial, we will look at how to copy Linux directories entirely or specific content.

Copy Only Directory

In this first example, we will copy the only directory. We can just provide the source directory and destination directory names to the cp command. In this example, we will try to copy a single directory. As we can see we get an error like below.

Copy Entire Directory Recursively

Now here is the real scenario. We will copy our directory named backup with a new named newbackup . We will use the -r option which makes copy operation recursively. Recursive means subdirectories and files will be copied too.

Copy Special Files

During recursive copy operation, special file content will not be copied by default. We can copy special file contents with the -b option like below.

Copy While Preserving Source Directory Attributes

Files and directories provide some attributes in Linux operating systems. These attributes are file mode, ownership, timestamp, context, links, xattr, etc. During directory copy, we can copy these attributes too with the -a option like below.

Copy Entire Directory To The Current User Home Directory

Linux bash shell provides some shortcuts about the paths. If we want to copy a directory completely into the current user home directory we can

tilde to specify the user home directory path. In this example, we will copy the directory named backup to the current user home directory.

Copy Verbosely Providig Detailed Information

During copy operation, there will be no sign about the current operation. We can make this copy operation verbose where copied files will be printed to the shell. We will use -v command like below.

Copy Entire Directory with rsync Command

rsync is very popular and useful tools used to copy, sync, and backup files and directories. We can use rsync in order to copy the complete directory with its subdirectories locally or to the remote systems. We will use -r options to copy all directories and files recursively and also use -v option for verbose output.

Источник

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