简体   繁体   中英

How to print the name of the files that contains “#!bin/bash” on the first line

I tried to print the name of the files that contains #!bin/bash on the first line (the extension doesn't matter) that are in the same file.

I did this which allow me to print the name of the files that contains : #!bin/bash everywhere in the file, but I only need to print the name of the files that contains this chain on the first line.

Here is what I did :

find . -type f -name - grep -L '#!bin/bash' {} \; -printf '%f\n'> file.txt

What about using awk for this?

awk 'NR==1 && ($0 == "#!bin/bash") {print FILENAME} {exit}' file

This checks if the first line has this content. If so, it prints the filename. Then, no matter what happened before, it exits the file since it is not needed to process it anymore.

Using find to provide the files to the awk command, just say find /your/path -exec awk '...' {} \\; such as:

find -type f -exec awk 'NR==1 && ($0 == "#!bin/bash") {print FILENAME} {exit}' {} \;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM