Sponsored Content
Top Forums Shell Programming and Scripting Files with 0 file size and created today Post 302260766 by jim mcnamara on Friday 21st of November 2008 02:47:52 PM
Old 11-21-2008
hmm. Are you on solaris - is there a modern version of touch in the xpg4 directory?

If all else fails -- Here is some older code that sets filetimes. You can try it.
Code:
/* 	mytouch.c  set file mtime & atime        
   	usage: mytouch newfiletime  [file ... ]
   	      source | mytouch newfiletime
	where newfiletime is ONLY one of
	YYYY-mm-dd or today or yesterday
	POSIX.1 version
*/
#define  _INCLUDE_POSIX_SOURCE
#include <utime.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#ifndef PATH_MAX
#define PATH_MAX _POSIX_PATH_MAX
#endif
static char *fmt="%Y-%m-%d";

void usage()/* error message */
{
	fprintf(stderr,"%s%s%s",
		"usage: mytouch newfiletime  [file ... ]\n",
		" where newfiletime is ONLY one of: \n",
		"  YYYY-mm-dd (YYYY is 1970-2037)\n  today\n  yesterday\n");
	exit(1);
}

char *today(int which)  /* yesterday & today only */
{
	static char t[80]={0x0};
	time_t lt=time(NULL)-(86400 * which);

	struct tm *tmptr=localtime(&lt);
	(void)strftime(t, sizeof(t), fmt, tmptr);

	return t;
}
/* create struct tm for date desired*/
time_t conv(const char *tspec)
{
	struct tm tms;
	char tmp[80]={0x0};
	int yr=0;

    strcpy(tmp, tspec);
	if(strcmp(tmp, "today")== 0)
		strcpy(tmp, today(0));
	if(strcmp(tmp, "yesterday")== 0)
		strcpy(tmp, today(1));
	yr=atoi(tmp);	
	if( yr > 2037 || yr < 1970 || strptime(tmp, fmt, &tms)==NULL)
		usage();

	return mktime(&tms);
}
/* check file existence && create - set mtime && atime*/
void set_utimes(char *filespec, struct utimbuf *ut)
{	
	FILE *tmp=NULL;

	if(access(filespec, F_OK)== -1 )
	{
		tmp=fopen(filespec, "a");
		if(tmp!=NULL)
			(void)fclose(tmp);
		else
		{
			perror("Cannot create file");
			exit(1);
		}
	}
	if(utime(filespec, ut)== -1)
	{
		perror("File access error");
		exit(1);
	}
}

char *check(char *value, const size_t len) /*limited validation*/
{
	if(*value && memchr(value, 0, len + 1)!=NULL)
	{
		char *p=strchr(value, '\n');
		if(p!=NULL) *p=0x0;
		for(p=value; *p && isprint(*p); p++);
		if(*p)
			usage();
		return value;
	}
	usage(); /* everything else is an error */
}

int main(int argc, char **argv)
{
	char filename[PATH_MAX]={0x0};
	int i=0;

	if(argc > 1)
	{
		time_t when = conv(check(argv[1], 10 ));
		struct utimbuf ut={when, when};
		if(argc > 2)
			for(i=2; i < argc; i++)
			   set_utimes(check(argv[i], PATH_MAX), &ut);

		if(argc == 2)
			while(fgets(filename, sizeof(filename), stdin)!=NULL)
                set_utimes(check(filename, PATH_MAX), &ut);
	}
	if(argc== 1)
		usage();

	return  0;
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Archiving Files by selecting years file created

I have recently taken on a new position and want to clean up several file locations that currently hold data back through 1999. While I need to keep this data for business reasons, I have created directories to help sort out the individual years. Is there a quick command that I can use to archive... (2 Replies)
Discussion started by: dmhammond
2 Replies

2. UNIX for Dummies Questions & Answers

To list all the files created today with directory path

Hi, Can any one tell the command to list all the files that are created as of today from all the directories? The Command "ls -ltR" is listing all the files. But I want the list of files that has been created as of today along with the directory path:) Thank you in advance.:) Regards,... (4 Replies)
Discussion started by: meetusha.b
4 Replies

3. Shell Programming and Scripting

Calculate Files Created Today

I need to figure out how to get all the files from a certian dir ./123/*sat files and ./230/*sat files and several other directories which have these *sat files in them. I need to calculate how many were created today and how many yesterday from 2:00 pm on the 28th to 2pm on the 29th. It's a... (1 Reply)
Discussion started by: xgringo
1 Replies

4. UNIX for Advanced & Expert Users

AT can't be created for you. Max size of the queue is reached..

I have a listener (a batch job written in shell script) that calls a perl file (say apple.pl). The listener runs at scheduled timing (say 11 pm to 6 am)&. Owner of the listener is a common id. My question is.. Once the listener starts running (here, Im NOT attempting to generate any reports... (1 Reply)
Discussion started by: vavjeeva
1 Replies

5. Shell Programming and Scripting

ftp - How to download files which created today?

Hello experts, I have written following script to download files which created today. Unfortunately, it's not working. test.ksh: #Defining variables USR='xxx' PASSWD='yyyy' HT='test.test.com' FILE='S*.pdf' XFILE=$(echo find . -type f -mtime 0) ZFILE=$(echo ls -tR|grep 'Jun 8')... (14 Replies)
Discussion started by: dipeshvshah
14 Replies

6. Shell Programming and Scripting

check file exists and created today issue

Morning My other issue I have seems very simple but im just not seeing it. I have a script that checks on a remote share to see if the backups for some systems have run. Its as simple as: find /mnt/ukwcs-net-config/WLAN-Controllers/ -mtime -1 -ls | egrep '(cfg)' > wlanlog.txt cut -c 1-92... (4 Replies)
Discussion started by: ltodd2
4 Replies

7. UNIX for Advanced & Expert Users

default size of a newly created folder

Hi all, In linux how to create a directory with specified size, so that it can be used only up to the mentioned size. Actually my question is, whether we can do directory quota in linux. mounting the directory in a partiton will do that, but do we have any other option... (1 Reply)
Discussion started by: anishkumarv
1 Replies

8. Shell Programming and Scripting

How to find files created today in a particular directory?

Dear All, I want a Hp Ux command to find out the files created today in a particular directory or mountpoint. Kindly help. Thanks Bhaskar (10 Replies)
Discussion started by: sudiptabhaskar
10 Replies

9. Linux

List all files created today exclude last one

Hi, ALL thanks in advance, i listed all files using this command ls -ltr $(date +%Y%m%d)*.xmlbut i would like to exclude the last one created ; Best regard MEROUAN Use code tags, thanks. (4 Replies)
Discussion started by: merouan
4 Replies

10. Shell Programming and Scripting

Listing the file name and no of records in each files for the files created on a specific day

Hi, I want to display the file names and the record count for the files in the 2nd column for the files created today. i have written the below command which is listing the file names. but while piping the above command to the wc -l command its not working for me. ls -l... (5 Replies)
Discussion started by: Showdown
5 Replies
GETDATE(3)						     Linux Programmer's Manual							GETDATE(3)

NAME
getdate, getdate_r - convert a date-plus-time string to broken-down time SYNOPSIS
#define _XOPEN_SOURCE 500 #include <time.h> struct tm *getdate(const char *string); extern int getdate_err; #define _GNU_SOURCE #include <time.h> int getdate_r(const char *string, struct tm *res); DESCRIPTION
The function getdate() converts a string representation of a date and time, contained in the buffer pointed to by string, into a broken- down time. The broken-down time is stored in a tm structure, and a pointer to this structure is returned as the function result. This tm structure is allocated in static storage, and consequently it will be overwritten by further calls to getdate(). In contrast to strptime(3), (which has a format argument), getdate() uses the formats found in the file whose full pathname is given in the environment variable DATEMSK. The first line in the file that matches the given input string is used for the conversion. The matching is done case insensitively. Superfluous whitespace, either in the pattern or in the string to be converted, is ignored. The conversion specifications that a pattern can contain are those given for strptime(3). One more conversion specification is specified in POSIX.1-2001: %Z Timezone name. This is not implemented in glibc. When %Z is given, the structure containing the broken-down time is initialized with values corresponding to the current time in the given timezone. Otherwise, the structure is initialized to the broken-down time corresponding to the current local time (as by a call to local- time(3)). When only the weekday is given, the day is taken to be the first such day on or after today. When only the month is given (and no year), the month is taken to be the first such month equal to or after the current month. If no day is given, it is the first day of the month. When no hour, minute and second are given, the current hour, minute and second are taken. If no date is given, but we know the hour, then that hour is taken to be the first such hour equal to or after the current hour. getdate_r() is a GNU extension that provides a reentrant version of getdate(). Rather than using a global variable to report errors and a static buffer to return the broken down time, it returns errors via the function result value, and returns the resulting broken-down time in the caller-allocated buffer pointed to by the argument res. RETURN VALUE
When successful, getdate() returns a pointer to a struct tm. Otherwise, it returns NULL and sets the global variable getdate_err to one of the error numbers shown below. Changes to errno are unspecified. On success getdate_r() returns 0; on error it returns one of the error numbers shown below. ERRORS
The following errors are returned via getdate_err (for getdate()) or as the function result (for getdate_r()): 1 The DATEMSK environment variable is not defined, or its value is an empty string. 2 The template file specified by DATEMSK cannot be opened for reading. 3 Failed to get file status information. 4 The template file is not a regular file. 5 An error was encountered while reading the template file. 6 Memory allocation failed (not enough memory available). 7 There is no line in the file that matches the input. 8 Invalid input specification. ENVIRONMENT
DATEMSK File containing format patterns. TZ, LC_TIME Variables used by strptime(3). CONFORMING TO
POSIX.1-2001. NOTES
The POSIX.1-2001 specification for strptime(3) contains conversion specifications using the %E or %O modifier, while such specifications are not given for getdate(). In glibc, getdate() is implemented using strptime(3), so that precisely the same conversions are supported by both. EXAMPLE
The program below calls getdate() for each of its command-line arguments, and for each call displays the values in the fields of the returned tm structure. The following shell session demonstrates the operation of the program: $ TFILE=$PWD/tfile $ echo '%A' > $TFILE # Full weekday name $ echo '%T' >> $TFILE # ISO date (YYYY-MM-DD) $ echo '%F' >> $TFILE # Time (HH:MM:SS) $ date $ export DATEMSK=$TFILE $ ./a.out Tuesday '2009-12-28' '12:22:33' Sun Sep 7 06:03:36 CEST 2008 Call 1 ("Tuesday") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 9 tm_mon = 8 tm_year = 108 tm_wday = 2 tm_yday = 252 tm_isdst = 1 Call 2 ("2009-12-28") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 28 tm_mon = 11 tm_year = 109 tm_wday = 1 tm_yday = 361 tm_isdst = 0 Call 3 ("12:22:33") succeeded: tm_sec = 33 tm_min = 22 tm_hour = 12 tm_mday = 7 tm_mon = 8 tm_year = 108 tm_wday = 0 tm_yday = 250 tm_isdst = 1 Program source #define _GNU_SOURCE 500 #include <time.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct tm *tmp; int j; for (j = 1; j < argc; j++) { tmp = getdate(argv[j]); if (tmp == NULL) { printf("Call %d failed; getdate_err = %d ", j, getdate_err); continue; } printf("Call %d ("%s") succeeded: ", j, argv[j]); printf(" tm_sec = %d ", tmp->tm_sec); printf(" tm_min = %d ", tmp->tm_min); printf(" tm_hour = %d ", tmp->tm_hour); printf(" tm_mday = %d ", tmp->tm_mday); printf(" tm_mon = %d ", tmp->tm_mon); printf(" tm_year = %d ", tmp->tm_year); printf(" tm_wday = %d ", tmp->tm_wday); printf(" tm_yday = %d ", tmp->tm_yday); printf(" tm_isdst = %d ", tmp->tm_isdst); } exit(EXIT_SUCCESS); } SEE ALSO
time(2), localtime(3), setlocale(3), strftime(3), strptime(3), feature_test_macros(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2008-09-07 GETDATE(3)
All times are GMT -4. The time now is 10:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy