Skip to content

Instantly share code, notes, and snippets.

@mojaveazure
Last active April 29, 2019 18:20
Show Gist options
  • Save mojaveazure/891723904ace9712dfe4 to your computer and use it in GitHub Desktop.
Save mojaveazure/891723904ace9712dfe4 to your computer and use it in GitHub Desktop.
A simple script to copy files defined by a list in parallel
#!/bin/bash
set -e
set -u
set -o pipefail
# A simple script to find files listed in a text file and copy them elsewhere
# Written for Chaochih Liu
# Written by Paul Hoffman
function Usage() {
echo -e "\
Usage: ./so_needy.sh <file_info> <file_dir> <cp_dir> \n\
where: <file_info> is a list of file names \n\
either including or excluding extensions \n\
\n\
<file_dir> is the directory where files are stored \n\
\n\
<cp_dir> is the directory to copy the files to \n\
\n\
ex. ./so_needy.sh files.txt ~/directory1 ~/directory2 \n\
---------------------------------------------------------------\n\
\n\
Please note: this requires GNU Parallel to run \n\
""" >&2
exit 1
}
if [ "$#" -lt 3 ]
then
Usage
fi
# Check to see if GNU Parallel is installed
if `command -v parallel > /dev/null 2> /dev/null`
then
echo "Parallel is installed"
else
echo "Parallel is not installed"
exit 1
fi
# Specify the list of files
FILE_INFO="$1"
# Specify the directory where files are stored
FILE_DIR="$2"
# Specify the directory to where they need to be copied
CP_DIR="$3"
# Find the files and copy them in parallel
cat ${SAMPLE_INFO} | parallel "find ${FILE_DIR} -name *{}* | xargs -I % cp -a % ${CP_DIR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment