-1

Currently I'm doing this to try and store the actual file and its contents the find command finds:

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 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 keyword, then perform a scan on that original file.

  • 1
    use `-l` option to list only filenames that match the given search term – Sundeep Jun 01 '21 at 14:46
  • Does this answer your question? [How can I use grep to show just filenames on Linux?](https://stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-on-linux) – Sundeep Jun 01 '21 at 14:47
  • I don't think that answers the question. I tried using both the -l flag and -rl flag and they produced the same result as I have described in my question. I will update with an image to demonstrate if possible – i'i'i'i'i'i'i'i'i'i Jun 01 '21 at 14:55
  • In your image, note that you wrote `$file.ps1` instead of `file1.ps1` for both the `echo` and the `cat` command. Also note that in your `-lr` example, your regular expression is trying to match the asterisk character (`*`) before `myfile` - is that what you intended? – Thomas Jun 01 '21 at 15:14
  • If I do echo file1.ps1 it literally writes 'file1.ps1' to the console. – i'i'i'i'i'i'i'i'i'i Jun 01 '21 at 15:20
  • That's what it should do: `echo` is a command that prints its arguments. You should run `cat file1.ps1` to get the contents of `file1.ps1`. – Thomas Jun 01 '21 at 15:22
  • Btw, it might be a good idea not to use the same file extension for the output file as you use in your `grep` command, or else the output file will also be searched next time you run the same command. – Thomas Jun 01 '21 at 15:28
  • I've read this a few times and still can't figure if when you say `store the file` you mean `store the name of the file` or `store the contents of the file`, nor can I figure out what it is you want to do with either of them. Please [edit] your question to include a [mcve] with concise, testable sample input and expected output. – Ed Morton Jun 01 '21 at 20:12
  • From the pink image you posted you clearly have a variable named `file` that contains the string `try.ps1 try{` but where it came from, how it's related to your `grep` command line and what you want to do with it is a mystery. – Ed Morton Jun 01 '21 at 20:17
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Jun 02 '21 at 04:23
  • If you want to find the files whose names contain "keyword", that would be `echo *keyword*` where the wildcards get expanded by the shell to only print the names of matching files. (Beginners would probably use `ls *keyword*` but [don't use `ls` in scripts.](https://mywiki.wooledge.org/ParsingLs)) – tripleee Jun 02 '21 at 04:35
  • @EdMorton I mean store the contents of the file - I wouldn't be able to perform static analysis on a file name. My particular use case is to perform static analysis of the file, which is what I state I want to do in the question - do you not understand that sentence? I am using a tool which requires a specific file as an argument. How do I pass this file and its contents as an argument after having found it with grep? – i'i'i'i'i'i'i'i'i'i Jun 02 '21 at 08:43
  • I have now edited the question to provide more clarity. – i'i'i'i'i'i'i'i'i'i Jun 02 '21 at 11:00
  • 1
    @i'i'i'i'i'i'i'i'i'i you can perform analysis of the contents of a file by storing the file name or the file contents as a starting point. Your question is **much** clearer now. Just stop saying things like `How do I pass this file and its contents` as then it gets muddy again because it's not clear what "file **and** its contents" means - it sounds like you want to pass the name of the file **and** the contents of the file to something. If you make sure to always say `file name` or `file contents` then your intent will always be clear. – Ed Morton Jun 02 '21 at 11:31
  • `find` by default, and as clearly stated in the man page, outputs the path to each file it finds so it's not clear why you'd expect it to output the contents of the files it finds instead of their names. You have several ways to get the contents of the files, including `find /Users/Documents -name "Myfile" -exec cat {} + > outputfile` or `find /Users/Documents -name "Myfile" > outputfile; < outputfile xargs cat` and you may want to use some other command name directly instead of `cat | command` or whatever you have in mind. – Ed Morton Jun 02 '21 at 11:35

1 Answers1

1

Let's assume I have three files in my directory, called a.txt, b.txt, and c.txt with the following contents:

$ for file in *.txt; do echo "[$file]"; cat $file; done
[a.txt]
foo
[b.txt]
bar
[c.txt]
foobar

If I'm interested in all files that contain, say, the string oo, this will do the trick:

$ grep -l "oo" *.txt > oo.out
$ cat oo.out
a.txt
c.txt

If I want the files that contain ba, it's the same:

$ grep -l "ba" *.txt > ba.out
$ cat ba.out
b.txt
c.txt
Thomas
  • 17,016
  • 4
  • 46
  • 70
  • Thanks for that, I will have a further think about it, although the two examples miss the point, when `cat ba.out` and `cat oo.out` - I don't care about which files include this, I care about the actual contents of the file which is named a certain way. So I guess from your example, the only way to do this is through a for loop. – i'i'i'i'i'i'i'i'i'i Jun 01 '21 at 16:41
  • So, okay, I'm still trying to understand your original question in the light of your comment. Is this correct: what you want is to find files that contain a certain regular expression, collect them somehow, and then in a second step run another command on each of them? – Thomas Jun 01 '21 at 19:00
  • @i'i'i'i'i'i'i'i'i'i with respect to `the only way to do this is through a for loop` - probably not but so far it's still not clear what the "this" is you're trying to do. – Ed Morton Jun 01 '21 at 20:14