BASH script to cp/mv files of a certain date
I have a client that owns a high volume call center. For quality assurance and training purposes they record all the calls. Periodically the volume of recorded calls grows to the point that it slows down the NFS share to the Queuematrics call center software(Tomcat app). I wrote a script that finds files by time and moves them to a different directory.
Usage: copy-by-date.sh -i INPUT_PATH -o OUTPUT_PATH -d +DAYS_OLD
I created two test folders for INPUT(“files”) and OUTPUT(“My drive”).
copy-by-date.sh files My drive README
[matt@mattcom1 copy-by-date]$
I the “files” folder I have four test files. I modified the time stamp so three of them appear to be from 2016.
total 8
drwxrwxr-x 2 matt matt 4096 Aug 1 03:24 .
drwxrwxr-x 5 matt matt 4096 Aug 1 03:24 ..
-rw-rw-r– 1 matt matt 0 Jan 18 2016 test1.wav
-rw-rw-r– 1 matt matt 0 Jan 18 2016 test2.wav
-rw-rw-r– 1 matt matt 0 Jan 18 2016 test3.mp3
-rw-rw-r– 1 matt matt 0 Jul 31 23:32 test4.wav
[matt@mattcom1 copy-by-date]$
Copying: files/test1.wav to My drive
Copying: files/test2.wav to My drive
Copying: files/test3.mp3 to My drive
The above script moved all files is input directory to the output directory.
test1.wav test2.wav test3.mp3
The script still needs more testing and I’m adding an option to “–copy-only” using ‘cp -rfp’ rather than ‘mv’. Check back for an update when I get back from vacation. Thank you for reading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#!/bin/bash # Copyright Matt Birkland 2017 # copy-by-date.sh License: GPLV2 # For more info: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html # # Tested of Fedora 21 & CentOS 7 # # copy-by-date.sh synopsis: # # - check for file lock # - check if Directories exist # - checks to make sure options properly set # - mv files # lockfile /tmp/file_move if [ -e "/tmp/cp-by-date.lock" ]; then echo "cp-by-date already in progress" exit 1 else touch /tmp/file_move.lock fi # Set command line options #--------------------------------------- while getopts 'd:i:o:p:' OPTION do case $OPTION in d) DATE="$OPTARG" ;; i) INPUT_DIR="$OPTARG" ;; o) OUTPUT_DIR="$OPTARG" ;; p) PATTERN="$OPTARG" ;; esac done #-------------------------------------- # test date switch # begin file move if [ $DATE ] && [ $PATTERN ] && [ "$OUTPUT_DIR" ] && [ $INPUT_DIR ]; then ITEMS=$(find $INPUT_DIR/* -mtime $DATE -print | grep $PATTERN) for i in $ITEMS; do mv $i "$OUTPUT_DIR" echo "Copying: $i to $OUTPUT_DIR" done elif [ $DATE ] && [ "$OUTPUT_DIR" ] && [ $INPUT_DIR ]; then ITEMS=$(find $INPUT_DIR/* -mtime $DATE -print) for i in $ITEMS; do mv $i "$OUTPUT_DIR" echo "Copying: $i to $OUTPUT_DIR" done else echo "The following example moves files from INPUT directory to OUTPUT directory that are 30 days or older." echo "copy-by-date example: copy-by-date -i INPUT -o OUTPUT -d +30" echo "Optionally add -p to add grep pattern match to the directory." exit 1 fi # remove lock rm -rf /tmp/cp-by-date.lock exit 0 |
No Comments »
RSS feed for comments on this post. TrackBack URL