The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To list all the files created today with directory path meetusha.b UNIX for Dummies Questions & Answers 4 08-18-2008 06:53 AM
Sort files by date, not showing files from today carlosdivega UNIX for Dummies Questions & Answers 3 07-17-2008 11:37 AM
How to list all the files which are not generated today redlotus72 UNIX for Dummies Questions & Answers 2 10-05-2007 10:27 AM
capture all the today files Teh Tiack Ein Shell Programming and Scripting 3 07-28-2005 07:30 PM
Archiving Files by selecting years file created dmhammond UNIX for Dummies Questions & Answers 2 06-13-2005 03:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-19-2008
rushs682 rushs682 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Files with 0 file size and created today

hi,

I am building a script to identify those files created/modified today and with file size 0. I am able to find the files with 0 file size and created/modified in last 24 hrs as shown below but not today (current date), I tried using (touch -t time filenm) but in my version of unix at work it does not allow me to, please post if somebody has an alternative solution,

Here is my current script,

cd /usr/local/upload;
for i in *;do
if [ -f $i ];then
if [ ! -s $i ];then
find $i -type f -mtime -1;
fi
fi
done >> usr/file3.txt

Thanks for looking into it.
  #2 (permalink)  
Old 11-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,777
Quote:

I tried using (touch -t time filenm) but in my version of unix at work it does not allow me to, please post if somebody has an alternative solution,
The touch method is the one you want, what error do you get when you try touch?
  #3 (permalink)  
Old 11-20-2008
rushs682 rushs682 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Following is the error I get

usage: touch [-amcf] file ...
  #4 (permalink)  
Old 11-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,777
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;
}
  #5 (permalink)  
Old 11-21-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Here is a way to approach

although, it can probably be simplified...

Code:
> cat today_empty.sh
#! /usr/bin/bash
# script to show empty files created on this day
# original posting to www.unix.com


ls >today_empty_mylist
month=$(ls -l today_empty_mylist | awk '{print $6}')
day=$(ls -l today_empty_mylist | awk '{print $7}')
#echo $month $day

while read myfile
   do
   if [ -s $myfile ]
      then
      continue
   fi

#   ls -l $myfile

   ls -l $myfile | awk -v mon=$month -v day=$day '$6==mon && $7==day {print $9}'
done<today_empty_mylist
  #6 (permalink)  
Old 11-21-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
Isnt this what you want?
Code:
find . -ctime -1 -type f -size 0 -exec ls -l {} \;
My bad, you want just current date...

This is kinda crude but it could work:
Code:
find . -ctime -1 -type f -size 0 -exec ls -l {} \;  | grep "`date '+%b %d`"
EDIT:
Problem ran into with previous,:
If date is 1 digit it gives "Dec 01", where ls -l is in "Dec 1" format, on my CentOS anyway.

Last edited by Ikon; 11-21-2008 at 04:33 PM..
  #7 (permalink)  
Old 11-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,777
I think this is the issue
The old version of touch does not allow the creation of a filetime on a comparison file,
- for example the OP wants to be able to create a file with a filetime like this:
Code:
-rw-rw-rw-   1 jmcnama    prog             0 Nov 21 00:00 a.lis
Where Nov 21 00:00 has to be dynamic, and is today's first second.

ctime is just the time the inode was changed - nothing else. It is not a create date - they do not exist in standard UNIX.

If the OP has perl v 5.8 you can write perl code to do this. I posted ancient POSIX.1 C code that does that and some other stuff - poorly. If we knew the OS and version it might help. If the OP could compile gnu version of touch that would be good.

Otherwise I have misunderstood completely.
Closed Thread

Bookmarks

Tags
files, modified, today

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0