# Grep and ls -lrt

**URL:** <https://community.unix.com/t/grep-and-ls-lrt/391339>\
**Category:** AIX\
**Created:** [June 28, 2023, 1:47pm UTC](https://community.unix.com/t/grep-and-ls-lrt/391339 "2023-06-28T13:47:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [June 28, 2023, 1:47pm UTC](https://community.unix.com/t/grep-and-ls-lrt/391339/1 "2023-06-28T13:47:39Z")

</div>

Hi,  
On AIX , when I run:  
`grep myexpression * -exec ls -lrt {} \;`  
I recive:

```plaintext
grep: 0652-033 Cannot open -exec.
grep: 0652-033 Cannot open ls.
grep: 0652-033 Cannot open -lrt.
grep: 0652-033 Cannot open {}.
grep: 0652-033 Cannot open ;.

```

Any help please?  
Thanks

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [June 28, 2023, 2:16pm UTC](https://community.unix.com/t/grep-and-ls-lrt/391339/2 "2023-06-28T14:16:26Z")

</div>

Hi, grep doesn't have an `-exec.` option.  
A 5 minute read of the grep documentation on your system will help. `man grep` , come back with questions once you've sorted the basics out and we can assist.

Additionally, show expected input and expected results from your intended commands, otherwise we are guessing.

Also, learn how to enclose code/input/output in markdown syntax by using the options menu

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [June 28, 2023, 2:19pm UTC](https://community.unix.com/t/grep-and-ls-lrt/391339/3 "2023-06-28T14:19:02Z")

</div>

Ok, Thank you very much.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [June 28, 2023, 3:47pm UTC](https://community.unix.com/t/grep-and-ls-lrt/391339/4 "2023-06-28T15:47:13Z")

</div>

`-exec ls -lrt {} \;` fits for `find`.  
Perhaps you want

```plaintext
find . -name "*" -type f -exec grep -q myexpression {} \; -exec ls -lrt {} +

```

Find files that contain the word "myexpression" and list them sorted, oldest first.

The second `-exec` is run if the first `-exec` was successful.  
To immediately chain the second `-exec` the first `-exec` ends with a `\;`  
The second `-exec` ends with `+` that queues arguments and finally runs one `ls -ltr` with the queued arguments. Only then the `ls` can sort.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [July 5, 2023, 3:48pm UTC](https://community.unix.com/t/grep-and-ls-lrt/391339/5 "2023-07-05T15:48:09Z")

</div>

Closed on July 5, 2023, 3:48pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
