Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 15, 2015 17:09
Show Gist options
  • Save joyrexus/5294052 to your computer and use it in GitHub Desktop.
Save joyrexus/5294052 to your computer and use it in GitHub Desktop.
Move files with pattern in first line to target dir

Header Testing

List files containing PATTERN in first line of a file:

perl -ne'print "$ARGV\n" if /PATTERN/; close ARGV' *.txt

List files not containing PATTERN in first line of a file:

perl -ne'print "$ARGV\n" unless /PATTERN/; close ARGV' *.txt
#!/bin/bash
# move.sh - move files with specified pattern in first line to target dir.
USAGE="Usage: $0 PATTERN FILES... DIR"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
PATTERN=$1
TARGET_DIR=${!#}
shift
for i in "$@"
do
if [ -f $i ] && [ `perl -ne'print 1 if /'$PATTERN'/; close ARGV' $i` ]
then
echo "Moving $i to $TARGET_DIR"
mv $i $TARGET_DIR
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment