?
Solved

Unix shell script + How to check the file and date

Posted on 2014-08-08
6
Medium Priority
?
2,742 Views
1 Endorsement
Last Modified: 2014-08-18
How to check the file is present or not ? If present then check date and if it is current date then give the errore message and exit.
Need unix shell script.
1
Comment
Question by:digs developer
6 Comments
 
LVL 37

Expert Comment

by:Kimputer
ID: 40249108
Which folder ? Recursive (i.e. subfolders also?). If NOT current date, what action?
0
 
LVL 22

Expert Comment

by:Brian Utterback
ID: 40249169
Define current date? The same as today's date, or less than 24 hours old? Does the file have a specific name or is there more than one or different? Any limitation on which shell (bash, bourne, perl, etc.)
0
 

Author Comment

by:digs developer
ID: 40249287
Suppose there is one file    ABC_08082014.txt  present in one folder.
Folder contains  2 files ... one for current day and other for yesterday.
ABC_07082014.txt   --- yesterday file
ABC_08082014.txt

So my requirements is check the todays file is present or not.
If todays file is not preasent then create the todays file.
If file is present then give the error message that file is already created.

Need Unix shell script.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:digs developer
ID: 40249529
Please help
0
 
LVL 23

Accepted Solution

by:
savone earned 1240 total points
ID: 40249593
Here ya go...

Of course you will have to change new/ to the path of your directory, and this will also just create an empty txt file with todays date.

#!/bin/bash
TD=`date +%d%m%Y`
CHECK=`find new/ -type f -mtime -1 | grep -oh $TD`

if [ "$TD" = "$CHECK" ]; then
	echo "FILE EXISTS"
else
	echo "TODAYS FILE DOES NOT EXIST"
	echo "Creating todays file.."
	touch "new/ABC_$TD.txt"
fi

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 40250201
Assuming the filenames use the date format you showed, then do

#!/bin/bash
TODAY=$(date +%d%m%Y)

cd /to/your/folder

if [ -f ABC_$TODAY.txt ]
then
      echo "File already exists for today"
else
     touch ABC_$TODAY.txt
fi

Open in new window

0

Featured Post

Industry Leaders: 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

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…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

840 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