There are a number of ways to do this. You were on the right track using touch
and temp files
to set the boundaries for the search. You are probably better off using find
as opposed to ls
. The following script takes the target directory
and the start time
as arguments to search for files in the target dir
that are between that start time
and end time (default = start time + 1 day)
. If any files fall within the time(s) given, the files are added to an array. You can then use the filenames in the array to move as you desire.
Since the script makes use of find
, you can tailor the selection by find
to meet almost any need. The move criteria will have to be provided in the script as needed. Here is a quick example:
#!/bin/bash
## validate required input and provide usage information
[ -n "$1" -a -n "$2" ] || {
printf "\nerror: insufficient input. Usage: %s path start_date [end_date (end=start+1d)]\n\n" "${0//\//}"
printf " NOTE: date STRING -- any allowable formatt accepted by coreutils 'date -d=STRING'\n\n"
printf " examples -- 11/01/2014 or \"11/01/2014 10:25:30\" or 20141101\n\n"
exit 1
}
## store input path and start time
path="$1"
starttm=$2
## set start and end date (end defaults to starttm + 1 day)
startd="$(date -d "$starttm" +%s)"
endd="$(date -d "${3:-$(date -d "$starttm + 1 day")}" +%s)"
## set temp dir
[ -d /tmp ] && tmpdir="/tmp" || tmpdir="$PWD"
## tempfiles start & end (to create with compare dates)
tfs="${tmpdir}/ttm_${startd}"
tfe="${tmpdir}/ttm_${endd}"
## create temp file with start and end dates
touch -t $(date -d "@${startd}" +%Y%m%d%H%M.%S) $tfs
touch -t $(date -d "@${endd}" +%Y%m%d%H%M.%S) $tfe
## fill array using find to select files between tempfile dates
file_array=( $(find "$path" -maxdepth 1 -type f -newer $tfs ! -newer $tfe) )
## cleanup - remove temp files
rm $tfs $tfe || printf "warning: failed to remove tempfiles '%s' or '%s'\n" "$tfs" "$tfe"
## rename files at will
if [ "${#file_array[@]}" -gt 0 ]; then
for i in "${file_array[@]}"; do
fdir="${i%/*}"
ffn="${i##*/}"
printf " moving %-32s -> %s\n" "$i" "${fdir}/newname_${ffn}"
done
fi
exit 0
usage (run without arguments):
$ ./find_range_snip.sh
error: insufficient input. Usage: .find_range_snip.sh path start_date [end_date (end=start+1d)]
NOTE: date STRING -- any allowable formatt accepted by coreutils 'date -d=STRING'
examples -- 11/01/2014 or "11/01/2014 10:25:30" or 20141101
test directory:
ls -l ~/tmp
total 387224
drwxr-xr-x 3 david david 4096 Nov 3 15:51 asm
drwxr-xr-x 16 david dcr 4096 Jul 13 2010 fluxbox
drwxr-xr-x 3 david david 4096 Oct 13 13:42 log
-rw-r--r-- 1 david david 159557 Jul 16 04:15 acpidumpfile.bin
-rw-r--r-- 1 david david 1429 Jul 13 10:57 blderror.txt
-rw-r--r-- 1 david david 7663 Aug 21 05:39 fc-list-fonts-sorted-no-path.txt
-rw-r--r-- 1 david users 60 Jul 13 02:20 homelnk
-rw-r--r-- 1 david david 870 Sep 6 03:32 junk.c
-rw-r--r-- 1 david david 32323 Aug 1 21:53 knemo-no-essid.jpg
-rw-r--r-- 1 david david 14082 Sep 19 18:29 rlfwiki.tbl.desc
-rw-r--r-- 1 david david 2211 Jul 29 02:23 scrnbrightness.sh
-rw-r--r-- 1 david david 7456152 Sep 19 13:22 tcpd.tar.xz
-rw-r--r-- 1 david david 3371941 Sep 19 22:08 tcpdump-capt
-rw-r--r-- 1 david david 589676 Sep 19 14:49 tcpdump.new.1000
-rw-r--r-- 1 david david 0 Oct 26 02:38 test
-rw-r--r-- 1 david david 595 Jul 23 21:25 tmpkernel315.txt
-rwxr-xr-x 1 david david 12694 Oct 13 17:44 tstvi
-rw-r--r-- 1 david david 620 Oct 13 17:47 tstvi.c
-rw-r--r-- 1 david david 3599 Jul 16 04:29 xrandr-output.txt
output:
$ ./find_range_snip.sh ~/tmp 09/19/2014
moving /home/david/tmp/tcpd.tar.xz -> /home/david/tmp/newname_tcpd.tar.xz
moving /home/david/tmp/rlfwiki.tbl.desc -> /home/david/tmp/newname_rlfwiki.tbl.desc
moving /home/david/tmp/tcpdump-capt -> /home/david/tmp/newname_tcpdump-capt
moving /home/david/tmp/tcpdump.new.1000 -> /home/david/tmp/newname_tcpdump.new.1000