Copying and moving files on Linux
Copy or move files of type from one directory to another on a Linux operating system via the command line.
Copying or moving files en masse in Linux via the command line is very efficient, but the syntax can be a challenge. I have found the below commands particularly useful in managing this process.
In the following examples, we will:
- Create a destination directory at /location/of/files/destination/ (if required).
- Copy or move files from /location/of/files/source/ to /location/of/files/destination/ recursively and non-recursively.
- Delete the folder and other contents at /location/of/files/source/.
Here’s what you can do after completing the first steps:
- Recursively copy files of type into a flat directory.
- Recursively copy files of type and maintain the directory structure.
- Recursively copy all files and maintain the directory structure.
- Copy all files of type from a single directory.
- Recursively move files of type into a flat directory.
- Move all files of type from a single directory.
- Delete a directory and all its subdirectories and contents.
Remember to backup your work before you begin!
First steps: Check contents and create the destination directory
Go to the source location of the files:
cd /location/of/files/source/
List the files in the directory and review (if so desired):
ls /location/of/files/source/
Create a new directory (if required):
mkdir /location/of/files/destination/
Copy files of type into a single directory, recurse
This uses the -iname
flag which is case insensitive. The following commmand will go through every subdirectory and copy all files into the destination directory, but will not keep the directory structure (ie the directory structure is flattened).
find -iname '*.png' -exec cp {} /location/of/files/destination/ \;
For targeting multiple file types, you can use regular expressions (regex), but as in the case above, directory structures will not be copied:
find -regex '.*\.\(png\|jpg\|svg\)' -exec cp {} /location/of/files/destination/ \;
Copy files of type, recurse, maintain directory structures
This following commmand uses regular expressions to identify files of various types and copies them whilst maintaining their directory structure. Note that directories that do not have any files that match the regular expression will not be copied.
find -regex '.*\.\(png\|jpg\|svg\)' -exec cp --parents \{\} /location/of/files/destination/ \;
Copy all files and directories, recurse
This command will create the destination folder, go through every subdirectory and copy all files into the destination directory whilst maintaining the directory structure.
cp -r /location/of/files/source/ /location/of/files/destination/
Copy files of type, do not recurse
This will go through the current directory only and copy all files of type into the destination directory.
find -maxdepth 1 -iname '*.png' -exec cp {} /location/of/files/destination/
Move files of type, recurse
This will go through every subdirectory and move all files into the destination directory, but will not keep the directory structure.
find -iname '*.log' -exec mv {} /location/of/files/destination/ \;
Move files of type, do not recurse
This will go through the target subdirectory and move all files into the destination directory.
find -maxdepth 1 -iname '*.log' -exec mv {} /location/of/files/destination/ \;
Delete a folder and subfolders, including all remaining contents
This command will recurse through the target directories and delete everything, so be careful!
rm -r /location/of/files/source/
There may be more efficient ways of achieving the above, but these work well for me.
Comments
No comments have yet been submitted. Be the first!