CrazedNerd
Well-Known Member
Here is the best script i have made, and i use it on a pretty regular basis, but for some reason it does not work properly in my home folder, i even put a message at top to point that out:
When i use this script at the bottom of my home directory, for some reason it just prints out all of the files located in the home directory. When I use it in any directory (like, for example, i have a directory just for educational scripts that don't have any practical value, and also Documents) then it does what it's supposed to do, and just prints the file and file types of text files that contain the string. It has a lot of other aspects, but the important part of this is:
running grep by itself like that with a typed regular expression has the same issue.
Code:
#!/bin/bash
#this will look through every file in a working directory and
#find the text files that contain a string
echo -e "Enter string you want to find in your files. This script only searches files"
read -p "in your working directory. Don't use directly from home directory: " term
echo -e "\nUN-HIDDEN FILES\n"
FILES="$(grep -lIs "$term" *)"
#options mean show files containing text of file, omit binary files,
#omit error messages.
#file takes grep output to display their type,
#then sed removes error messages when files have spaces
file $FILES | sed '/No such file or directory/d'
FILESH="$(grep -lIs "$term" .*)"
echo -e "\nHIDDEN FILES\n"
file $FILESH | sed '/No such file or directory/d;'
echo -e "\nFind and grep can't process these files because they have spaces in them:\n"
#sed removes directories and their listings by quitting sed when it finds
#the first directory listing.
ls *' '* | sed -n '/:$/q;p;'
echo -e "\nIf files has returned \"Usage:\", then the search has come back with no results"
echo -e "for that type of file."
When i use this script at the bottom of my home directory, for some reason it just prints out all of the files located in the home directory. When I use it in any directory (like, for example, i have a directory just for educational scripts that don't have any practical value, and also Documents) then it does what it's supposed to do, and just prints the file and file types of text files that contain the string. It has a lot of other aspects, but the important part of this is:
Code:
grep -lIs "$term" *
running grep by itself like that with a typed regular expression has the same issue.
Last edited: