Visit Our UNIX and Linux User Community


Script to check for the file existence, if file exists it should echo the no of modified days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check for the file existence, if file exists it should echo the no of modified days
# 1  
Old 08-02-2011
Script to check for the file existence, if file exists it should echo the no of modified days

Hi,

I am looking for a shell script with the following.

1. It should check whether a particular file exists in a location

Code:
#!/bin/sh

if [ -e /Users/Shared/xxx.txt ];
then
echo "xxx.txt File Exists"
else
echo "File Not Found"
fi


2. If file exists, it should check for the modified date and run a command if the modified date is greater than 45 days

I have somehow managed to do the first point but I have to add the second query in my script.

Thanks!!!

Last edited by pludi; 08-02-2011 at 07:42 AM..
# 2  
Old 08-02-2011
On OSX, Linux, and BSD you can use the stat command to get the modification time. Read it into a variable using $( ... ) and check if that's greater than 45.
# 3  
Old 08-03-2011
[Solved]check file existense and mod date older than 45 days

Hi,

I am managed to get the script with the help of my friend. It works.


Code:
#!/bin/sh


#This script checks if the <filename> esixts in the <fileloc> and also if the modification date is less than 45 days.

filename=filename.txt

fileloc="filepath"

cd $fileloc

if [ -f $filename ]; then

  echo "File Exists"
  filestr=`find . -name $filename -mtime +45 -print`
  if [ "$filestr" = "" ]; then
    echo "File is not older than 45 days"
  else
    echo "File is older than 45 days"
  fi
else

  echo "File does not exist"
fi

exit 0


Previous Thread | Next Thread
Test Your Knowledge in Computers #218
Difficulty: Easy
A peer-to-peer network sets all connected computers equal and all connected computers share the same abilities to use resources available on the network.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

2. Shell Programming and Scripting

Except script fails to check file exists or not in remote node

Dear members, The following expect script connects to remote node and check for the file "authorized_keys" in directory /root/.ssh in remote node. However the result is always found even if the file exist or doesn't exist. expect { "$fname" { send_user "found\n" } Any idea what is... (4 Replies)
Discussion started by: Sudhakar333
4 Replies

3. Shell Programming and Scripting

Script to check file exists

Hi, I am trying to write a script which checks if any file exists with "*.log" or "*.out" in Directory below is the code #------------------ path=/abd/xyz/ if ; then echo "Good" else echo "Failure" fi #-------------------------- its always going to else part and printing... (8 Replies)
Discussion started by: ch33ry
8 Replies

4. Shell Programming and Scripting

script to check for existence of file (or else sleep for x time)

Hi Forum. I have a script that accepts 3 input parameters (source directory, list file text, sleep time) and checks for the presence of files. If not there, script goes to sleep for certain amount of time provided by 3rd input. list file text contains 1 entry but may contain more (file... (13 Replies)
Discussion started by: pchang
13 Replies

5. Shell Programming and Scripting

Script to check if file exists

guys, I am trying to write a script that does the following: it looks for a file in a specific directory and if the file is not there (NOT), it emails me. I have tried the following but its not working. It simply hangs up. Please help. if then mail -s 'blah blah blah' my email... (4 Replies)
Discussion started by: basisvasis
4 Replies

6. Shell Programming and Scripting

Checking the existence of a file before getting last modified

Hi, I am trying to check the existence of a file, from a list of possible filenames: status-A status-B status-C before retrieving the last modified datetime using ls, I want to check it exists or ls will throw an error. So I have tried this: if ; then ls status-* fi But the if... (3 Replies)
Discussion started by: LostInTheWoods
3 Replies

7. Shell Programming and Scripting

Shell script to check if any file exists in 4 folders

Hi All, working on AIX 5.3. Requirement is: Shell script in ksh to check if any file exists in 4 folders as below: 1. /FILE/INB/INT1 2. /FILE/INB/INT2 3. /FILE/INB/INT3 4. /FILE/INB/INT4 Thanks a lot for your time! a1_win. (3 Replies)
Discussion started by: a1_win
3 Replies

8. Shell Programming and Scripting

How many days since a file was modified?

I am trying to write a script to backup my laptop to a NAS drive using rsync. I want the backup to be done, only if it has been more than a week since my last backup. Each time the rsync command executes, I also create a file backuptime.txt file, with the time at which the script completed the... (1 Reply)
Discussion started by: anandjayaraman
1 Replies

9. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies

10. Shell Programming and Scripting

unix script to check whether particular file exists and to find its size

I want to find the size of particular file exists in a particular directory and i wnt to zip it. In the below mentioned code it should check the MQ.log in the particular directory.Please correct my code so that it will check for particular MQ.log but i could not able to check whether the... (9 Replies)
Discussion started by: Balachandar
9 Replies

Featured Tech Videos