Create automated scan of specific directory using bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create automated scan of specific directory using bash
# 1  
Old 01-06-2017
Create automated scan of specific directory using bash

I am trying to use bash to automate the scan of a specific directory using clamav. Having this in place is a network requirement. The below is an attempt to:

1. count the extensions (.txt, .jpeg) in a directory and write them to a virus-scan.log (section in bold)

2. scan each folder in the dir and log the results of the scan by date. (section in italics)

Each folder in the directory is scanned and the results of each day the scan is run is logged to /HOME/virus-scan.log by date.

Thank you Smilie.

Code:
#!/bin/bash
DIR=/home/cmccabe/Desktop/NGS/API
line_no=$(ls | awk -F . '{print $NF}' | sort | uniq -c | awk '{print $2,$1}') # count folder type and store as variable
echo >> $HOME/virus-scan.log"The folders detected are:
$line_no"

# Get rid of old log file
rm $HOME/virus-scan.log 2> /dev/null
 
while read FILE in $DIR;
do
     # check file length is nonzero otherwise commands may be repeated
     if [ -s $FILE ]; then
          date > $HOME/virus-scan.log
          clamscan -r $FILE >> $HOME/virus-scan.log
     else "echo nothing detected by scan"
     fi
done


Last edited by cmccabe; 01-06-2017 at 12:10 PM.. Reason: fixed format
# 2  
Old 01-06-2017
So... What's your question?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-06-2017
Sorry, the script runs but just halts and nothing happens and I'm not sure whats wrong or if this is the best way. Thank you Smilie.

Last edited by cmccabe; 01-06-2017 at 01:06 PM.. Reason: added details
# 4  
Old 01-06-2017
Try for FILE in $DIR instead of while read FILE in $DIR , read actually tries to read from standard input (in this context, your keyboard).
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-06-2017
Thank you very much, that worked perfectly Smilie. The explanation is appreciated Smilie
# 6  
Old 01-06-2017
Yes the for loop is best to cycle through a list.
But $DIR is only one item.
Maybe you want $DIR/* aka all items in $DIR ?
This might do what you intend
Code:
#!/bin/bash
DIR=/home/cmccabe/Desktop/NGS/API
log=$HOME/virus-scan.log

{
echo "The extensions are"
ls | awk -F'\.' 'NF>1 {ext[$NF]++} END {for (i in ext) print ext[i],i}'
} > $log

scanned=0
for FILE in "$DIR"/*
do
     # check file length is nonzero otherwise commands may be repeated
     if [ -s "$FILE" ]; then
          {
          date
          clamscan -r "$FILE"
          } >> $log
          ((scanned++))
     fi
done
[ $scanned -eq 0 ] && echo "nothing detected by scan" >> $log

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 01-06-2017
On linux systems clamav implements a system to watch directories using inotify subsystem.

You might want to check that out :
http://blog.clamav.net/2016/03/confi...in-clamav.html
This User Gave Thanks to Peasant For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to check directory and create missing folder from file

In the below bash I am trying to ensure that all folders (represented by $folders) in a given directory are created. In the file f1 the trimmed folder will be there somewhere (will be multiple trimmed folders). When that trimmed folder is found (represented by $S5) the the contents of $2 printed... (19 Replies)
Discussion started by: cmccabe
19 Replies

2. Shell Programming and Scripting

Create directory and sub-directory with awk and bash

In the below I am trying to create a parent directory using the R_2019 line from f1 if what above it is not empty. I then create sub-directories under each parent if there is a match between $2 of f1 and $2. Inside each sub-folder the matching paths in $3 and $4 in f2are printed. If there is no... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Bash directory loop, but only choose those folders with specific word in it

Hello, how in bash i can get directory loop, but only choose those folders with specific word in it, so it will only echo those with specific word #!/bin/bash for filename in /home/test/* do if ; then echo $filename; fithx! (4 Replies)
Discussion started by: ZerO13
4 Replies

5. Shell Programming and Scripting

Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the... (5 Replies)
Discussion started by: cmccabe
5 Replies

6. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

Bash to create new directory by date followed by identifier and additional subdirectories

I have a bash that downloads a list and if that list has data in it then a new main directory is created (with the date) with several subdirectories (example1, example2, example3). My question is in that list there are portion of specific file types (.vcf.gz) - identifier towards the end that have... (0 Replies)
Discussion started by: cmccabe
0 Replies

8. Solaris

Limit bash/sh user's access to a specific directory

Hello Team, I have Solaris 10 u6 I have a user test1 using bash that belong to the group staff. I would like to restrict this user to navigate only in his home directory and his subfolders but not not move out to other directories. How can I do it ? Thanks in advance (1 Reply)
Discussion started by: csierra
1 Replies

9. Shell Programming and Scripting

Create a specific bash to increase the automatic update on my pi

hi everyone, I just began to be interested about the bash access. I buy a time ago a Raspberry pi, I installed raspbmc and now I would like build a bash to copy everyday all new files inside my server directly on the hard drive in my Pi. So my HDD is directly plug on my pi with usb connect,... (4 Replies)
Discussion started by: nagito34
4 Replies

10. Solaris

create user with RWX access to a specific directory in Solaris 10

I need to create a user account for a developer that will allow him rwx access to all resources in a directory. How can I do that? Thanks (5 Replies)
Discussion started by: gsander
5 Replies
Login or Register to Ask a Question