Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-10-2011, 02:35 AM   #1
LQ Newbie
Registered: May 2011
Posts: 11

Rep: Reputation: 0
[shell script] ignore folder if it contains file x

Okay so some of you may have realised that i am creating a small backup script, now i have another question.

Lets say that the file backup_log.txt will be within the directory where files are backed up to.

So for example i have run my script and it has backed up to
so within that folder there is a file named backup_log.txt

Now my problem is, lets assume i am backing up the current directory, and this directory contains a previous backup folder.

So i am backing up my home directory:
and inside i have

I have a sort of recursive script to go through and backup files, and i created a simple case to check whether the folder name was .backup

 case $(dirname $filestoback) in
                                        */.backup*) echo "is backup folder"
However what i would really like to do, is exclude the folder, only if it contains the backup_log.txt file inside it, because theoretically, the backup folder could have a different name!
Thank you very much.
Old 05-10-2011, 03:52 AM   #2
Registered: Aug 2004
Location: Adelaide, South Australia
Distribution: Ubuntu (Natty)
Posts: 38

Rep: Reputation: 18
IMHO, it would look something like this:

if [ -f $dirname/backup_log.txt ] ; then
echo "is backup folder"
echo "is not..."

Square brackets are shorthand for the test shell builtin - "man test" will show all of the options, but the "-f" means file exists and is readable.
Old 05-10-2011, 03:57 AM   #3
LQ Newbie
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you for the response however, that will simply be if that specific file is found. What i need is a check before hand, to exclude the folder that that file is contained in, not simply exclude that file itself
Thanks again !
Old 05-10-2011, 06:30 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
I think you may have to explain further as obviously if the directory contains the file then the directory must exist.

Also, you could exclude the folder using find so it will also depend on how you are retrieving the folder and / or file names.
Old 05-10-2011, 06:38 AM   #5
LQ Newbie
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
How would i go about excluding the directory using find?

Edit nvm found it:
find $directory -name ".foldername" -prune -o -type f -print

Last edited by vraptor; 05-10-2011 at 07:06 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ignore/Disable Ctrl-C (SIGINT) in a shell script dctw Programming 3 01-11-2010 02:30 AM
Shell Script To extract more than one tar file in to a single folder greensuman Linux - General 5 12-02-2009 11:08 PM
shell script which makes *something* with every file in folder matt79 Programming 5 02-17-2009 03:58 PM
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration