Skip to content

Copying and moving files on Linux

G8th September 2020

CNo Comments

TLinux

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:

  1. Create a destination directory at /location/of/files/destination/ (if required).
  2. Copy or move files from /location/of/files/source/ to /location/of/files/destination/ recursively and non-recursively.
  3. Delete the folder and other contents at /location/of/files/source/.

Here’s what you can do after completing the first steps:

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 every subdirectory and move all files into the destination directory, but will not keep the directory structure.

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!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.