LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Simple bash scripting question, involving finding newer files (http://www.linuxquestions.org/questions/linux-software-2/simple-bash-scripting-question-involving-finding-newer-files-744338/)

salasi 08-01-2009 07:01 AM

Simple bash scripting question, involving finding newer files
 
This is a simple syntax question about trying to find files that are newer than a certain date within a particular directory tree (in this case,within a .kde4 tree). This is just out of general interest, and can't be said to be in any way urgent, as I'm just trying to satisfy curiosity here and my crude script accomplished the necessary minimum.

What I am trying to do is use find to go through the .kde4 tree and find what is new there. What I've got is:

Code:

sep
find ~/.kde4 -type f -mtime -5 -ls | grep -v .org | grep -v .co.uk | grep -v .com | grep -v cache-
sep

(you can ignore the two 'sep'statements - they just print a row of dashes, to make script output easier to read, and is used in other scripts)

The first issue is that the line of 'grep -v' statements is inelegant, and I'm sure there is a way of filtering several things at once, but I can't figure out the syntax.

The supplementary question is that the script uses fixed parameters (-mtime 5, .kde4) which it might be nice to read from the invocation.

I could read $1, $2,... but sorting out the business of the parameters being given in different orders, sometimes being present, sometimes absent, sometimes non-numerical parameters being given where numbers should have been given and this seems more complication than is sensible for a trivial script. Is this because I'm trying to do this the hard way, and there is an easy way that I should be using?

colucix 08-01-2009 09:06 AM

Quote:

Originally Posted by salasi (Post 3627540)
The first issue is that the line of 'grep -v' statements is inelegant, and I'm sure there is a way of filtering several things at once, but I can't figure out the syntax.

You can negate a test using !. If you want to exclude the .org, .co.uk, .com, and cache- files, you can do something like:
Code:

find ~/.kde4 -type f -mtime -5 ! -name \*.org ! -name \*.co.uk ! -name \*.com ! -name cache-\* -ls
You can also consider the test -regex.

Regarding your second question I think you can't avoid to use positional parameters. A bunch of checks at the beginning of the script it's easy to implement. Every time some condition is not satisfied, you can print a "usage" message and exit. "usage" can be a function at the beginning of the script, so that you have not to repeat the same lines of code all over the script.

salasi 08-01-2009 03:22 PM

Yes, that's neater than my version. Oddly though, it doesn't quite work as well for me in a way that I hadn't thought of.

It turns out that my version also cuts out some stuff like
Code:

.kde4/cache-lenny/http/d/download.kde.org_khotnewstuff_plasmathemes_previews_m100945-1.png_7369118b_freq
where your version doesn't get the ones that don't have the .org at the end of the term. Both are OK though (note that this isn't a criticism in any way; I didn't give any kind of spec for what it should do and I hadn't thought about this part.)

I probably should have given some more detail, although you have figured out the important parts; I was trying to have a look at a user's kde tree, to see what had been changed, if anything, in an update. Just looking at everything that has been changed since the update doesn't give a clear picture because it throws up lots of stuff that has been changed, but isn't relevant, such as cached records of websites, apparently. hence the isea of scrubbing the .com, .org and .co.uk, which gets a lot of the junk in my case.

Quote:

Regarding your second question I think you can't avoid to use positional parameters.
Ah well, I was hoping that there was a clever and simple way that I was overlooking, particularly as it is such a common problem.

Anyway, thank you for your help.


All times are GMT -5. The time now is 12:22 PM.