1

I'm trying to figure out a way to use a bash script that will: - Search a directory - Identify a file by creation date - Rename that file

I'm 'hoping' to use a: 'if, then' script to accomplish this. Anybody have any idea how to do this???

Example:

if 'search in /current/path' for 'file created 12-25-2000' then mv /current/path/FILENAME /other/path/FILENAME-1

Here's what I have right now but it doesn't work:

#!/bin/bash

touch --date "2000-12-24" /tmp/start
touch --date "2000-12-26" /tmp/end

if ls -l /current/path -type f -newer /tmp/start -not -newer /tmp/end

then mv /current/path/FILENAME-1 /other/path/FILENAME 

#Reapplying original FILENAME 

echo "Back to boring..."

fi
thazsar
  • 25
  • 7
  • Check out this link as a start : http://stackoverflow.com/questions/158044/how-to-use-find-to-search-for-files-created-on-a-specific-date – Tim Botha Nov 20 '14 at 04:44

2 Answers2

1

To find all files in /tmp and its subdirectories with a modify date of 2000-12-25 and to move them to /other/path/, try:

find /tmp -daystart -type f -newermt "2000-12-24"  ! -newermt "2000-12-25" -exec mv {} "/other/path/$(basename {})-1" \;

Explanation

  • /tmp

    Start looking in the /tmp directory and its subdirectories. This can be replaced with any other path, or list of paths, that you like.

  • -daystart

    Optional. Count the day a file was created from the beginning of the day rather than multiples of 24 hours before now.

  • -newermt "2000-12-24" ! -newermt "2000-12-25"

    Select only files with a modify date newer than 2000-12-24 but not newer than 2000-12-25. This selects only files from 2000-12-25.

  • -type f

    Optional. Select only regular files. Don't try to move directories.

  • -exec mv {} "/other/path/$(basename {})-1" \;

    When a file is found, execute this move command on it. The name of the file, including path elements needed to find it are substituted in for {} wherever it occurs. $(basename {}) gets the name of the file without the path.

    It may seem a little odd at first find requires that -exec commands such as this end with a semicolon. To keep the shell from eating the semicolon, it has to be escaped. Hence, the \; at the end of the command.

Debugging

If things are not working as expected, try leaving off the -exec clause:

find /tmp -daystart -type f -newermt "2000-12-24"  ! -newermt "2000-12-25"

Instead of moving files, this will just print their names. If the correct names print, then the problem is with the -exec clause. If the wanted file names are missing, try leaving off more clauses. For example, this should list all files in /tmp and its subdirectories:

find /tmp

This should list all file in /tmp that are older than 2000-12-25:

find /tmp -daystart ! -newermt "2000-12-25"
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanx for that detailed explanation! Unfortunately, I can't rely on finding the file within a timeframe. I need to find the exact date that I set for this file – thazsar Nov 20 '14 at 05:31
  • This is for iOS and I was meaning that the search function can't depend on 'how long ago' a file was modified. My file is dated for 12-25-2000 so I can easily find it in the folder. I just can't find any examples that work for identifying an exact date. – thazsar Nov 20 '14 at 05:42
  • @thazsar I updated the answer to look for that date. Let me know if it works for you. – John1024 Nov 20 '14 at 05:49
  • @thazsar I searched [Apple](https://developer.apple.com/search/index.php?q=find%20exec&platform=iOS) and did not find a man page for `find` on iOS. Is it available on your device? – John1024 Nov 20 '14 at 06:02
  • Tried the above code but still no dice. Yes, 'find' can be used in iOS so I'm not sure why nothing is taking. – thazsar Nov 20 '14 at 06:13
  • Any tips on inserting a string w/ a Boolean TRUE value via preinst and then removing that string using a postrm? I'd be fine w/ that, too – thazsar Nov 20 '14 at 06:19
  • @thazsar Inserting and removing strings sounds easy. I am, however, not familiar with iOS or its preinst or postrm. – John1024 Nov 20 '14 at 06:22
  • Turns out iOS doesn't like -newermt. I'll keep playing but thanx for all your help!!! – thazsar Nov 20 '14 at 06:34
1

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
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Turns out you were right in instructing me to use 'find!' I reverted back to my script but placed it in the preinst file and it worx!!! – thazsar Nov 20 '14 at 07:10