Copy from command line linux

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

Источник

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 and Paste Text, Files and Folders in Linux Terminal

Copying and pasting is one of the most used actions on a computer. While it is easy to do so with the Ctrl + C and Ctrl + V keyboard shortcuts, on the Linux terminal it is not so straightforward. You have several options to get the job done. Here is how you can copy and paste text, files and directories in Linux terminal.

Also read: How to Easily Rename Files in Linux

Copy and Paste Text

If you just want to copy a piece of text in the terminal, all you need to do is highlight it with your mouse, then press Ctrl + Shift + C to copy.

To paste it where the cursor is, use the keyboard shortcut Ctrl + Shift + V .

The Paste shortcut also applies when you copy a section of text from a Word document (or any other application) and want to paste it in the terminal. For example, you can copy a command from a web page in your browser and use the Ctrl + Shift + V shortcut to paste it in the terminal.

Copy and Paste a Single File

Any time you want to copy a file or folder in the Linux command line, the above keyboard shortcut won’t work. You have to use the cp command. cp is shorthand for copy. The syntax is simple, too. Use cp followed by the file you want to copy and the destination where you want it moved.

That, of course, assumes that your file is in the same directory you’re working out of. You can specify both.

You also have the option of renaming your file while copying it. Specify the new name in the destination.

Copy and Paste a Folder and Its Contents

In order to copy a folder and its contents, you’re going to need to tell the cp command to copy recursively. That’s simple enough with the -r flag.

All the rest of your syntax is exactly the same. The -r flag serves to tell cp that it’s working with a directory and should copy its contents.

If you want the paste action to overwrite existing files, you can add the -f flag:

Also read: How to Compress Files with zstd Utility in Linux

Copy and Paste Multiple Files

You can also copy multiple files. The Linux command line lets you target multiple items at once with brackets <> . You can use them to list the names of each file to be copied separated by commas.

All three files of differing file types will be copied to the Documents directory.

Copy and Paste All Files of the Same Type

If you have a ton of files of the same type to copy, you can use the wildcard character * . The asterisk/wildcard tells the Linux command line to accept absolutely anything in that place. So, if you tell Linux to copy *.jpg , it’ll copy all JPG files, regardless of the name or whatever comes before the .jpg part.

If you want to use multiple file types, say JPG and PNG, you can use the brackets from before.

Move a File or Folder

If you came here looking to move a file from one place to another without making a duplicate, you can do that easily too, but moving a file requires the mv command. The syntax is very similar to cp.

Similarly, you can also rename it.

There is one major difference, though. You don’t need the -r flag to move a whole folder.

That’s all there is to it. You’re ready to start copying and moving your files from the command line. You can see that the command line way can be very efficient in some situations.

Want more pointers on the Linux command line? Here’s how to check sudo history or find out what the chmod 777 command does to your file permission.

Image credit: Copy – Paste by DepositPhotos

Источник

How to Copy Files and Directories in Linux

Home » SysAdmin » 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.

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:

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:

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:

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

To rename and copy a file to a different path:

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:

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:

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:

–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:

  • 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:

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.

Источник

Читайте также:  Подпись файла повреждена или недопустима windows 10
Оцените статью