Copy all linux command line

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

Источник

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

Источник

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

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:

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:

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.

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

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:

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.

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.

That’s it

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.

Источник

cp Command in Linux: 7 Practical Examples

One of the commands that you must know in Linux is cp. It’s often called the copy command in Linux and it is actually short for copy and it does exactly as it name suggests: it copies.

cp is used for copying files from one location to other. cp can also be used to copy entire directories into a new location. You can use it to copy multiple files and directories as well.

7 Examples of using cp command in Linux

Let’s see how you can use cp command for various purposes:

1. How to copy a file

The simplest example would be to copy a file. To do that, you just have to specify the source file and the destination directory or file.

In the above example, if the target_fille doesn’t exist in the target_directory, it will create target_file.

However, if the new_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.

I’ll show you how to deal with overwriting of files later in this tutorial.

Keep in mind: By default, cp commands overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.

2. How to copy multiple files

If you want to copy multiple files at once to a new location, you can do that in the following manner:

This will copy all the specified files to the target directory. If the target directory has file(s) matching the name of the source file(s), it will be overwritten.

3. Multiple ways of dealing with overwriting while copying files

You probably won’t always want that your existing target files are overwritten and that’s totally logical.

To prevent overwriting existing files, you can use the -n option. This way, cp won’ overwrite existing files.

But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).

You can enter y for overwriting the existing file or n for not overwriting it.

There is also an option for making automatic backups. If you use -b option with cp command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.

The backup of the file ends with

You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be copied to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.

  • -i : Confirm before overwriting
  • -n : No overwriting
  • -b : Overwriting with backup
  • -u : Overwrite if the target file is old or doesn’t exist

4. How to copy a directory in Linux

You can also use the cp command to copy a directory in Linux including all its files and sub-directories. You have to use the -r option here which stands for recursive.

This will copy the entire source_dir into target_dir. Now the source_dir will be a subdirectory of the target_dir.

5. How to copy only the content of a directory, not the directory itself

In the previous example, you copied the entire directory into a new location.

But if you just want to copy the content of the source directory into the target directory, you should add /. at the end of the source directory. This will indicate that you only want to copy the content of the source directory.

Let’s see it with an example:

Now copy the content of the source directory:

If you check the contents of the target directory now, you’ll see that only the contents of the source directory have been copied.

6. How to copy multiple directories

You can also copy multiple directories at once with cp command in Linux.

Just use it the same way you did for a single directory.

It’s always the last argument in the command that is taken as the target directory.

If you want to copy only the content of multiple directories at once, you can do that as well:

In fact, you can mix directories, their content and files altogether.

Tip: You can use the verbose mode with option -v to see what files are being copied.

7. How to preserve the attributes while copying

When you copy a file to a new location, its attributes like the file permissions and the file timestamps are changed.

If you want to retain the attributes of the original file, you can copy the files with the option -p.

Let’s see it with an example.

If I try to copy this file normally, its attributes will be changed:

But if I use the option p, the copied file will retain the mode, ownership and the timestamp.

As you can see, you preserved the access mode and the timestamp pf the source file with the -p option.

But wait! Was it not supposed to preserve the ownership of the source files as well? But here the owner (root) of the source file has been changed to abhishek.

This is because only root has the permission to change the ownership of a file owned by root. If you use the -p option with a file not owned by root, it will preserve the ownership. Or, you can run the command with sudo to preserve the ownership of a file owned by root.

You can also specify the attributes you want to preserve. But then you’ll have to use the –preserve option.

As you can see in the above output, it preserved only the timestamp of the source file.

You can further explore the cp command by browsing its man page. The examples shown here are the most common ones that you’ll be using as Linux user, sysadmin or software developer.

If you liked this tutorial, please share this article on social media and various forums.

Источник

Читайте также:  Lexmark mx517de драйвер windows 10
Оцените статью