I have a file that contains directory names:
my_list.txt
:
/tmp /var/tmp
I'd like to check in Bash before I'll add a directory name if that name already exists in the file.
I have a file that contains directory names:
my_list.txt
:
/tmp /var/tmp
I'd like to check in Bash before I'll add a directory name if that name already exists in the file.
grep -Fxq "$FILENAME" my_list.txt
The exit status is 0 (true) if the name was found, 1 (false) if not, so:
if grep -Fxq "$FILENAME" my_list.txt then # code if found else # code if not found fi
Here are the relevant sections of the man page for grep
:
grep [options] PATTERN [FILE...] -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by new- lines, any of which is to be matched. -x, --line-regexp Select only those matches that exactly match the whole line. -q, --quiet, --silent Quiet; do not write anything to standard output. Exit immedi- ately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.
Regarding the following solution:
grep -Fxq "$FILENAME" my_list.txt
In case you are wondering (as I did) what -Fxq
means in plain English F
affects how PATTERN is interpreted (fixed string instead of a regex), x
match whole line, q
shhhhh... minimal printing
From the man file:
-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) -x, --line-regexp Select only those matches that exactly match the whole line. (-x is specified by POSIX.) -q, --quiet, --silent Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX.)
Three methods in my mind:
1) Short test for a name in a path (I'm not sure this might be your case)
ls -a "path" | grep "name"
2) Short test for a string in a file
grep -R "string" "filepath"
3) Longer bash script using regex:
#!/bin/bash declare file="content.txt" declare regex="\s+string\s+" declare file_content=$( cat "${file}" ) if [[ " $file_content " =~ $regex ]] # please note the space before and after the file content then echo "found" else echo "not found" fi exit
This should be quicker if you have to test multiple string on a file content using a loop for example changing the regex at any cicle.
Simpler way:
if grep "$filename" my_list.txt > /dev/null then ... found else ... not found fi
Tip: send to /dev/null
if you want command's exit status, but not outputs.
If I understood your question correctly, this should do what you need.
In one line: check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt
If you just want to check the existence of one line, you do not need to create a file. E.g.,
if grep -xq "LINE_TO_BE_MATCHED" FILE_TO_LOOK_IN ; then # code for if it exists else # code for if it does not exist fi
My version using fgrep
FOUND=`fgrep -c "FOUND" $VALIDATION_FILE` if [ $FOUND -eq 0 ]; then echo "Not able to find" else echo "able to find" fi
Easiest and simplest way would be:
isInFile=$(cat file.txt | grep -c "string") if [ $isInFile -eq 0 ]; then #string not contained in file else #string is in file at least once fi
grep -c will return the count of how many times the string occurs in the file.
grep -E "(string)" /path/to/file || echo "no match found"
-E option makes grep use regular expressions
A grep-less solution, works for me:
MY_LIST=$( cat /path/to/my_list.txt ) if [[ "${MY_LIST}" == *"${NEW_DIRECTORY_NAME}"* ]]; then echo "It's there!" else echo "its not there" fi
if grep -q "$Filename$" my_list.txt then echo "exist" else echo "not exist" fi