Linux command copy all files in directory

Copy Files and Directories in Linux

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Copying a file is one of the most common Linux tasks. Ubuntu and other Linux distributions use the cp command to copy one or more files, and to copy entire directories. This guide explains how to use the cp command to copy files on Linux. It also lists the different variations of this command and describes the different cp command options.

An Introduction to cp

The cp command is used to copy one or more files on a Linux system to a new location. It is similar to the mv command, except it does not move or remove the original file, which remains in place. Like most Linux commands, cp is run using the command line of a system terminal.

The cp command allows users to copy a file to either the same directory or a different location. It is also possible to give the copy a different name than the original file. The -r option enables the cp command to operate recursively and copy a directory along with any files and subdirectories it contains. cp has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file attributes of the original.

Users must have sudo privileges to copy protected files. Otherwise, sudo is not required.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo . If you are not familiar with the sudo command, see the Users and Groups guide.

Читайте также:  Linux ограничение времени работы пользователя

How to Use the cp Command to Copy Files and Directories in Linux

The cp command works similarly on most Linux distributions. The command operates in four basic modes.

  • Copies a file to the same directory. The new file must have a different name.
  • Copies a file to a different directory. It is possible to rename the file or retain the old name.
  • Copy multiple files to a different target directory.
  • Recursively copy the contents of a directory, including subdirectories, to a different target directory.

There are a number of concerns to be aware of when using cp . For instance, cp does not display a warning when overwriting an existing file. This situation occurs when copying a file to a new directory already containing a file with the same name. This problem is more likely to happen when copying multiple files. To avoid this problem, users can use interactive mode to force Linux to request confirmation before overwriting a file.

cp is often used in conjunction with the ls command. ls lists the contents of the current directory. This is handy for confirming the exact name and location of the source files and directories.

Some of the most important cp command options include the following:

  • -f : Forces a copy in all circumstances.
  • -i : Runs cp in interactive mode. In this mode, Linux asks for confirmation before overwriting any existing files or directories. Without this option, Linux does not display any warnings.
  • -p : Preserves the file attributes of the original file in the copy. File attributes include the date stamps for file creation and last modification, user ID, group IP, and file permissions.
  • -R : Copies files recursively. All files and subdirectories in the specified source directory are copied to the destination.
  • -u : Overwrites the destination file only if the source file is newer than the destination file.
  • -v : Runs cp in verbose mode. This mode provides extra information on the copying process. This is useful for keeping track of progress when copying a large number of files.

The options -H , -L , and -P indicate how the cp command should process symbolic links. See the cp man page for a full description of cp and symbolic links. The options for cp vary between Linux distributions. A list for Ubuntu 22.04 LTS is available in the Ubuntu cp documentation.

How to Copy a File in Linux

One common use of cp is to make a second copy of the source file in the same directory. Supply a different name for the copy to differentiate it from the original. A common convention is to add an extra extension such as .bak or .cp to the existing file name. For example, a standard name for a backup copy of archive.txt is archive.txt.bak .

Читайте также:  Linux file command in windows

The cp command operates in the context of the current working directory. However, files can be specified using either an absolute or relative path. Here is the basic cp command to copy a file within the same directory.

Источник

The Linux cp Command – How to Copy Files in Linux

Dillion Megida

Dillion Megida

The Linux cp Command – How to Copy Files in Linux

There are a couple different ways to copy and paste content when you’re working on your computer.

If you spend more time in the user interface of your device, you’ll probably use your mouse to do this. You can copy files by right-clicking on the file and selecting «Copy», then going to a different directory and selecting «Paste».

For my terminal friends, you can also perform file copy-paste operations without leaving the terminal. In a Linux-based terminal, you do this using the cp command.

In this article, I’ll explain what the cp command is and show you how to copy and paste files and directories in Linux using the terminal.

What is the cp command?

You use the cp command for copying files from one location to another. This command can also copy directories (folders).

The syntax of this command is:

cp [. file/directory-sources] [destination] 

[file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.

To understand the rest of this article, I will use this folder structure example. Let’s say a directory called DirectoryA has two directories in it: DirectoryA_1 and DirectoryA_2. These subdirectories have many files and sub directories in them.

I’ll also assume you’re currently in the DirectoryA location in the terminal, so if you aren’t, make sure you are:

How to copy files with the cp command

If you want to copy a file, say README.txt from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:

cp ./DirectoryA_1/README.txt ./DirectoryA_2 # ./DirectoryA_1/README.txt is the source file # ./DirectoryA_2 is the destination 

If you want to copy more than a file from DirectoryA_1 to DirectoryA_2, you will use the cp command like this:

cp ./DirectoryA_1/README.txt ./DirectoryA_1/ANOTHER_FILE.txt ./DirectoryA_2 

As you can see, you will put all the source files first, and the last argument will be the destination.

Читайте также:  Linux mint нет desktop sharing

How to copy directories with the cp command

By default, the cp command works with files. So if you attempt to copy a directory like this:

cp ./DirectoryA_1/Folder/ ./DirectoryA_2 

You will get an error stating:

./DirectoryA_1/Folder/ is a directory

To copy directories, you have to pass the -r flag. This flag informs the cp command to recursively copy a directory and its contents (which could be files or other sub directories). So for the previous command, you can add the flag before the directory sources like this:

cp -r ./DirectoryA_1/Folder/ ./DirectoryA_2 

This command will recursively copy the Folder directory in ./DirectoryA_1/ as well as all files and directories in the Folder directory.

How to copy files that match a glob pattern

A glob pattern is similar to Regex, which allows you to match multiple files with names that match a specific pattern. Learn more about the difference here: Regex vs Glob patterns.

For example, if you want to copy all files in DirectoryA_1 with the .txt extension, you can execute this command:

cp ./DirectoryA_1/*.txt ./DirectoryA_2 

./DirectoryA_1/*.txt matches files with the .txt extension in their names, and the cp command can copy all those files to the destination.

You can check out the glob documentation to learn more about globbing patterns and characters you can use.

Now you know how to copy files (and directories) right from the command line. Thanks for reading!

Dillion Megida

Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

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