0

I am getting GREP invalid option error in the code below :

file=$(find . -mtime -4 |ls -lt)
for f in $file
do
 po=$(echo $f|cut -d"_" -f2)
 find . -mtime -4 |ls -lt|grep "$po"|while read fn
   do
         if [ -s $fn ]; then #checks if the file is not empty
           if [ -d tmp ]; then
                rm -r tmp
           fi
           mkdir tmp
           cp -p $fn /tmp/$fn
           break
         fi
   done
done

Basically I am trying to sort the list which I am getting from find then looping through it taking the latest non zero file for a PO.

List of file is

-rw-rw-r-- 1 loneranger loneranger 37 Jul 21 06:30 belk_po12345_20140721.log
-rw-rw-r-- 1 loneranger loneranger 24 Jul 22 06:30 belk_po12345_20140722.log
-rw-rw-r-- 1 loneranger loneranger  0 Jul 23 06:30 belk_po12345_20140723.log
-rw-rw-r-- 1 loneranger loneranger 11 Jul 24 12:00 belk_po12348_20140723.log

PO - po12345 or po12348 these are...

3
  • 1
    The find . -mtime -4 |ls -lt is not valid construction. Commented Jul 24, 2014 at 21:49
  • @Asfakul,Why you are using this command file=$(find . -mtime -4 |ls -lt) Commented Jul 25, 2014 at 7:51
  • I am using this for sorting list which I am getting out of find command. I have changed it to find . -mtime -4 |xargs ls -lt Commented Jul 25, 2014 at 8:03

1 Answer 1

1

Basically I am trying to sort the list which I am getting from find then looping through it taking the latest non zero file for a PO.

You might use find for all of that except the final sort:

find . -size '+1c' -type f -printf "%f %T@\n" | sort -k2

The find part search files (-type f) of more than 1 byte long (-size '+1c') and for each one print the file's base name (%f) and the modified time as seconds since Jan. 1, 1970, 00:00 GMT (%T@). After that, it is a simple sort on the second field (timestamp).

Of course, you might add all the search criterion you need on find but that's the basic idea.

And if you want to loop over the result, do as usual:

find . -size '+1c' -type f -printf "%f %T@\n" | 
    sort -k2 |
    while read fname mts; do
       # ...
    done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.