WORLD'S WORST SOFTWARE
Software by jason, for jason.
Software by jason, for jason.
Home
About
Articles
Resume
Contact
Software
iPlaylist CopierJava Libraries
ItunesUtilitiesScripts
Bash Scripts
File Size File Size Test Free Space Line Counter List Copy MD5 Tool OS X Rotate Logs Rename It SnapshotPHP Scripts
Write Variable To File WWS MailerJavascript Scripts
WWS Background ScalerInteresting People
Admins
Phil GlocknerDesigners
Tonya Browning Catherine Chu Justin Cox Richard Georges Stephen Gray Jonathan Horak Justin Kiehl Andrea Spencer MeatheadDevelopers
Chris Austin Joost van Dongen Noah Figg Rick Hogge John Quarles Wayne Rittiman Joe Rivera Brent Schneeman David Scott Andy Skelton Pavan TumatiList Copy
The list copy bash script copies files or directories listed in a text file to a directory you specify. Simply specify one file or directory per line in a text file and execute the script. List copy makes it easier for you to maintain a list of files and/or directories to copy during a backup procedure. The list file can be either DOS or UNIX formatted, and comments within the list file will be ignored.
Dependencies
bash, echo, cat, tr, test, grep, cp
Download
listcopy.sh [2.40 KB]
The Script
#!/bin/bash
#####################################################################
#
# listcopy.sh - copy files or folders specified in a text file
# written by Jason Baker (http://www.worldsworstsoftware.com)
#
#####################################################################
#
# UPDATES:
# 2006/10/25
# - changed dos2unix usage to tr
# - updated usage notes
# 2006/10/13
# - initial version
#
#####################################################################
exitcode=0
function print_usage()
{
echo "listcopy.sh copies files or folders specified in a text file"
echo
echo "USAGE"
echo " listcopy.sh LIST_FILE TARGET_PATH"
echo
echo "NOTES"
echo " LIST_FILE should contain only one file or folder entry per line."
echo
echo " Lines in LIST_FILE prefixed with a \"#\" will be ignored."
echo
echo "EXIT STATUS"
echo " 0 - success"
echo " 1 - errors occurred"
echo
echo
echo " ERROR: $1"
echo
exit 1
}
function list_copy()
{
#
# thanks to http://www.vasudevaservice.com/documentation/how-to/converting_dos_and_unix_text_files
# for help w/ dos2unix to TR convert tip
#
LIST_ENTRIES=`cat "$1" | tr -d '\15\32'`
TARGETPATH=${2%*/}/ #make sure the target path has a / on its end
#make for's argument seperator newline only
IFS=$'\n'
for ENTRY in $LIST_ENTRIES
do
if test -z "$ENTRY"
then
#echo "Ignoring Blank Line." #test stuff
continue
fi
#TODO FUTURE: handle case where there is whitespace before comment marker
if test ${ENTRY:0:1} = "#"
then
#echo "Ignoring Comment: $ENTRY" #test stuff
continue
fi
if test -z `echo "$ENTRY" | grep -E '[^[:blank:]]+'`
then
#echo "Ignoring whitespace: $ENTRY" #test stuff
continue
fi
if test -f "$ENTRY"
then
echo "Copying file: $ENTRY"
cp -f "$ENTRY" "$TARGETPATH"
elif test -d "$ENTRY"
then
echo "Copying folder: $ENTRY"
ENTRY=${ENTRY%*/} #make sure the folder does not have a / on its end
cp -Rf "$ENTRY" "$TARGETPATH"
else
echo "File or folder does not exist: $ENTRY"
exitcode=1
fi
done
}
if test -z "$1" -o -z "$2"
then
print_usage "Invalid number of arguments specified."
fi
if test ! -r "$1"
then
print_usage "Cannot read file: $1"
fi
if test ! -d "$2"
then
print_usage "Target path is not a directory: $2"
fi
if test ! -w "$2"
then
print_usage "Target path is not writable: $2"
fi
list_copy "$1" "$2"
exit $exitcode
#####################################################################
#
# listcopy.sh - copy files or folders specified in a text file
# written by Jason Baker (http://www.worldsworstsoftware.com)
#
#####################################################################
#
# UPDATES:
# 2006/10/25
# - changed dos2unix usage to tr
# - updated usage notes
# 2006/10/13
# - initial version
#
#####################################################################
exitcode=0
function print_usage()
{
echo "listcopy.sh copies files or folders specified in a text file"
echo
echo "USAGE"
echo " listcopy.sh LIST_FILE TARGET_PATH"
echo
echo "NOTES"
echo " LIST_FILE should contain only one file or folder entry per line."
echo
echo " Lines in LIST_FILE prefixed with a \"#\" will be ignored."
echo
echo "EXIT STATUS"
echo " 0 - success"
echo " 1 - errors occurred"
echo
echo
echo " ERROR: $1"
echo
exit 1
}
function list_copy()
{
#
# thanks to http://www.vasudevaservice.com/documentation/how-to/converting_dos_and_unix_text_files
# for help w/ dos2unix to TR convert tip
#
LIST_ENTRIES=`cat "$1" | tr -d '\15\32'`
TARGETPATH=${2%*/}/ #make sure the target path has a / on its end
#make for's argument seperator newline only
IFS=$'\n'
for ENTRY in $LIST_ENTRIES
do
if test -z "$ENTRY"
then
#echo "Ignoring Blank Line." #test stuff
continue
fi
#TODO FUTURE: handle case where there is whitespace before comment marker
if test ${ENTRY:0:1} = "#"
then
#echo "Ignoring Comment: $ENTRY" #test stuff
continue
fi
if test -z `echo "$ENTRY" | grep -E '[^[:blank:]]+'`
then
#echo "Ignoring whitespace: $ENTRY" #test stuff
continue
fi
if test -f "$ENTRY"
then
echo "Copying file: $ENTRY"
cp -f "$ENTRY" "$TARGETPATH"
elif test -d "$ENTRY"
then
echo "Copying folder: $ENTRY"
ENTRY=${ENTRY%*/} #make sure the folder does not have a / on its end
cp -Rf "$ENTRY" "$TARGETPATH"
else
echo "File or folder does not exist: $ENTRY"
exitcode=1
fi
done
}
if test -z "$1" -o -z "$2"
then
print_usage "Invalid number of arguments specified."
fi
if test ! -r "$1"
then
print_usage "Cannot read file: $1"
fi
if test ! -d "$2"
then
print_usage "Target path is not a directory: $2"
fi
if test ! -w "$2"
then
print_usage "Target path is not writable: $2"
fi
list_copy "$1" "$2"
exit $exitcode
© 2006, 2007 Jason Baker