Copying directory linux command

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.

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
Читайте также:  Чем разархивировать rar linux

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.

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

Источник

How to Copy a Directory in Linux Command Line [Beginner’s Tip]

This quick tip for beginners demonstrates how to copy a directory in Linux using the cp command. You’ll also learn a few additional tips around copying directory.

If you are new to Linux command line, you probably have this question in mind: How do I copy a directory and the files inside it to another directory in Linux command line?

Here’s how to copy a directory in Linux:

cp -r source_directory destination_directory

You probably already know that you can use cp command to copy files in Linux. Do you know that you can use the same cp command to copy a folder in Linux command line?

Hah! You already tried that and perhaps got this error:

cp: -r not specified; omitting directory 'test_dir'

Let me show you a thing or two about copying directory in Linux.

Copy directory in Linux command line

You can definitely use the same cp command but with the recursive option -r to copy a folder with its content to another folder. The above mentioned error also hints that you missed the -r option.

Читайте также:  Auto run file in linux

All you got to do is to use the command in this fashion:

cp -r source_directory destination_directory

And now if you use ls command on the destination directory, it should have the entire source directory inside it.

The -r option allows the recursive option. This means the entire content of the directory including its own subdirectories, everything in the directory will be copied to the destination.

A few things to note about copying directory in Linux

Here are a couple of things to note and tips about copying folders.

The destination directory doesn’t exist? It will be created

If the destination directory doesn’t exist (but the path exists), it will be created up to one level (explained in the next section). However, it will now copy the contents of the source directory, not the source directory itself.

For example, if you do this:

cp -r source_directory non_existing_directory

The non_existing_directory will be created with the content of the source_directory but it won’t have the source_directory inside it. Just the files of source_directory will be copied. It would be like non_existing_directory will be a replica of source_directory.

[email protected]:~$ ls test_dir [email protected]:~$ cp -r test_dir new_dir [email protected]:~$ tree . ├── new_dir │ ├── c.xyz │ ├── myzip1.zip │ └── myzip2.zip └── test_dir ├── c.xyz ├── myzip1.zip └── myzip2.zip 2 directories, 6 files

Can’t create nested directory while copying

You cannot use the above command to create nested directory structure.

For example, if you try to use cp -r source_dir dir1/dir2/dir3 but dir2 and dir3 don’t exist, it won’t create the nested directory structure and the command fails.

Preserve the original file attributes

One last tip to keep things short. If you use the -a option along with the -r option, it will preserve the original file information such as file permissions, file timestamps etc. It will archive the directory to the new location instead of creating it afresh.

This much information should be enough for you to know how to copy a directory in Linux. If you have questions or suggestions, please feel free to leave a comment.

Источник

Copy Directory/Folder in Linux via Command Line

Copy Directory/Folder in Linux via Command Line

There are various commands in Linux operating systems to copy a folder. The cp command helps you to do so. To organize files on your server, you will need to be copying. With cp command, you can copy a directory and an entire subdirectory with its content and everything beneath it. cp and rsync are one of the most popular commands for copying files and directory.

In this tutorial, we will explain how to copy the folder in Linux operating system.

Basic Syntax of cp Command

cp command is used to copy files or directories in Linux. It creates an exact copy of a file on a disk with different name.

basic syntax of cp command is shown below:

cp [OPTION] Source Destination
cp [OPTION] Source-1 Source-2 Source-3 Destination

You should see all the options available with cp command by running the following command:

You should see the following screen:

cp-help

Copying Directories with cp Command

If you want to copy directory, including all its files and subdirectories, use -R or -r option with cp command.

For example, copy /etc directory to the /opt directory with the following command:

Читайте также:  Astra linux directory vs freeipa

The above command will create a destination directory and copy all files and subdirectories recursively to the /opt directory.

If the destination directory already exists and you want to copy only the files and subdirectories but not the target directories, run the cp command with -T option as shown below:

If you want to preserve the specified attributes such as, ownership, timestamps, context and links, run the cp command with -a option as shown below:

If you want to display output during the copying process, use -v option with cp command:

You should see the output as shown below:

cp-verbose

If you want to copy only the content of /etc directory to /opt, you will need to specify the star wildcard as shown below:

If you want to copy multiple directories like, /etc and /var to /opt directory run the following command:

Note : In order to copy directories, you must have read permissions on source directory and write permissions on the destination directory.

Copying Directories with rsync Command

rsync is an advanced file copying tool that allows you to copy directory across local the remote location.

The basic syntax of rsync command is shown below:

rsync [OPTION] SOURCE DESTINATION

You can see all the options available with rsync command as shown below:

You should see the following screen:

rsync-help

To copy /etc directory to /opt, run the following command:

In the above command -a option will copy a directory with all its permission and other information including recursive copy. If the destination directory exists rsync will overwrite it.

If you want to copy only the contents of the /etc directory then put a trailing slash / at the end of /etc:

You can use rsync with -v option to display the verbose output as shown below:

You should see the following screen:

rsync-verbose

If you want to display progress during the copying process, run the rsync with -P option as shown below:

You can also use -z option with rsync command to compress file data during the transfer:

If you want to exclude a specific directory from the source, run the following command:

rsync -a —exclude ‘directoryname’ /opt/

To excluse multiple directories, run the following command:

If you want to copy multiple directories to /opt, run the following command:

Conclusion

In the above tutorial, we learned how to copy a directory with cp and rsync command. For more information, you can visit the Rsync official documentation at Rsync Doc.

Comments & Discussion:

Thanks for the tips, we use Rsync for local to remote backups and this was helpful for using it to copy local/local!

Recent Posts

  • Forcepoint Next-Gen Firewall Review & Alternatives
  • 7 Best JMX Monitoring Tools
  • Best PostgreSQL Backup Tools
  • Best CSPM Tools
  • Best Cloud Workload Security Platforms
  • Best Automated Browser Testing Tools
  • Event Log Forwarding Guide
  • Best LogMeIn Alternatives
  • Citrix ShareFile Alternatives
  • SQL Server Security Basics
  • Cloud Security Posture Management Guide
  • Cloud Workload Security Guide
  • The Best JBoss Monitoring Tools
  • ITL Guide And Tools
  • 10 Best Enterprise Password Management Solutions

Online DNS Tools

WebServerTalk participates in many types affiliate marketing and lead generation programs, which means we may get paid commissions on editorially chosen products purchased through our links. This is not change the outcome of any reviews or product recommedations.

Источник

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