Linux copy folders and subfolders

how do you copy a directory and its contents to a new location under a new directory name?

I’m new to the Linux command line and am trying to get to grips with the copy command at the moment. Can anyone please tell me if it’s possible to copy a directory with its subdirectories and associated files to a new directory with a new name, e.g. directory_backup ? Thanks for any and all help in advance.

7 Answers 7

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 . 

Sorry Zanna. I’ll put your advice into practice and see how I go. Like the idea of the verbose flag . assume this will also work as a report while other command are being worked.. Cheers.

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.

Читайте также:  Kyocera fs 1060dn драйвер linux

Not used to this comment section Byte Commander. keep hitting return to change line but post instead. Very clear outline in your answer . Look forward to sorting out those directories now that I’ve been given good instruction on the correct usage of the available tools. Excellent.

@OwenHines Just that comments strip any linebreak and additional whitespace and having them in the editor doesn’t change anything in the result.

You can use the below command to copy directory from one place to another sub directory.

cp -Rvp /home/edmuntu/data /home/edmuntu/mydata/ 
  • -R = copy directories recursively
  • v = explain what is being done
  • p = It will help your to preserve your permission and ownership with timestamps

Thank you @Rakesh Chauhan.»preserve your permission and ownership with timestamps» . Wow. I need to get to it. Yet another aspect of line commands to be explored. Thank you for your reply.

As an alternative to the generally useful cp command you can use rsync which has the added benefit of replicating the permissions/timestamps of the original directory structure:

note the slash after the source dir.

Had huge issues with rsync. cp -Rvp with a . after the directory to copy is working well for me (the . will copy hidden files/foders). And @HelloUniverse — if you use the p command with cp, it preserves permissions, ownership, timestamps

cp is the command that does the function that you’re looking for. It stands for copy and it does just that. The command that would work for you in this situation is

This would copy the source/ folder and all of its sub-directories to destination/ .

If destination/ doesn’t exist, it will be created.

The -R flag stands for recursive, which tells cp to copy all sub-directories.

@Edmuntu Glad I could help, if you found the solution to your issue, can you please mark the answer that worked for you as the correct one by clicking the check mark under it? This will help alert others who view the page of the question being solved.

I find it easier to change to the directory I’m copying from first. For this example change to a directory everyone has called /boot . Anyone can copy and paste the commands below into their Terminal.

cd /boot sudo mkdir /boot_backup sudo cp -r . /boot_backup du /boot_backup -h 752K /boot_backup/extlinux/themes/debian-wheezy 756K /boot_backup/extlinux/themes 832K /boot_backup/extlinux 2.5M /boot_backup/grub/i386-pc 20K /boot_backup/grub/locale 2.3M /boot_backup/grub/fonts 7.2M /boot_backup/grub 565M /boot_backup 

For the cp command the current directory is identified as . which is the /boot directory we changed to. The -r option makes it recursive to include all sub-directories.

To ensure it worked run du to list all sub-directories and total file sizes in the new directory /boot_backup in this case.

Читайте также:  How to mount usb drive linux

After finishing this walk-through, use: sudo rm -r /boot_backup to remove the new directory and it’s sub-directories.

Источник

How to Copy Files and Directories in Linux

This guide will show you how to copy files and directories in Linux by executing commands from the command line. Furthermore, the commands listed below detail how to create system-wide backups or filter out and copy only specific files.

how to copy files and directories in linux

Note: These Linux commands can only be run from a terminal window. If your version of Linux boots to a desktop graphical interface, launch a terminal window by pressing CTRL-ALT-F2 or CTRL-ALT-T.

Using the cp Command to Copy Files and Directories in Linux

The cp command is the primary method for copying files and directories in Linux. Virtually all Linux distributions can use cp . The basic format of the command is:

cp [additional_option] source_file target_file
cp my_file.txt my_file2.txt

This Linux command creates a copy of the my_file.txt file and renames the new file to my_file2.txt.

By default, the cp command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory. You’ll need to change the name of the target file to copy in the same location. Some users will add _old, some will add a number, and some will even change the three-letter extension (e.g., .bak instead of .txt).

You may not get a warning before Linux overwrites your file – be careful, or see below for the –i option.

Additional Options

Additional options can be used in combination with the cp command:

  • –v verbose: shows the progress of multiple copied files
  • –ppreserve: keeps the same attributes, like creation date and file permissions
  • –f force: force the copy by deleting an existing file first
  • –i interactive: prompts for confirmation, highly advised
  • –Rrecursive: copies all files and subfolders in a directory
  • –u update: copy only if source is newer than destination

Note: The -p (preserve) option forces the system to preserve the following source file attributes: modification time, access time, user ID (UID), group ID (GID), file flags, file mode, access control lists (ACLs), and extended attributes (EAs).

How to Copy File to Another Directory in Linux

To copy a file from the directory you’re working in to a different location, use the command:

cp my_file.txt /new_directory

You don’t need to rename the file unless there’s already one with the same name in the target directory.

To specify a path for the source file:

cp /etc/my_file.txt /new_directory

This lets you copy without having to change directories. The cp command will create the /new_directory if it doesn’t exist.

Читайте также:  Canon mf4410 driver linux

To rename and copy a file to a different path:

cp my_file.txt /new_directory/my_file2.txt

Copying a file in Linux with cp.

This option is useful for creating backups of configuration files, or for copying data to a storage device.

Note: Learn how to move directories in Linux.

Copy Multiple Files from One Directory to Another in Linux

You may need to copy more than one file at a time.

List each file to be copied before the target directory:

cp my_file.txt my_file2.txt my_file3.txt /new_directory

This example created a copy of all three files in the /new_directory folder.

Use a wildcard to specify all files that share a string of characters:

cp /pictures/*.jpg /new_directory

This would find all the files with the .jpg extension in the /pictures directory, and copy them into the /new_directory folder.

To copy an entire folder and its subfolders and files, use the –R option:

cp –R /documents /new_directory

Copying and renaming a folder in Linux.

–R stands for recursive, which means “everything in that location.” This would copy all the files, as well as all the directories, to the /new_directory folder.

Copy Using rsync Command

The rsync command in Linux is used to synchronize or transfer data between two locations. Usage is similar to cp , but there are a few key differences to note.

To copy a single file, enter the following into a terminal:

rsync –a my_file.txt /new_directory/my_file_backup.txt
  • The –a option means all, and is included with rsync commands – this preserves subdirectories, symbolic links, and other metadata.
  • Replace the my_file.txt file in the working directory.
  • Replace /new_directory/ with the destination.
  • Using my_file_backup.txt as the target indicates the file will be renamed during the copy.

To copy a directory with rsync, enter the following:

rsync –a /etc/docker/ /home/backup/docker/

This copies the contents of the /etc/docker/ directory to /home/backup/docker/. Make sure to keep the slashes. Omitting the slash on the source directory will copy the contents into a subdirectory.

To omit files from being copied, check out our guide on how to exclude files and directories in data transfer using rsync command.

Other Options

The ls command is a handy partner to the cp command in Linux.

To list the contents of a directory enter the command:

The example above displays all the files in /directory. Use this command after copying to verify the files were copied successfully.

To change directories, use cd and the name of the directory. For example:

The command prompt will change to display that you’ve changed directories.

Now you understand how to copy files in Linux. The cp command is a versatile and powerful tool for managing and backing up files.

Источник

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