How can I store the file that find produces i.e the actual file and contents as a file and not the console output of find?
Currently I’m doing this to try and store the actual file and its contents the find
command finds as I’m searching for a file named Myfile
:
find /Users/Documents -name Myfile > outputfile
The contents of Myfile
is:
This is my original file.
however when I go to perform an operation, such as cat outputfile
it takes the actual output of the find
command,rather than the file itself and displays:
/Users/Documents/Myfile
I am expecting when I do cat outputfile
:
This is my original file.
How can I store the file itself and perform operations on it? My particular use case is to perform static analysis of the file and I need to pass it as an argument. I have recently found using find
. I don’t care for displaying the output of the command. I want to find a file based on the name of the file, then perform a scan and work with the contents of that original file, by storing it as a variable. The reason I want to store it as a variable is so that I can pass it as an argument to another program.
Answer
To store the path to the file in a variable, use:
fil=$(find /Users/Documents -name Myfile)
To actually redirect the output of the found file(s) use the exec flag in find and so:
find /Users/Documents -name Myfile -exec cat '{}' ; > outputfile
If you want to store the output in a variable as opposed to redirect to a file you can use:
conts=$(find /Users/Documents -name Myfile -exec cat '{}' ;)