user3408678
user3408678

Reputation: 155

Find commands don't work in script only

I have a script that searches specific locations for .txt files and outputs the results to stdout, and to a file using the tee command. At least it's supposed to. I'm having some strange issues with it however. Here's the code:

echo -e "${HIGHLIGHT}Sensitive files:${OFF}"
echo "## Sensitive files:" >> $ofile
for file in $(cat $1); do ls -lh $file 2>/dev/null; done | tee -a $ofile
echo " " | tee -a $ofile
echo -e "${HIGHLIGHT}Suids:${OFF}"
echo "## Suids:" >> $ofile
find / -type f \( -perm -04000 -o -perm -02000 \) -exec ls -Alh {} \; 2>/dev/null | tee -a $ofile
echo " " | tee -a $ofile
echo -e "${HIGHLIGHT}Owned by root only:${OFF}"
echo "## Owned by root only:" >> $ofile
find / -type f -user root \( -perm -04000 -o -perm -02000 \) -exec ls -lg {} \; 2>/dev/null | tee -a $ofile
echo " " | tee -a $ofile

# Text files
echo -e "${HIGHLIGHT}Text files:${OFF}"
echo "## Text files:" >> $ofile
find /etc -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
find /home -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile
find /root -type f -name *.txt -exec ls -lh {} \; 2>/dev/null | tee -a $ofile

The strange thing is that all of the commands work just fine, except for the find searches for .txt files at the bottom. None of those commands work in the script, yet if I copy and paste them into the terminal and run it exactly as they were in the script, they work just fine. How is this even possible?

Upvotes: 1

Views: 102

Answers (1)

Eric Renouf
Eric Renouf

Reputation: 14490

You need to quote or escape the * in your -name patterns, otherwise the shell tries to expand it and use the expanded form in its place in the command line.

find /etc -type f -name '*.txt' -exec ls -lh {} \; 2>/dev/null | tee -a $ofile

and the others being similar will work

Upvotes: 3

Related Questions