I have a working find query find . -type f | gshuf -n2
find . -type f | gshuf -n2
find . -type f | gshuf -n2
which returns two lines of files. I know I can open files in text edit through open -a TextEdit ./text.txt
but how would I put these together so Textedit opens them after finding instead of me manually copying and pasting?
I tried variations like
open -a TextEdit find . -type f | gshuf -n2
open -a TextEdit (find . -type f | gshuf -n2)
etc
Can this be done in one command or if not what's the best way?
You were close with your second attempt. To substitute the output of a command into a command line, you use $( command )
.
open -a TextEdit $(find . -type f | gshuf -n2)
However, this won't work properly if any of the filenames have spaces in their names, because each word will be treated as a separate filename. This will work better:
find . -type f | gshuf -n2 | while read filename; do
open -a TextEdit "$filename"
done
The double quotes prevent the name from being split up.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.