Hi There
Hi There

Reputation: 187

Pasting into a file cleanly using Bash

I'm trying to paste an output of a bash script to a text file, but I want to have only the results instead of the whole writings; this is my current code:

#!/usr/bin/bash
result=$(grep -r -i --include=\*.exe ./ > output.txt)
result

but what I get in the text file is Binary file ... matches .. Whereas all I want is the names of the files with .exe extension alone each on a different line. Is there a way to do that?

Upvotes: 1

Views: 149

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133770

EDIT: In case you only want to print file names while searching specific formats then following may help you.

find -type f -iname "*.exe" -printf "%f\n"

As per OP, OP wants to take this command in a variable and print the file names into new lines so for that use following.

var=$(find -type f -iname "*.exe" -printf "%f\n")
echo "$var"

Could you please try find command which could use grep in it for looking for a specific string into file of specific type.

Solution 1st: Simple find to look for specific string into the .exe files.

find -type f -iname "*.exe" -exec grep -l "test_my_text" {} \+

Solution 2nd: Use find to all kind of files.

find -type f -exec grep -l "test_my_text" {} \+

Upvotes: 1

asklc
asklc

Reputation: 495

grep is not the right command for what you want to achieve. What you want is consulting the find command for such use cases. Try it like this:

find . -type f -name "*.exe"

Here, -type f asks specifically for files (-type d would give you directories respectively). You should get your desired output by wrapping it with $(...) for further refinement.

Upvotes: 0

Related Questions