how to define arg in bash

Posted on 2008-10-20
Last Modified: 2010-04-21
Please tell me what is wrong with this script. I want to define what is passed to +mtime and -mtime command line. So it would run like: 10 20

Then it would list anything older than 10 days (+mtime) and shorter than 60 days.

find . -type f \( -mtime +$args 1 -o -mtime +$ARGS[1] \) -printf "%Tw %p\n" | \
grep -v '^0' | cut -c3- | xargs ls -l
Question by:linuxpig
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 84

Expert Comment

ID: 22764331
find . -type f -mtime +$1-mtime -$2
LVL 40

Expert Comment

ID: 22764400
shell refer to arguments as positional arguments and they are accessed by names like $1 $2 $3 ...

Author Comment

ID: 22764684
Looking at the find command itself, with the two mtimes, will it keep anything within 14 days and a sunday and remove everything else is the mtimes are 14 and 60 as below?

find . -type f \( -mtime +14 -o -mtime +60 \) -printf "%Tw %p\n" | grep -v '^0' | cut -c3- | xargs rm -rf
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 48

Expert Comment

ID: 22764712

find . -type f \( -mtime +$1 -o -mtime +$2 \) -printf "%Tw %p\n"|grep -v "^0" | cut -c3- | exec ls -l

Open in new window


Author Comment

ID: 22766389
Thanks tintin,

But that will do what i want it to do right? Using the two mtimes will  keep anything within 14 days and a sunday and remove everything else is the mtimes are 14 and 60 on command line as below? 14 60
LVL 48

Expert Comment

ID: 22770125

That's correct.

Author Comment

ID: 22772354
If i want to define the arguments below to be entered command line, will what i have below work?
I want to be able to type the command as: 10 60 Sun

Where 10 is the first mtime, 60 is the second, Sun is the day to define in the grep -v '^$3'


find /local/backup/uni* -type f \( -mtime +$1 -o -mtime +$2 \) -printf "%Tw %p\n" | grep -v '^$3' | cut -c3- | xargs ls -la
LVL 48

Accepted Solution

Tintin earned 50 total points
ID: 22772916
I noticed that the mtime logic is incorrect.  It needs to be

-mtime +$1 -a -mtime -$2

To add the 3rd parameter, you can do
find /local/backup/uni* -type f \( -mtime +$1 -a -mtime -$2 \) -printf "%Ta %p\n" | grep -v "^$3" | cut -c5-  | xargs ls -l

Open in new window


Author Closing Comment

ID: 31508141
Actually -a wont work, -o will, ive tested it already and i also tried the grep you listed, it doesnt do anything but return a 0
LVL 48

Expert Comment

ID: 22773121
I beg to differ with your assessment.

If you call your script with 10 60 Sun

then the logic you have is files that have been modified greater than 10 days ago OR modified greater than 60 days ago will be listed.  The 2nd -mtime is made redundant in this case.

Also, I can not see anyway possible, you would be getting the grep to return 0.  Can you please show an example where that behaviour occurs.


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question