Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 2 down vote accepted

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"
share|improve this answer
    
+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
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 <filename> – 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .