Skip to content

Instantly share code, notes, and snippets.

@remore
Created November 15, 2012 08:12
Show Gist options
  • Save remore/4077337 to your computer and use it in GitHub Desktop.
Save remore/4077337 to your computer and use it in GitHub Desktop.
Copy specific files from local storage to local storage with using folder names passed by pipe
#!/bin/bash
# usage:
# ls -R | grep FOLDER_NAME_TO_SEARCH | ./this_script.sh TARGET_FILE.html
# Verify if the # of passed parameter is only 1
if [ $# -ne 1 ]; then
echo "you must specify 1 parameter to grab file to be copied"
exit 0;
fi
# Declare variables
FILENAME=$1
DESTINATION="checkAndCopy_`date +"%Y%m%d_%I%M%S"`"
# Make folder to contain contents
mkdir "${DESTINATION}"
# Copy
while read data
do
if [ ${data:0:1} = "/" ]; then
SOURCE="${data/%?/}/${FILENAME}"
TARGET=${SOURCE//\//_}
TARGET=${TARGET//\:/_}
TARGET=${TARGET//:/_}
cp "${SOURCE}" "${DESTINATION}/${TARGET}"
fi
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment