2

In many cases after I find a file using the find command I then want to open the file or cat it or maybe print it. How can I operate on the result from find? For example,

: find . -name "myfile.txt"
./docs/myfile.txt

: find . -name "myfile.txt" | less

does not work because it feeds the string "./docs/myfile.txt" to less, not the contents of the file at the specified path.

  • Do any of the 4 answers solve your problem? Don't forget to use the checkmark to indicate, if so. – Jeff Schaller Jun 2 '16 at 17:43
2

Similar to @coffeeMug, this is the more up-to-date way to doing this as it is apparently faster:

find . -name "*.log" -exec ls -l '{}' +

I'll also point you to CommandLineFu, which is always helpful with these things.

2

less $(find . -name myfile.txt)

less `find . -name myfile.txt`

The first is, I believe, both POSIX-compliant and nest-able. The second, I believe, is more portable.

2

You can achieve this using find's -exec flag:

find . -name "*.log" -exec ls -l '{}' \;

In this example find searches for all log files in current directory and then list them using ls -l. In your case you should replace ls with less. See the ACTION part of find man page here find(1) man page.

0

There's no way to automatically grab the output of the previous command (not under any usual environment). But if you manually select the output with the mouse, then you can run

less `xsel`

or, if the output contains spaces or wildcard characters and you aren't running zsh,

less "`xsel`"

If you don't mind running the command again, press Up then add less ` at the beginning of the line and ` at the end (or less "` at the beginning of the line and `" to cope with spaces in bash or ksh).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.