LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2012, 01:12 PM   #1
ghost_1on
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
I need a linux script that will validate the date created on a folder


Hello All,

Basically I'd like a command that I can run in a folder lets say Products- inside that folder has another set of folders of prodcut names, a- b- c- d etc.

In each of those(a b c d) -they are versions created 1, 2 ,3, 4 etc.

Each version is created on different days-

I'd like to list from folder products- all the products(a,b,c,d) that have versions created within the last 3 months. so the out put would to a txt file would be

a
e
h
j - all of which have Versions created within the last 3 months.

Anyway to do this?
 
Old 02-08-2012, 02:13 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi,
is this a homework? What have you got so far?

Those 'versions' -- are they files? Directories? Is there anything else in directories a, b, c, d...?
 
Old 02-08-2012, 02:15 PM   #3
ghost_1on
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
lol not homework- just giving an example.

yes theres .zip files and xml files in the version folders( 1 2 3 4) not in the a b c d. The only thing in a b c d is the 1 2 3 4 versions.
 
Old 02-08-2012, 02:31 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
How about something like this?

Code:
#!/bin/bash

for fname in "$1"/*; do
	if [ ! -d "$fname" ]; then continue; fi
	if [ "$(find "$fname" -mindepth 1 -maxdepth 1 -mtime -90 | wc -l)" -gt 0 ]; then
		echo "$fname"
	fi
done
 
Old 02-09-2012, 09:02 AM   #5
ghost_1on
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
#!/bin/bash

for fname in "$1"/*; do
if [ ! -d "$fname" ]; then continue; fi
if [ "$(find "$fname" -mindepth 1 -maxdepth 1 -mtime -90 | wc -l)" -gt 0 ]; then
echo "$fname"
fi
done


--

Some explanation would be perfect, I'm a linux noob!

fname would be which variable? the main folder?

we are echoing to fname variable instead of a .txt file?

why the greater than 0?

Looks good! but for me to use it I should understand it haha

Last edited by ghost_1on; 02-09-2012 at 09:04 AM.
 
Old 02-09-2012, 09:42 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Ok. The entire script is a loop.
Code:
for fname in "$1"/*; do
    ...
done
It creates a variable called fname and loops over all values listed after the in keyword.
The expression "$1"/* is a glob that will be expanded by bash into the list of all files in the directory
"$1". The "$1" variable itself refers to the first command line argument of the script, so if I name the file "script"
and execute it as

Code:
./script ./Products
the "$1" will be "./Products". If the Products directory contains directories a, b and c, the for line
will be expanded to

Code:
for fname in ./Products/a ./Products/b ./Products/c; do
Note that unlike some other methods, such as pasting the output of ls, the * glob will expand into a set of correctly delimited filenames even in the
case when the filenames contain spaces.

The first thing I do with each fname is checking that it is a directory

Code:
if [ ! -d "$fname" ]; then continue; fi
The "!" sign is a negation (logical not), the -d is true if its argument exists and is a directory. The line therefore means
"if $fname is not a directory, skip the rest of the loop and continue with the next iteration"

Next part is using find (see man find for a complete manual).

[code]find "$fname" -mindepth 1 -maxdepth 1 -mtime -90[code]

This will search all files/directories in $fname that fit the following conditions:
-mtime -90 - was modified 90 or less days ago
-mindepth 1 -- min. level to be searched -- this prevents the directory $fname itself from being included in the list
-maxdepth 1 -- max. level to be searched -- this prevents find from searching the subdirectories of $fname recursively

The output of find is then piped to wc which counts the entries returned by find. Both commands are enclosed in $( ), which means
"run the commands in parentheses and paste their output here":

Code:
$(find "$fname" -mindepth 1 -maxdepth 1 -mtime -90 | wc -l)
The output (which is the number of files in $fname that were modified in the last 90 days) is then compared with zero in an if construct
Code:
if [ "$( ... )" -gt 0 ]; then
	echo "$fname"
fi
which means "if there's more than zero files modified in the last 90 days, print $fname"

So, that's about it. Hope this makes it more clear

Oh, sorry if this is more detailed than what you wanted... It's just that it is a long boring afternoon in a lonely office and I am trying to keep myself busy by writing long posts )

Last edited by millgates; 02-09-2012 at 09:49 AM.
 
Old 02-09-2012, 01:40 PM   #7
ghost_1on
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
I think I should take you to dinner...

IT WORKS! I'm just going to edit it with an echo command to a txt file... see if I can do that

THANKS SO MUCH, saved me the time of manually having to do this
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to monitor files created on a folder cmadiam82 Programming 9 05-11-2013 12:59 AM
[SOLVED] find only new created files from certain date => display folder and path only dragonetti Linux - Newbie 5 10-27-2011 10:38 AM
Bash Script to Copy Modification Date from a file to his folder pjgm Programming 12 07-31-2011 08:33 AM
Sort files under a folder by created date Kunsheng Programming 3 05-11-2009 04:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration