Unix shell script + How to check the file and date

Posted on 2014-08-08
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.
Question by:digs developer
    LVL 35

    Expert Comment

    Which folder ? Recursive (i.e. subfolders also?). If NOT current date, what action?
    LVL 22

    Expert Comment

    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.)

    Author Comment

    by:digs developer
    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

    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.

    Author Comment

    by:digs developer
    Please help
    LVL 23

    Accepted Solution

    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.

    TD=`date +%d%m%Y`
    CHECK=`find new/ -type f -mtime -1 | grep -oh $TD`
    if [ "$TD" = "$CHECK" ]; then
    	echo "FILE EXISTS"
    	echo "Creating todays file.."
    	touch "new/ABC_$TD.txt"

    Open in new window

    LVL 48

    Expert Comment

    Assuming the filenames use the date format you showed, then do

    TODAY=$(date +%d%m%Y)
    cd /to/your/folder
    if [ -f ABC_$TODAY.txt ]
          echo "File already exists for today"
         touch ABC_$TODAY.txt

    Open in new window


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now