Using cat and grep to print out the names of files that contain the "password:" string within the current directory:
cat * 2>&- | grep -ls 'password:' *
With full file path:
cat * 2>&- | grep -ls 'password:' ${PWD}/*
2>&- is used to suppress error messages about being unable to read certain files/directories.
Using only grep to do the same thing:
grep -ls 'password:' *
With full file path:
grep -ls 'password:' ${PWD}/*
Using grep to pattern match for the "password:" string, printing the filename, then using cat to display the entire file(s) contents:
for match in $(grep -ls 'password:' *) ; do
echo "${match}"
cat "${match}"
done
With full file path:
for match in $(grep -ls 'password:' *) ; do
echo -e "\e[7m\n$(readlink -f "${match}")\e[0m"
cat "${match}"
done
catwhen you cangrepdirectly? You can usegrep -R "pattern" .(-Rwould search for pattern recursively and.refers to current directory). If there's some other reason to usecat, please [edit] your question and explain that. – Kulfy Jul 22 '20 at 19:18grep password *will - by default - print the names of files in which matches are found if `matches multiple files*. If you want it to print filenames unconditionally, add the-H(or long-form--with-filename`) option – steeldriver Jul 22 '20 at 20:59