File monitoring Bash script - linux

I created this script for check whether specific files exist or not in the given location. but when I run this its always showing
Failed - Flag_lms_device_info_20160628.txt do not exist
Failed - Flag_lms_weekly_usage_info_20160628 do not exist
but both files are existing.
PREFIX="/opt/data"
REPORT="/tmp/report.txt"
DATE=$( date -d "${dtd} -1 days" +'%Y%m%d' )
rm -f "$REPORT"
FILENAME="Flag_lms_device_info_${DATE}.txt"
FULLFN="$PREFIX/$FILENAME"
if [ -f "$FULLFN" ]; then
echo "OK - $FILENAME exists" >> $REPORT
else
echo "Failed - $FILENAME do not exist" >> $REPORT
fi
FILENAME="Flag_lms_weekly_usage_info_${DATE}.txt"
FULLFN="$PREFIX/$FILENAME"
if [ -f "$FULLFN" ]; then
echo "OK - $FILENAME exists" >> $REPORT
else
echo "Failed - $FILENAME do not exist" >> $REPORT
fi

First of all, you have strange output in your question: your second line of output lacks a .txt extension. This might be an accident but if it's not it's worth investigating.
Assuming your date command is working correctly (I don't know that particular command), I would reduce your use of variables. In addition, I would use the -e test operator instead of -f because it's more inclusive. (If you haven't put data in the files yet, -f could return an error even if the file exists.) :
REPORT="/tmp/report.txt"
DATE=$( date -d "${dtd} -1 days" +'%Y%m%d' )
echo "" > "$REPORT" # Wipes file instead of completely removing it
filename="/opt/data/Flag_lms_device_info_$DATE.txt"
if [ -e "$filename" ]; then
echo "OK - Flag_lms_device_info_$DATE.txt exists" >> $REPORT
else
echo "Failed - Flag_lms_device_info_$DATE.txt doesn't exist" >> $REPORT
fi
filename="/opt/data/Flag_lms_weekly_usage_info_$DATE.txt"
if [ -e "$filename" ]; then
echo "OK - Flag_lms_weekly_usage_info_$DATE.txt exists" >> $REPORT
else
echo "Failed - Flag_lms_weekly_usage_info_$DATE.txt doesn't exist" >> $REPORT
fi

if [ -f "find "$FULLFN" -type f -name "$FILENAME"" ];then
Here you check for existance of a strange file named find... Use backquotes
if [ -f `find "$FULLFN" -type f -name "$FILENAME"` ];then
or, in bash,
if [ -f $(find "$FULLFN" -type f -name "$FILENAME") ];then
to get the command's output as a string.
Furthermore, your find invocation does not look promising. If you need to find a file named Flag_lms_device and so forth somewhere under /opt/data/, use find "$PREFIX" -type f -name "$FILENAME". If you know for sure that /opt/data is the exact location, then use
if [ -f "$FULLFN" ]
and you don't need to find the file.

Related

Backup script in bash

I need to write a script that will look for all files with the suffix ~ (eg file.txt ~) in current directory. If the script will find something, it should be copied to BACKUP directory.
If the BACKUP directory does not exist, the script should create it. If there is already a file (or other non-directory) named BACKUP, the script should report an error.
The problem is that on line if [ $x -eq BACKUP.* ];. Bash shows if [ $x -eq BACKUP.* ];
Appreciate any help
#!/bin/bash
if [ ! -d BACKUP ];
then
mkdir BACKUP;
fi
for x in *. *~ ; do
if [ $x -eq BACKUP.* ];
then
echo "Error, file BACKUP exist";
else
cp ./$x ./BACKUP;
fi
done
You mean something like that?
#!/bin/bash
BACKUP=./BACKUP
if [[ -e "$BACKUP" ]]; then
echo "$BACKUP already exists!" >&2
exit 1
fi
mkdir "$BACKUP"
find . -maxdepth 1 -type f -name "*~" -exec cp {} "$BACKUP" \;

Bash script for monitor files [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
scenario is -
In the Location /opt/data/ there are 10 difference files with name ending with previous day
eg - file_1_20160627
I need to check whether these 10 files exist or not.
If one exists, I need to show output - "OK - file_1_20160627 exist" and write output in /tmp/report.txt file
If a file does not exist, I want the same as above - "Failed - file_1_20160627 not exist" and write output in the same /tmp/report.txt file
Every day when the script runs, content on that file must replace.
I tried to write but I'm not good in scripting. below script only for 4 files. I think so many things need to be change.
appreciate someone help me to create this script.
#!/bin/bash
now=`date +%Y%m%d%H%M%S`
time=`date +%H%M`
week=`date +%a`
/bin/rm -f /tmp/report.txt
if [ "$time" -ge 1300 ] && [ $time -lt 2359 ]; then
if [ "$week" == Sun ]; then
if [ -f "find /opt/data/ -type f -name "file_1_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_1 file does exist" >> /tmp/report.txt
else
echo "Failed - file_1 file does not exist." >> /tmp/report.txt
fi
if [ -f "find /opt/data/ -type f -name "file_2_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_2 file exist." >> /tmp/report.txt
else
echo "Failed - file_2 file does not exist" >> /tmp/report.txt
fi
else
fi
if [ -f "find /opt/data/ -type f -name "file_3_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_3 file exist" >> /tmp/report.txt
else
echo "Failed - file_3 file does not exist" >> /tmp/report.txt
fi
if [ -f "find /opt/data/ -type f -name "file_4_`date -d "1 day ago" +%Y%m%d`.txt"" ];
then
echo "OK - file_4 file exist" >> /tmp/report.txt
else
echo "Failed - file_4 file does not exist" >> /tmp/report.txt
fi
else
fi
PREFIX="/opt/data"
REPORT="/tmp/report.txt"
DATE=$( date +%Y%m%d )
rm "$REPORT"
for i in `seq 1 10`;
do
FILENAME="file_${i}_${DATE}"
FULLFN="$PREFIX/$FILENAME"
if [ -f "$FULLFN" ]; then
echo "OK - $FILENAME exists" >> $REPORT
else
echo "Failed - $FILENAME do not exist" >> $REPORT
fi
done

Find using file for folder locations Linux Bash

I am trying to use a txt file to store folder locations to use in find command. But keep getting folder not found works with only one folder location in file
with "$addfolder"
found=$(find "$addfolder" ! -path "*/.bak/*" -type f -iname "*$ffind*" | sort)
and replacing \"
addfolder="$addfolder $Folder"
folder.txt :-
Main/Public
Main/General
Not Used
Old Backup Files
#!/bin/bash
addfolder=""
filename="Settings/folders.txt"
#Read Folder.txt for locations
while read -r Folder; do
if [ ! "$Folder" == "" ];then
if [ -d "$Folder" ]; then
addfolder="$addfolder \"$Folder\""
echo "$addfolder"
fi
fi
done < "$filename"
if [ "$addfolder" == "" ]; then
exit
fi
echo -e "\e[36mEnter Filename To Find :-\e[0m"
read -p "" ffind
echo -e "\e[92mSearching:\e[0m"
found=$(find $addfolder ! -path "*/.bak/*" -type f -iname "*$ffind*" | sort)
echo -e "\e[33m$found\e[0m"
echo "Press Enter To Exit"
read -s -n 1 -p ""
Regular variables should only hold single strings.
To hold lists of strings, use an array:
#!/bin/bash
addfolder=()
filename="Settings/folders.txt"
#Read Folder.txt for locations
while IFS= read -r Folder; do
if [ ! "$Folder" == "" ];then
if [ -d "$Folder" ]; then
addfolder+=( "$Folder" )
echo "${addfolder[#]}"
fi
fi
done < "$filename"
if [ "${#addfolder[#]}" == 0 ]; then
exit
fi
echo -e "\e[36mEnter Filename To Find :-\e[0m"
read -p "" ffind
echo -e "\e[92mSearching:\e[0m"
found=$(find "${addfolder[#]}" ! -path "*/.bak/*" -type f -iname "*$ffind*" | sort)
echo -e "\e[33m$found\e[0m"
echo "Press Enter To Exit"
read -s -n 1 -p ""

errors running sh script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I keep getting these errors running my script and i just cannot work it out...
the error that keeps coming up is;
rm: cannot remove ~/my-documents/article:': Is a directory. The directory its referring to is $2...here is my script.
#! /bin/sh
SRC=$1
DES=$2
if [ $# -ne 2 ]; then
echo "1. Please enter the source directory"
echo "2. Please enter the destination directory"
echo "thankyou"
exit
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory please enter a valid directory"
echo "thankyou"
exit
fi
#gives the user a error warning the source directory is invalid
if [ -d $2 ]; then
echo "output directory exists"
else
echo "Output directory does not exist, creating directory"
mkdir $2
fi
#creates the destination directory if one doesn't exist
IFILE=$GETFILES;
FINDFILE=$FINDFILE;
find $1 -name "*.doc" > FINDFILE
find $1 -name "*.pdf" > FINDFILE
find $1 -name "*.PDF" > FINDFILE
#finds doc, pdf & PDF files and sends data to findfile.
while read -r line;
do
cp $line $2
done < FINDFILE
#files read and copied to destination directory
IFILE=$2/$GETFILES;
ls -R $1 | egrep -i ".doc | .pdf" > IFILE;
LCOUNT=0
DIFFCOUNT=0
FOUND=0
ARCHIVE=1
BASE="${line%.*}"
EXTENSION="${line##*.}"
COUNT=$COUNT;
ls $2 | grep ${line%%.*} \; | wc -l
if [[ $COUNT -eq 0 ]];
then
cp $1/$line $2;
else
echo "there is already a file in the output so need to compare"
COMP=$2/$line
fi
while [[ $FOUND -eq 0 ]] && [[ $LCOUNT -lt $COUNT ]];
do
echo "diffcount is $DIFFCOUNT"
###compares the file from the input directory to the file in
###the output directory
if [ $DIFFCOUNT -eq 0 ];
then
echo "file has already been archived no action required"
FOUND=$FOUND [ $FOUND+1 ]
else
LCOUNT=$LCOUNT [ $LCOUNT+1 ]
COMP="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION"
echo "line count for next compare is $LCOUNT"
echo "get the next file to compare"
echo "the comparison file is now $COMP"
fi
if [ $LCOUNT -ne $COUNT ]; then
ARCHIVE=$ [ $ARCHIVE+1 ]
else
ARCHIVE=0
fi
if [ $ARCHIVE -eq 0 ];
then
NEWOUT="OUT"/"$BASE"_"$LCOUNT"."$EXTENSION";
echo "newfile name is $NEWOUT"
cp $1/$LINE $NEWOUT
fi
done < $IFILE
rm $IFILE
OFILE=$2/DOCFILES;
ls $2 | grep ".doc" > $OFILE;
while read -r line;
do
BASE=${line%.*}
EXTENSION=${line##*.}
NEWEXTENSION=".pdf"
SEARCHFILE=$BASE$NEWEXTENSION
find $2 -name "$SEARCHFILE" -exec {} \;
done < $OFILE
rm $OFILE
### this will then remove any duplicate files so only
### individual .doc .pdf files will exist
a plain call to rm can only remove files, not directories.
$ touch /tmp/myfile
$ rm /tmp/myfile
$ mkdir /tmp/mydir
$ rm /tmp/mydir
rm: cannot remove ‘/tmp/mydir/’: Is a directory
You can remove directories by specifying the -d (to delete empty directories) or the -r (to delete directories and content recursively) flag:
$ mkdir /tmp/mydir
$ rm -r /tmp/mydir
$
this is well described in man rm.
apart from that, you seem to ignore quoting:
$ rm $OFILE
might break badly if the value of OFILE contains spaces, use quotes instead:
$ rm "${OFILE}"
and never parse the output of ls:
ls $2 | grep ".doc" > $OFILE
(e.g. if your "$2" is actually "/home/foo/my.doc.files/" it will put all files in this directory into $OFILE).
and then you iterate over the contents of this file?
instead, just use loop with file-globbing:
for o in "${2}"/*.doc
do
## loop code in here
done
or just do the filtering with find (and don't forget to call an executable with -exex):
find "$2" -name "$SEARCHFILE" -mindepth 1 -maxdepth 1 -type f -exec convertfile \{\} \;

script what to do if ls -lrtd returns file or directory does not exists

This is a part of a script I'm trying to write.
If the directory exist there is no problem, but I don't know what to do if it not exists.
Some one has an idea how to solve this.
Thanks
do echo "$number"
newdir="../FILE-ID/*/${number:2:1}${number:1:1}/+33$number"
nbrdir=$(ls -lrtd $newdir|wc -l)
echo "$nbrdir"
if [ "$nbrdir" -gt 1 ]; then
echo "$number"
echo "error 1.greater"
fi
if [ "$nbrdir" -eq 1 ]; then
echo " equal 1"
else
echo "equal 0"
fi
done
You can use the following to test whether a file (including a directory) exists:
if [[ -f ${newdir} ]]; then ...
or another switch if you want to test specifically just for a directory:
if [[ -d ${newdir} ]]; then ...
A more comprehensive list can be found here.
If your directory name contains wildcards and matches potentially zero or more, you can use:
nbrdir=$(ls -1d ${newdir} 2>/dev/null | wc -l)
This should give you a count of matching directories. Note that any error message which might be generated will be directed to /dev/null which is probably what you want here to avoid errors messing up your output.
[ -d $newdir ] && echo "Directory exists" || echo "Directory does not exists"

Resources