0

I'm currently learning Linux, and some of the practice I've has me absolutely stumped. I'm trying to look for a file in the current directory with particular text, and output its inode number. I can confidently do all these things individually but it keeps falling apart when I try to put them together;

 find. -maxdepth 1 -type f | grep -r "#include" -ls -i 

but for some reason it just outputs the file names?

Any ideas?

2
  • Would find . -maxdepth 1 -type f | grep -r "#include" | ls -i bring you any closer to what you are looking for?
    – arkascha
    Commented Oct 28, 2017 at 8:28
  • if maxdepth is not important, you can just do grep -r to recursively search the directory. If it is, use exec with find as in the 1st answer.
    – ahron
    Commented Sep 8, 2022 at 4:43

3 Answers 3

2
find . -type f -exec grep '#include' {} \; -printf '%p %i\n'

or grep -q to hide grep output

find . -type f -exec grep -q '#include' {} \; -printf '%p %i\n'
1

If you are using grep in recursive mode -r - you don't actually need find command:

grep + xargs approach:

grep -rl '#include' | xargs -I {} ls -i '{}'

An approximate output (<inode> <filename>):

1837827 test/1.pdb
1712970 2_clean.pdb
1837846 test2/2.pdb
1712965 1_clean.pdb
2
  • I assume that grep requires a filename or data via stdin.
    – Cyrus
    Commented Oct 28, 2017 at 9:45
  • @Cyrus, explain your phrase Commented Oct 28, 2017 at 9:46
0

Since your search is limited to the current directory, if you don't care about dot files, you can do this more simply with grep -l, command substitution and a wildcard (*):

ls -i $(grep -ls '#include' *)

Output:

109057734 file1.txt  109056004 file2.txt  5505834 file3.txt

or with older syntax, using backticks instead of $():

ls -i `grep -ls '#include' *`

Note that I added the -s option for grep to silence some warnings about grepping directories.

If you want pretty columns, add the -1 option for ls.

ls -i1 $(grep -ls '#include' *)

Output:

109057734 file1.txt
109056004 file2.txt
  5505834 file3.txt

You must log in to answer this question.