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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
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…

726 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