Today's article has you using the 'find' command...

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
11,497
Reaction score
9,994
Credits
95,326
Previous articles have used the find command but this one finally covers how to find files by name. It also repeats my usual warning about why you don't parse the output of 'ls' and we use find instead. It's a pretty easy article.


I do like me some feedback.
 


The find command is pretty massive and has a huge amount of features, but you covered the most important ones. The thing i like about it is that you can also search entirely by type:

Code:
find -type d

find -type f

That's a way to find every directory and file within a directory hierarchy...i'm pretty sure it has more features than both sed and awk, which says a lot:

Code:
sisyphus@sisyphus:~$ man sed | wc -l
301
sisyphus@sisyphus:~$ man find | wc -l
1685
sisyphus@sisyphus:~$ man awk | wc -l
1081
 
The find command is pretty massive and has a huge amount of features, but you covered the most important ones.

I've taken the 'find' command in small bites, hoping to make it more approachable. There's just so much to it that it likely overwhelms new users.

It's a bit like ffmpeg... I'd like to cover some of that command, but it sure can be involved.
 
I've taken the 'find' command in small bites, hoping to make it more approachable. There's just so much to it that it likely overwhelms new users.

It's a bit like ffmpeg... I'd like to cover some of that command, but it sure can be involved.
If the focus is the new users, you could explain that a directory hierarchy is the directory and all its child directories. It wasn't obvious to me at first.
 
If the focus is the new users, you could explain that a directory hierarchy is the directory and all its child directories. It wasn't obvious to me at first.

I've previously covered directories pretty heavily and aim for a specific word count. If it's too wordy, people gloss right over it. (I've monitored heat maps.) I could probably add that it's 'recursive', but that was obvious (to me) when I mentioned you can search everywhere with picking your directory's root "/".
 
If it's too wordy, people gloss right over it.
That's true, but you also don't want future users of the info to make up their own ideas about what they think you mean either. Letting people do that with general fiction/non is fine, but this is just too strict and rule oriented for that.

Also, another problem is that using the root directory for this purpose just isn't very practical...the home directory is better for examples, and you could easily explain directory hierarchies with simple everyday language in that context.
 
and you could easily explain directory hierarchies with simple everyday language in that context.

LOL The best you're gonna get is me mentioning that it's recursive (and maybe saying what that means). I'm definitely not gonna define the directory hierarchy and explain it in every article with directories. They'll figure it out.
 
LOL The best you're gonna get is me mentioning that it's recursive (and maybe saying what that means). I'm definitely not gonna define the directory hierarchy and explain it in every article with directories. They'll figure it out.
Nobody ever figures it out without experimenting so the terse description is fine i guess, i was only trying to help you...i have a lot of experience with the find command, even though i will probably never understand all the features.
 
i was only trying to help you..

Oh, I appreciate it, I just have to stay within the realm of what folks are going to read and how much time I can dedicate to each article. I'll figure out a way to work in a bit about it being recursive - but probably not until later tonight or tomorrow. I'm in the middle of a lovely blizzard and I'm going outside to play in it.
 
CrazedNerd wrote:
problem is that using the root directory for this purpose just isn't very practical...the home directory is better for examples, and you could easily explain directory hierarchies with simple everyday language in that context.
I find this an interesting point in so far as it directs attention to useful ways of learning. I've found that creating a learning "space" to work within on the filesystem is an effective way of being able to use commands, watch their effects and do it with a lower risk of unintended consequences. For example, the creation of directory with subdirectories and files sprinkled throughout it where one can go finding, deleting, moving, renaming, chmoding, chowning, linking files etc to one's heart's content as they become familiar with what gives.
 
CrazedNerd wrote:

I find this an interesting point in so far as it directs attention to useful ways of learning. I've found that creating a learning "space" to work within on the filesystem is an effective way of being able to use commands, watch their effects and do it with a lower risk of unintended consequences. For example, the creation of directory with subdirectories and files sprinkled throughout it where one can go finding, deleting, moving, renaming, chmoding, chowning, linking files etc to one's heart's content as they become familiar with what gives.
I only came to that conclusion about find because I tried to make a script that would search the whole OS for a file containing a certain term in the name or in the file using the read prompt, grep, and find, and i concluded that using find in that way was cumbersome and not a great idea.
 
I've found that creating a learning "space" to work within on the filesystem is an effective way of being able to use commands, watch their effects and do it with a lower risk of unintended consequences.

Ha! I've encouraged such in prior articles and have even given people files to download and use to explore.

Those articles get read thousands of times and maybe 3 people download the example file.

(I do sometimes have folks make special directories to play with, and then tell 'em how to clean up the mess after that. I haven't done so lately as I figure about the same percentage of people are going to do that.)

I'm lucky if I can keep their attention for more than 3 minutes. They skip the intro and ignore the closure.

I did heat maps for the longest time, until just about a week ago. I used Clarity from Microsoft to track (unknown) individuals through the site. It didn't identify anyone, but let me track individuals (and trends).

Heck, want some 'proof'? Search the site for the word 'charity', which was one of my ways of testing how accurate the heat tracking was. Feel free to claim the prize. That article has been "read" many hundreds of times.

Also, the scary part (or perhaps sad, or amazing) is that getting 3 minutes of their attention puts my site above average. It's well above average.

LOL

Ah well...

I'm gonna actually go be productive in the snow (shortly, I'll check the posts first). I'd love a glass of wine, but I'll be driving a big ol' plow truck with a thousand pounds of sand in the back. Gotta be sober for this sort of stuff, but it sure is fun!
 
Those articles get read thousands of times and maybe 3 people download the example file.
I personally have experimented with unix commands a lot over the past year, but i have also discovered that doing that is pretty rare overall...it's quite a lonely hobby.
 
Here's a little fun with find. Please don't pay attention to the mathmagics. This is just the formula. ;)

Say you want to find how many files are older than 13 years... It would look something like this. Note: for accuracy, you might want use 365.25 to account for leap years.
find ./ -name '*' -type f -mtime +$((365.25*13)) | wc -l

Now... you want to know how many files you have that are between 12 and 13 years old.
find ./ -name '*' -type f -mtime +$((365.25*12)) -mtime -$((365.25*l13)) | wc -l
Note that the "*" can be changed to a partial file name or extension or whatever is necessary to limit the output to just what you want to see.

Here's a fairly harry find command from back in the day. It kinda shows how to include and exclude paths etc...
find /shares/root/cad/ /shares/root/catia/ \( ! -path '/accessct/' -a ! -path '/util/' -a ! -path '/pcplanet/tools/' -a ! -path '/CAD/IT/' -a ! -path '/pcplanet/text/' -a ! -path '/CAD/ManualCAD/' \) -type f -daystart -mtime +$cutoff_days -printf "%Tm/%Td/%TY %TT\t%p\n" | tee -a $search
 
I am good with the first two. The third example makes my eyeballs cross, but I think it's (probably, I assume it works in the right situation) a good example of how involved the 'find' command can actually be.
 
I really don't know what Dart is doing above, but I had been wondering the way that you exclude certain directories from find, i researched it today and found you do it like this:

Code:
find -not -path "./<directory>/*"
 
I really don't know what Dart is doing above,

The first two are pretty easy. The mtime is the key. The first one is files older than that date and the second one is files between those dates.

The third one is gibberish to me! (Not quite, I can kinda suss out what it's doing.)

For some reason, the article was turned into a draft and no longer published. That has been fixed.
 
Sorry, I was kinda pressed for time. I posted it on my lunch break. The third example is about giving a starting path and adding exclusions. To be honest... I came up with this back in the early 2000's. It took quite a bit of time to iron out back then, but it was something I held onto. :) At the time I was responsible for quite a large number of CAD files and their whereabouts.

If you break it down, it's not too bad. The find command gives the starting path, the rest exclude sub folders. Those that are not excluded sends the files into an output folder called $search

All the exclusions are there just to show how deep you can go.

Hope this helps.
 
Sorry, I was kinda pressed for time. I posted it on my lunch break. The third example is about giving a starting path and adding exclusions. To be honest... I came up with this back in the early 2000's. It took quite a bit of time to iron out back then, but it was something I held onto. :) At the time I was responsible for quite a large number of CAD files and their whereabouts.

If you break it down, it's not too bad. The find command gives the starting path, the rest exclude sub folders. Those that are not excluded sends the files into an output folder called $search

All the exclusions are there just to show how deep you can go.

Hope this helps.
what is the "-a" for though?
 

Members online


Top