LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-28-2008, 04:39 PM   #1
nbdwt73
LQ Newbie
 
Registered: Oct 2006
Posts: 3

Rep: Reputation: 0
How to test if filename exists


I have an issue with detecting if a file that begins with "SYS_0" exists. We have a Windows based database that is backed up to a linux server (Debian). The file names are similar but with a date suffix. What I need to do is confirm that a particular backup file - or set of files - is present (meaning backup completed) and then process. Script follows:

#!/bin/bash
FILE="/media/Backup/SYS_0*"
if [ -f $FILE ]; then
rm -f /media/Backup/SYS_3*
rename -f 's/SYS_2/SYS_3/' /media/Backup/SYS*
rename -f 's/SYS_1/SYS_2/' /media/Backup/SYS*
rename -f 's/SYS_0/SYS_1/' /media/Backup/SYS*
fi

The problem is that multiple SYS_0... files can exist. The script works fine if only one exists. What am I doing wrong?
Thanks for any help.
 
Old 11-28-2008, 04:48 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Jessie, Linux Mint 17
Posts: 7,751

Rep: Reputation: 272Reputation: 272Reputation: 272
I would solve this problem by using the logrotate command instead of writing my own script. See:

man logrotate

Using logrotate you would set up the config_file to rotate your files through 4 generations.

--------------------
Steve Stites
 
Old 11-28-2008, 07:10 PM   #3
nbdwt73
LQ Newbie
 
Registered: Oct 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks - I tried logrotate but because of file naming, I had problems on the Windows server. I have to maintain the specific name(s) following the "SYS_X". The script I posted works except when there are multiple instances of SYS_0.

Is there any way to determine the existence of that prefix?
 
Old 11-29-2008, 11:40 AM   #4
nbdwt73
LQ Newbie
 
Registered: Oct 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Got it working. Maybe this will help someone...

#!/bin/bash
lc=$(ls -1 /media/Backup/QB | grep SYS_0 | wc -l)
if [ $lc -gt 0 ]; then
rm -f /media/Backup/SYS_3*
rename -f 's/SYS_2/SYS_3/' /media/Backup/SYS*
rename -f 's/SYS_1/SYS_2/' /media/Backup/SYS*
rename -f 's/SYS_0/SYS_1/' /media/Backup/SYS*
fi

Last edited by nbdwt73; 11-29-2008 at 01:46 PM. Reason: typo
 
Old 11-29-2008, 12:48 PM   #5
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Welcome to LQ.

The initial problem was caused by the test being passed too many arguments.

Congratulations, I would have suggested piping ls to wc -l but you already beat me to it
 
Old 11-29-2008, 12:54 PM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 518

Rep: Reputation: 177Reputation: 177
Your original code will work if you take the file test operator out of the if statement:
Code:
#!/bin/bash
FILE="/media/Backup/SYS_0*"
[ -e $FILE ] >/dev/null 2>&1 
if [ $? -ne 1 ]; then
rm -f /media/Backup/SYS_3*
rename -f 's/SYS_2/SYS_3/' /media/Backup/SYS*
rename -f 's/SYS_1/SYS_2/' /media/Backup/SYS*
rename -f 's/SYS_0/SYS_1/' /media/Backup/SYS*
fi
 
Old 11-29-2008, 01:05 PM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
true, but that still causes the test to bomb out if $FILE contains multiple files, which is why you need the 2>/dev/null
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 10:29 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 07:07 AM
to test that a file exists but is not writeable? johnpaulodonnell Linux - Newbie 2 01-30-2007 07:11 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 05:33 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 09:29 AM


All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration