Solved

Need help with BASH script to check existence of file and use an exclude list

Posted on 2010-09-22
9
468 Views
Last Modified: 2013-12-16
I'd like to create a Bash Script that will allow me to check the existence of a file in a directory and touch that file if it doesn't exist.  At the same time, I would like an exclusion list that will exclude certain directories from having that file touched.  Please see what I have so far.  I'm stuck on the excludes file:

#!/bin/bash

BASEDR="/var/lib/test"
TEMPFILE="/tmp/export_tmp"
CONFFILE="/root/conf/excludes.conf"


for i in `find $BASEDR -name "*stuff" -print`;

do

ls $i/hello > /dev/null 2>&1

rc=$?

[ $rc -ne 0 ] && echo "$i doesn't have the file."


done
0
Comment
Question by:unix_admin_1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:madan1278
ID: 33740269
Actually you don't need to test the existence of the file as touch command will do nothing if a file exists already...but I have just included a check for this as well. You just need to focus on the exclude list.

I don't have a system to test the below code but it should work..
#!/usr/bin/bash
#Input the filename with absolute path as a argument to the script.
FILE_TO_CHECK=$1
#Input the absolute path of dirs you want to exclude in the exclude file referenced below
EXCLUDE_LIST=`cat /tmp/exclude.txt` #Variable just for reference
FILENAME=`basename $1`
DIRNAME=`dirname $1`
grep $DIRNAME /tmp/exclude.txt > /dev/null
if [ $? -eq 0 ]
then
[ ! -f $1 ] && touch $1
else
echo "This dir is excluded"
fi
0
 
LVL 9

Accepted Solution

by:
fcontrepois earned 125 total points
ID: 33741282
#!/bin/bash

BASEDR="/var/lib/test"
TEMPFILE="/tmp/export_tmp"
CONFFILE="/root/conf/excludes.conf"

for i in `find $BASEDR -name "*stuff" -print`;

do

[ ! -f $i/hello ] && echo "$i doesn't have the file." && continue

toucn $i

done
0
 

Author Comment

by:unix_admin_1234
ID: 33747031
madan1278.  Your script is close.  However, the FILE_TO_CHECK need to include a way to find the files in a specific directory.  For example, I would want the script to look under /var/reps/ and look for a directory ending in .test/filename.   There are a lot of directories and subdirectories which include this pattern.  Please let me know if this doesn't make sense.

Thanks.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:unix_admin_1234
ID: 33747157
Also, I want to include the files to excludes, and not the directories.

Thanks.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33749841
Your requirements are very unclear.

It appears you are wanting to check for the existence of the file 'hello' in directories and sub-directories under /var/lib/test (excluding certain directories) and creating hello if it doesn't exist.

Is that correct?

If so, then you can do
#!/bin/bash
BASEDIR=/var/lib/test
CONFFILE=/root/conf/excludes.conf

find $BASEDIR -name "hello"  | grep -vf $CONFFILE | xargs -I touch {}

Open in new window

0
 
LVL 2

Assisted Solution

by:madan1278
madan1278 earned 125 total points
ID: 33750350
Give it shot....twigged slightly to exclude files and included some patterns for directories and filenames.

#!/usr/bin/bash
#Input the filename with absolute path as a argument to the script.
BASE_DIR="/var/reps"
DIR_PATTERN=".test"
FILE_PATTERN="filename"
FILE_TO_CHECK=`find $BASE_DIR/$DIR_PATTERN -name $FILE_PATTERN -print`
#Input the absolute path of files you want to exclude in the exclude file referenced below
EXCLUDE_LIST=`cat /tmp/exclude.txt` #Variable just for reference
if [ ! test -z "$FILE_TO_CHECK" ]
then
   for file_pattern in `echo $FILE_TO_CHECK
   do
 FILENAME=`basename $file_pattern`
 grep $FILENAME /tmp/exclude.txt > /dev/null
 if [ $? -ne 0 ]
 then
    touch $file_pattern
 else
    echo "This file is excluded"
 fi
   done
fi
 
0
 

Author Comment

by:unix_admin_1234
ID: 33750630
Tintin, that's exactly what I am looking to do.  I like the brevity of your script, but I'm unclear what the xargs -I touch {} does.  I tried to run the script and keep getting the error:  
+ xargs -I touch '/test{}'
xargs: /git-daemon-export-ok{}: No such file or directory

Thanks.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33751146
Sorry, my typo.  It can actually be simplified to

find $BASEDIR -name "hello"  | grep -vf $CONFFILE | xargs touch

0
 

Author Closing Comment

by:unix_admin_1234
ID: 33784920
Both scripts worked.

Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux Hosting 16 80
open source backup solution 1 45
error log using ftp 7 83
CentOS Backup Options 3 18
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question