LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-11-2013, 11:26 PM   #1
esanto1
LQ Newbie
 
Registered: Jun 2013
Location: Sydney, Australia
Distribution: Linux Mint 14
Posts: 8

Rep: Reputation: Disabled
Question Counting Files in a Directory


Hi,

I've just created a bash backup script and it does work as it and I just have a question about file counting in a directory.

Below is a snippet of my code which is straightforward but I'm confused by the output. As you can see, I've set a Threshold of 7 to limit the number of backup files however when I tested the script it actually creates 8 files before it stops. This is what I can't seem to understand. If I set the threshold to 7 why is it creating 8 files and then stopping? Does it start counting from zero?

Code:
BACKUPDIR=~/Documents/backup
SOURCEDIR=~/Documents/scripts/*.sh
TOTALBACKUPFILES=`ls $BACKUPDIR/scripts.* | wc -l`
THRESHOLD=7

if [ $TOTALBACKUPFILES -le $THRESHOLD ]
Thanks
 
Old 06-12-2013, 12:19 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
1. that should work, but show us the rest of the script, as the 'creates 8 files' bit is probably where the issue is.

2. Use [[ ]] in pref to [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS, but for arithmetic operations, use (( )) which enables you to use the normal maths symbols http://www.tldp.org/LDP/abs/html/ops.html

3. add 'set -xv' at the top of your script; it shows what the parser is doing in detail. A great debug tool

4. ensure you specify the exact shell at the top of the script
Code:
#!/bin/bash
(Use 'which bash' to check the path first)

Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/

Last edited by chrism01; 06-12-2013 at 12:20 AM.
 
Old 06-12-2013, 12:45 AM   #3
esanto1
LQ Newbie
 
Registered: Jun 2013
Location: Sydney, Australia
Distribution: Linux Mint 14
Posts: 8

Original Poster
Rep: Reputation: Disabled
Here's the full script.

Code:
#!/bin/bash

BACKUPFILE=scripts.backup.$(date +%H%M%S_%d%m%y)
BACKUPDIR=~/Documents/backup
SOURCEDIR=~/Documents/scripts/*.sh
TOTALBACKUPFILES=`ls $BACKUPDIR/scripts.* | wc -l`
THRESHOLD=7

if [ $TOTALBACKUPFILES -le $THRESHOLD ]
 then
        tar cvfj $BACKUPDIR/$BACKUPFILE.tar.bz2 $SOURCEDIR >/dev/null 2>>../backup/errors.txt 
        if [ $? != 0 ]
         then 
                echo "Problems creating the backup file"
        fi
fi
#END
 
Old 06-12-2013, 12:55 AM   #4
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,098

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Hi,

you are using -le which is "less than or equal to", so if there are 7 files it will evaluate to true and create an 8th file. Perhaps you want to use -lt.

Evo2.
 
Old 06-12-2013, 01:08 AM   #5
esanto1
LQ Newbie
 
Registered: Jun 2013
Location: Sydney, Australia
Distribution: Linux Mint 14
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi Evo2,

Had to re-read the code a few times but your explanation does make sense now.

Thanks
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with counting files in subdirectories NickPats Linux - Newbie 14 10-12-2012 11:44 AM
[Bash] Counting files paul_l Programming 4 09-04-2009 05:04 PM
a pipeline for counting the lines in a directory fara77 Linux - Newbie 1 03-05-2007 06:00 PM
counting number of lines inside a directory structure. vl@d Linux - General 4 11-20-2006 12:50 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM


All times are GMT -5. The time now is 09:37 PM.

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