Copying folder and files in linux

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 to Copy files and folders in Linux

Being a Linux user, copying files and directories is one of the most common tasks.

Читайте также:  Shut down programs windows

Linux users don’t spend a day without using the cp (copy) command according to my personal experience. cp command is used to copy a single file or group of files or directory.

To perform the copy operation, you must have at least read permission in the source file and write permission in the target directory.

In this article, we will explain how to use the cp command.

To perform remote file copy, use the commands such as rsync command or scp command or pscp command.

What is cp command?

‘cp’ command is one of the basic and most widely used Linux commands for copying files and directories from one location to another.

When copying files from source to destination, the source file remains the same and the target file may have the same or different name.

Common syntax for cp command:

Common options for cp command:

Options Description
-v Verbose mode (Show progress)
-r/R Copy directories recursively
-n Don’t overwrite an existing file
-d Copy a link file
-i Prompt before overwrite
-p Preserve the specified attributes
-b Make a backup of each existing destination file

To demonstrate file copy operation in detail, we will be performing a copy operation between user1 and user2 .

1) Copying files with cp command

Use the following command to copy a file from one location to another.

In the following example, we will copy the source file “/home/user1/cp-demo.txt” to the target directory “/home/user2” with the same name:

You can check whether the specific file is copied to the target directory using the ls command as shown below:

2) Copying files with a different name

Use the following command to copy a file from one place to another with a different name. You need to give a new name to the target file.

In the following example, we will copy a file named “/home/user1/cp-demo.txt” to the target directory with a new name “/home/user2/cp-demo11.txt” :

Use the ls command to verify this:

3) Copying multiple files in Linux

The following cp command copies multiple files from one location to another.

In this example, we will copy the following three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt” to the target.

No additional option is required to copy multiple files, and the file name(s) must be separated with a space as shown below:

Use the ls command to verify the copied files. You should see them in the target directory as shown below:

4) How to copy directories recursively

To copy a directory recursively from one location to another, use the -r/R option with the cp command. It copies everything, including all its files and subdirectories.

In the following example, we will copy the ‘/home/user1/cp-demo-folder’ directory to ‘/home/user2/’ and the target directory name will remain the same:

Use the ls command to verify the outcome. You should see them in the target directory as shown below:

5) Copying multiple directories recursively

Use the following command to copy multiple directories at once. In the following example, we will copy the public_html & public_ftp folders to the target directory named /home/2daygeek/cp-test.

Output can be verified using the ls command:

6) Copying specific format files in Linux

Sometimes you may have to copy files with a specific extension for a certain purpose. If so, you should use the cp command with the «wildcard (*)» option and the file extension name.

In the following example, we will copy all the files containing the *.sh extension from the source to the target directory. Similarly, you can copy any file extensions such as .jpg, .png, .txt, .php, and .html.

This can be verified using the ls command:

7) Copying all files at once in Linux

To copy all the files except the directory from one location to another, use the following cp command with wildcard (*) option.

cp command excludes directories by default, and the -r option must be added to copy them.

In this example, we will copy all the files from ‘/home/user1/’ to ‘/home/user2′ :

Verify the output using the ls command as shown below:

8) How to copy hidden files (‘.’ dot files) in Linux

By default, the cp command does not copy “dot (.)” or ‘hidden’ files in Linux.

Use the following command to copy all types of files, including link files (soft link or hard link), folders and hidden or dot files from source to destination.

This example is very useful to copy user’s home directory which has several hidden files:

You can verify this by using the ls command with the “-a” option:

9) Backup the file with cp command

cp command allows you to backup the file if the same file name exists in the target directory using the —backup option.

In this example, we will copy the /home/user1/passwd-up.sh file to the target directory /home/user2/.

It will backup the “passwd-up.sh” file in the target directory during the copy operation, if you say “yes” when you see the message below:

It adds the “Tilde (

)” symbol at the end of the old file, the same can be verified in the following output.

10) Copying files with attributes

By default, Linux replaces file attributes such as username, group name, date and time when copying files from a user, and does not carry the original file attributes.

To preserve the original attributes of the files, use the -p option with the cp command.

Upon querying we can see that the file “service-1.sh” has retained its original attributes as shown below:

11) How to avoid file overwriting with cp command?

When copying a file, use the -n option with the cp command to avoid overwriting an existing file.

It will only copy the source file, if the target directory does not have a file with the same name. If a duplicate file exists in the target, then the command will run, but won’t make any changes:

Upon querying using the ls command, it can be seen that no action is taken against the file because it still has the old attributes, which can be seen in the above example (refer section 10):

12) Prompting confirmation with cp command

Use the -i option with the cp command to prompt for confirmation before overwriting the file, in case the file exists in the target location.

The following example prompts for confirmation when copying the “cp-demo-9.txt” file because the file already exists in the target directory:

By default, the cp command excludes link files while performing the copy operation. Use the -d option with the cp command to copy the link files as shown below:

It has been successfully copied, and the soft link file can be found in the below output:

Conclusion:

In this tutorial, we have demonstrated 13 methods that are widely used by the Linux administrators in their daily operations. If you would like to explore other options, please visit the man page of the cp command by using the following command:

Источник

Linux Copy File Command [ cp Command Examples ]

cp Command Syntax

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Terminal app/Shell prompt
Est. reading time 3 mintues

The syntax is as follows to copy files and directories using the cp command:
cp SOURCE DEST
cp SOURCE DIRECTORY
cp SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY
cp [OPTION] SOURCE DEST
cp [OPTION] SOURCE DIRECTORY
Where,

  • In the first and second syntax you copy SOURCE file to DEST file or DIRECTORY.
  • In the third syntax you copy multiple SOURCE(s) (files) to DIRECTORY.

Note: You need to type the cp command at the dollar sign ($) prompt. This prompt means that the shell is ready to accept your typed commands. Do not type the dollar ($) sign. You need to open the Terminal app to use cp command on a Linux.

Linux Copy File Examples

To make a copy of a file called file.doc in the current directory as newfile.doc, enter:
$ cp file.doc newfile.doc
$ ls -l *.doc
Sample outputs:

You can copy multiple files simultaneously into another directory. In this example, copy the files named main.c, demo.h and lib.c into a directory named backup:
$ cp main.c demo.h libc. backup
If backup is located in /home/project, enter:
$ cp main.c demo.h libc. /home/project backup

Copy a file to another directory

To copy a file from your current directory into another directory called /tmp/, enter:
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename

Verbose option

To see files as they are copied pass the -v option as follows to the cp command:

Preserve file attributes

To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
$ cp -p file.txt /dir1/dir2/
$ cp -p filename /path/to/new/location/myfile
This option ( -p ) forces cp to preserve the following attributes of each source file in the copy as allowed by permissions:

  1. Modification time/date
  2. Access time
  3. File flags
  4. File mode
  5. User ID (UID)
  6. Group ID (GID)
  7. Access Control Lists (ACLs)
  8. Extended Attributes (EAs)

Copying all files

The star wildcard represents anything i.e. all files. To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup

The star wildcard represents anything whose name ends with the .doc extension. So, to copy all the document files (*.doc) in a directory to a new directory, enter:
$ cp *.doc /home/tom/backup

Recursive copy

To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively):
$ cp -R * /home/tom/backup

Linux copy file command with interactive option

You can get prompt before overwriting file. For example, if it is desired to make a copy of a file called foo and call it bar and if a file named bar already exists, the following would prompt the user prior to replacing any files with identical names:
cp -i foo bar

  • 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

Verbose output with cp command

If you pass the -v to the cp, it makes tells about what is going on. That is verbose output:
cp -v file1 file2
cp -avr dir2 /backups/

Conclusion

This page explained cp command that is used for copying files under Linux and Unix-like systems. For more info see man pages: ls(1).

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

Источник

Читайте также:  Настройка масштаба экрана windows
Оцените статью