1

I have a script that deletes files 7 days or older and then logs them to a folder. It logs and deletes everything correctly but when I open up the log file for viewing, its very sloppy.

log=$HOME/Deleted/$(date) 
find $HOME/OldLogFiles/ -type f -mtime +7 -delete -print > "$log"

The log file is difficult to read

Example File Output: (when opened in notepad)

/home/u0146121/OldLogFiles/file1.txt/home/u0146121/OldLogFiles/file2.txt/home/u0146121/OldLogFiles/file3.txt

Is there anyway to log the file nicer and cleaner? Maybe with the Filename, date deleted, and how old it was?

Any suggestions help!

terdon
  • 53,403
mkrouse
  • 71

1 Answers1

2

Sure try multiple exec statements like:

find $HOME/OldLogFiles/ -type f -mtime +7 -exec ls -latr "{}" \; -exec echo was deleted on `date` \; -exec rm -f "{}" \;|paste - - >> "$log"
mpy
  • 27,793
  • +1 for paste trick – mpy Jun 27 '13 at 21:13
  • log=$HOME/Deleted/$(date) find $HOME/OldLogFiles/ -type f -mtime -7 -exec ls -latr {} ; - exec echo was deleted on date ; -exec rm -f "{}" ;|paste - - >> $log

    that looks right but i keep getting this error and i dont know why

    ./test.sh: line 3: $log: ambiguous redirect find: ‘ls’ terminated by signal 13 find: ‘ls’ terminated by signal 13

    – mkrouse Jun 28 '13 at 14:27
  • 1
    @MattKrouse: The variable log contains white spaces (as it is derived from date: Fr Jun 28 16:49:19 CEST 2013), so the redirection >> $log should read >> "$log" like in your question. And probably you want to enclose also the ls parameter {} in quotes, like in the rm statement: ... -exec ls -latr "{}" ... – mpy Jun 28 '13 at 14:54
  • Thanks that worked! Can you answer another question? My log file now looks like this.. -rwxr-xr-x 1 u0146121 Domain Users 0 Jun 27 15:28 /home/u0146121/OldLogFiles/file1.txt Was deleted on Fri, Jun 28, 2013 10:08:28 AM

    What is the "-rwxr-xr-x 1 u0146121 Domain Users" part? Can I modify that at all?

    – mkrouse Jun 28 '13 at 15:12
  • @mpy alright, gotta do what you gotta do – mkrouse Jun 28 '13 at 15:17
  • @MattKrouse: You should make you comfortable with some basic linux commands... use e.g. ls -l and you will see the -rwxr-xr-x... part (this are the permission/owner/group), too. Use man ls for a more detailed explanation of the ls command. If you don't like that, simply omit the -l parameter in the find command: ...-exec ls -atr "{}". However then you will lose the modification date, too. I don't know an easy solution right now if you want to keep the date (that would make another good question). – mpy Jun 28 '13 at 15:25
  • 1
    If you want the modification date from ls -l you can pipe it through awk {print $6"-"$7"-"$8}' Or use stat -c %y – Gregg Leventhal Jun 28 '13 at 17:20
  • 1
    The -a option of ls is meaningless when applied to file argument(s).  (ls –l .bashrc works fine; you don’t need to say ls –la.)  And the -t and -r options are meaningful only when you have multiple arguments, or one (or more) directory argument(s).  So, since you’re saying -type f, you might as well say just -exec ls -l {}. – Scott - Слава Україні Jun 29 '13 at 19:01
  • Good point Scott. Not sure why I used all those switches. – Gregg Leventhal Jul 01 '13 at 15:14
  • @Gregg: Belatedly, welcome to Super User.  For your information, when you respond to a comment (in a new comment), it’s conventional to mention the author’s name, preceded by “@”, as in “@Scott”.  That way he gets notified.  You can abbreviate, or use an entire name (without spaces) as in “@GreggLeventhal”.  The author of a post is automatically notified of comments to that post (as, I presume, you have experienced).  See the Replying in comments paragraphs of the Comment formatting section of the Markdown Editing Help page. – Scott - Слава Україні Jul 02 '13 at 22:59