- Copy all files in a directory to a local subdirectory in linux
- 3 Answers 3
- Copy via find -exec:
- Copy with extglob:
- Copy without extglob:
- How to Copy All Files from a Directory to another Directory in Linux
- How to Copy Files with “cp” Command in Linux:
- Copy a file with the same name:
- Copy a file with a different name:
- Copy Multiple Files with “cp” Command:
- How to Copy files with the “rsync” command in Linux:
- Conclusion:
- About the author
- Syeda Wardah Batool
- The Linux cp Command – How to Copy Files in Linux
- What is the cp command?
- How to copy files with the cp command
- How to copy directories with the cp command
- How to copy files that match a glob pattern
Copy all files in a directory to a local subdirectory in linux
I’d like to make a copy of all the existing files and directories located in this directory in new_subdir . How can I accomplish this via the linux terminal?
3 Answers 3
This is an old question, but none of the answers seem to work (they cause the destination folder to be copied recursively into itself), so I figured I’d offer up some working examples:
Copy via find -exec:
find . ! -regex ‘.*/new_subdir’ ! -regex ‘.’ -exec cp -r ‘<>‘ new_subdir \;
This code uses regex to find all files and directories (in the current directory) which are not new_subdir and copies them into new_subdir. The ! -regex ‘.’ bit is in there to keep the current directory itself from being included. Using find is the most powerful technique I know, but it’s long-winded and a bit confusing at times.
Copy with extglob:
cp -r !(new_subdir) new_subdir
If you have extglob enabled for your bash terminal (which is probably the case), then you can use ! to copy all things in the current directory which are not new_subdir into new_subdir.
Copy without extglob:
mv * new_subdir ; cp -r new_subdir/* .
If you don’t have extglob and find doesn’t appeal to you and you really want to do something hacky, you can move all of the files into the subdirectory, then recursively copy them back to the original directory. Unlike cp which copies the destination folder into itself, mv just throws an error when it tries to move the destination folder inside of itself. (But it successfully moves every other file and folder.)
How to Copy All Files from a Directory to another Directory in Linux
Copying the file or folder means creating a new file having duplicate content as in the existing file.
Sometimes, we need to copy the files or folders rather than having a backup program. The files can be copied with the same name, or you can change the name as well.
Copying a file, folder, or directory is a simple and basic task in the Linux operating system. Rename, delete or copy commands are used as daily purpose operations while working with the command-line interface.
Although there are multiple commands to copying the files, the “cp” and “rsync” command are widely used simplest approaches.
How to Copy Files with “cp” Command in Linux:
The “cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.
The syntax of the “cp” command is:
Let’s take a look at an example to understand the “cp” command tool better.
In the home directory, create a “temp” folder with the text file named “text_file1.txt” and add random content to it.
Copy a file with the same name:
To copy a “text_file1.txt” file directory with the same name, open the terminal and type the mentioned “cp” command with the right path.
Get the folder’s path by right-clicking on the file and navigate to the “Properties” option (it is the easy way to get the path link).
A dialogue box will open with the complete path of a text file:
Use this path with the “cp” command to copy file:
This command will copy the “text_file1.txt” file to the “temp2” folder.
To verify it, type the “ls” command in the terminal:
Copy a file with a different name:
To copy the file in the current working directory with the different name, type the following “cp” command with file location:
Verify it using the “ls” command”:
Copy Multiple Files with “cp” Command:
To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.
$ cp text_file1.txt text_file2.txt text_file3.txt / home / wardah / temp2
Run the mentioned command to verify if files are copied successfully:
Above mentioned scenarios are how to copy a single or selected file in a directory. Now, use the wildcard character (*) to copy present files of one directory to any other specific directory.
Run the “ls” command to check how many files exist in the temp directory:
Instead of mentioning all file names in the terminal, use the wildcard (*) with the directory path to copy all the files into destination:
Now, run the “ls” command again to check if all files are copied in the “temp2” directory:
How to Copy files with the “rsync” command in Linux:
The “rsync” command is another versatile Linux tool to synchronize and copy files and directories locally as well as remotely.
The syntax of the “rsync” command is to copy files is:
It is a pre-built tool in many Linux distribution. However, if you don’t get it on your system, install it by executing the following command:
To copy a file from one place to another, run the following command:
To copy all the directory files to another location, the command would be:
(The “-a” with the “rsync” command is used to copy directories recursively)
Here are the two concepts:
If you add a trailing slash (/) with the path, it will copy the content of the source directory to the destination directory, just like shown in the image:
But, if you don’t add it, it will copy the source directory inside the destination directory, like:
The above command will copy a “dir1” directory to the “dir2” directory.
Conclusion:
Copying a file or directory is the basic command one can operate. One can use it multiple times while using Linux operating system.
This guide has seen the two simplest approaches, the “cp” command and the “rsync” command. Using these commands, we have learned how to copy a single file, multiple files, and even copy one directory to another.
About the author
Syeda Wardah Batool
I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.
The Linux cp Command – How to Copy Files in Linux
Dillion Megida
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.
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
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.