LinuxQuestions.org
Help answer threads with 0 replies.
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 09-25-2011, 09:56 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
Can somebody write me a bash script


Can somebody write me a bash script or maybe you have this script which shows files in catalog tree older than typed date.
 
Old 09-25-2011, 10:04 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296
Hello,

Sure, you can do that yourself. What will you learn if we give you a ready made solution? You'll learn and understand a lot more researching yourself. Have a look at these two great starting points:
Bash Guide for Beginners
Advanced Bash Scripting Guide

Also have a look at the man page for find:
Code:
man find
Kind regards,

Eric

Last edited by EricTRA; 09-25-2011 at 10:08 AM.
 
3 members found this post helpful.
Old 09-25-2011, 10:18 AM   #3
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
This time i can't, i am newbie at scripting and i have no idea what to do.
 
0 members found this post helpful.
Old 09-25-2011, 10:22 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296
Hello,

And why wouldn't you be able to? Just to find certain files older then a certain date you don't even have to write a script. Have a look at the man page I pointed you too in my previous post.

Kind regards,

Eric
 
3 members found this post helpful.
Old 09-25-2011, 10:24 AM   #5
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
I need script which asks me date I enter a date and i will see files older than the date which I typed.
 
0 members found this post helpful.
Old 09-25-2011, 10:29 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296Reputation: 1296
Hello,

Again, what's keeping you from reading and learning? Or are you on a time limit? Is this by any chance homework? If it is, you should be aware that your teacher might be a member of this community too and might even be looking over your shoulder (virtually that is) right now. LQ users are all volunteers here, putting in their own free time to help out others with their Linux related problems and questions. You'll have a hard time finding one user here that will give you a ready made solution if you cannot even invest the time to try something or to read a man page. The most widely used principle here when a user asks for help with a script is:

Show us what you've got and tell us where it's failing. Then we will be able to help you.

Kind regards,

Eric
 
3 members found this post helpful.
Old 09-25-2011, 10:48 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,069

Rep: Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887Reputation: 5887
Quote:
Originally Posted by trintukaz View Post
I need script which asks me date I enter a date and i will see files older than the date which I typed.
So write one. EricTRA gave you the link to the Advanced Bash Scripting Guide, which has examples, and from which you can learn. We will HELP you if you're stuck, so post what you've written and what error(s) you've got, and we can help you get past it. But we're not going to do your homework for you.

If you want someone else to write scripts for you, those people are called "consultants"...they get PAID to write programs for other people.
 
Old 09-25-2011, 10:55 AM   #8
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
Lightbulb

Here is a version for you.....

.....................

Quote:
#! /bin/bash
clear

cowsay "LinuxQuestions.org Request"
figlet "File Age Test."


echo
echo

echo -e "\e[107;34mHow many days old are the files you are searching for ?\e[00m\a"

read days

echo
echo

echo -e "What is the full directory path you want to \e[00;31m(Recursively)\e[00m search ?"


read dir

echo -e "\e[107;34mOutput piped to less : Use Arrow Keys to scroll , (q) to quit.\e[00m"

sleep 5

find "$dir" -type f -and -mtime "$days" | less




 
1 members found this post helpful.
Old 09-25-2011, 04:03 PM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
If you have date from the Coreutils package -- and all Linux distributions do, as far as I know --, you can use it to parse the number of minutes elapsed since any given point in time (since 1902, I think). For example, in a Bash script,
Code:
read -p 'Since when? ' timestr
minutes=$[ ($(date +%s) - $(date -d "$timestr" +%s) + 30) / 60 ]
will understand dates like 'jan 5', 'last year', 'last monday', '12/31/2010', or even international standard formats like '2010-12-31 23:59:59'. I personally prefer the last one, since it sorts correctly even as strings.

I add 30 seconds to the calculation, so that it rounds half minutes upwards. It does mean that if you specify a date in the future, the result will be off by a minute, but in this case it should not matter; you really should not have any files with timestamps in the future anyway.

Some dates, like 'last year', specify the date but use the same time as right now. (They do not mean 'start of last year', just 'last year' at this same day and time.) If you want to count the number of minutes since the start of the date the user specifies, you need to first convert the specified datetime to one that specifies midnight on that date. Here is a full Bash script, which also adds a loop that re-asks the date if it cannot be parsed correctly, and stops the script if the user does not supply any date.
Code:
#!/bin/bash

datestr=""
timestr=""
while [ -z "$datestr" ]; do
    [ -z "$timestr" ] || echo "I don't understand '$timestr'." 
    read -p 'Since when? ' timestr
    [ -z "$timestr" ] && exit 0
    datestr=$(date -d "$timestr" '+%Y-%m-%d 00:00:00' 2>/dev/null)
done

minutes=$[ ($(date +%s) - $(date -d "$datestr" +%s) + 30) / 60 ]

echo "$minutes minutes since $(date -d "$datestr")"
Personally, I prefer to supply the date on the command line:
Code:
#!/bin/bash
datestr=$(date -d "$1" '+%Y-%m-%d 00:00:00' 2>/dev/null) || exit $?
minutes=$[ ($(date +%s) - $(date -d "$datestr" +%s) + 30) / 60 ]
echo "$minutes minutes since $(date -d "$datestr")"
which uses the first command line parameter ($1); if the date has a space, you need to quote the date string.
 
1 members found this post helpful.
Old 09-26-2011, 04:02 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by trintukaz View Post
Can somebody write me a bash script or maybe you have this script which shows files in catalog tree older than typed date.
The concise answer is 'Yes'. However, I imagine that you really mean 'please give me a script which does what I want'. This is only possible once you've clearly defined the requirements of the script. Very often, when one invests the effort to define the requirements, the solution emerges with very little additional effort.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trying to write a bash script and I'm stuck Yig Programming 12 04-16-2008 11:04 AM
Trying to write a bash script christianunix Linux - Newbie 5 10-26-2007 06:30 PM
read write in bash script yhus Programming 6 09-18-2006 01:23 PM
Need to write a bash script imagineers7 Linux - General 5 05-10-2006 12:17 AM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 09:56 PM

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

All times are GMT -5. The time now is 08:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration