Skip to content

Instantly share code, notes, and snippets.

@nick96
Created June 8, 2017 07:41
Show Gist options
  • Save nick96/a4e95ea6fb0c3a204fc9ba9b1a260619 to your computer and use it in GitHub Desktop.
Save nick96/a4e95ea6fb0c3a204fc9ba9b1a260619 to your computer and use it in GitHub Desktop.
Copy multiple files of the same extension from one directory to another
# Requires find and xargs from GNU's findutils
# Just another example of the joys of composability in the UNIX philosophy
find <src-directory> -name *.<extension> -print0 | xargs -0 -I % cp % <dst-directory>
# Breakdown
# find -- from findutils, does what is says on the tin
# -name -- File name(s) to find, in this case we used the shell glob '*' to get any files with <extension>
# -print0 -- Separate files with a NULL byte
#
# xargs -- from findutils, applies a command to each individual file
# -0 -- Expect file names to be separated by a NULL byte
# -I -- symbol to use for the file names from the find command, here we use '%'
#
# Then it's just the normal cp command using '%' to represent the file being copied
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment