« January 2010 | Main | March 2010 »

February 28, 2010

Hidden Linux : Example commands - find

The basic syntax of the find command is simple:

find [where-to-look] [search-criteria] [what-to-do]

All parameters are optional (that's what those square brackets mean), and the defaults are:
find . (current directory), blank (show all files) and -print (display to the screen), so the command find
on its own will display the pathnames of all files in the current directory and all its subdirectories.

Basic searches

find -name myfile
Case-sensitive search for all files named "myfile" in the current directory or any of its subdirectories.

find -iname myfile
Case-insensitive search for all files named "myfile" in the current directory or any of its subdirectories. (This will also pick up files with names like "Myfile", "MYFILE" and "MyFiLe".)

find ~ -name my*
Find all files in the user's /home directory beginning with "my".

find ~ -name *file
Find all files in the user's /home directory ending in "file".

find -name "[0-9]*"
Find all files and directories starting with a number.

find -type d -name "[0-9]*"
Find only directories starting with a number.

find -type d -name "[a-c]*"
Find only directories beginning with the letters a, b or c (lowercase).


Controlling depth

By default find searches all subdirectories too. But you can control that with -maxdepth.

find . -maxdepth 1 -name myfile
Only search the current directory for files named "myfile".


Property searches

Find doesn't just work on file names!

find ~ -user fred
Find all files in /home directory owned by user fred.

find ~ -size +750k
Find all files in the /home directory larger then 7500k.

find ~ -size +500k -size -750k
Find all files in the /home directory larger then 500k but less than 750k.

find ~ -size +100M
Find all files in the /home directory greater than 100M.


Finding by time

-mtime is a measure of 24-hour periods, so ...

find -mtime 0
Finds all files that were modified in the last 24 hours.

find -mtime -2
Find all files that are 2 days old or newer.

find -mtime +30
Find all files that were modified more than 30 days ago.


-mmin is a measure of minutes, so ...

find -mmin -10
Finds all files that were modified less than 10 minutes ago.

find -mmin +5 -mmin -10
Find all files modified between 6 and 9 minutes ago.


Executing commands

If find returns a match, -exec command will execute command.

find -iname my* -type f -exec ls -lah '{}' \;
Find and list all files beginning with "my" (case insensitive).

find -size +500k -size -1000k -exec ls -lah '{}' \;
Find and list all files larger then 500k but less than 1,000k.

find -iname my* -exec file '{}' \;
Find all files beginning with "my" and display their file types.

find -iname my* -exec md5sum {} \;
Find all files beginning with "my" and display their md5sums.

You can even pipe output to other commands after the -exec ...
find . -type f -exec ls -sah {} \; | sort -n -r | head -10
Find, sort and list the 10 biggest files in the current directory (and its subdirectories).


Permission, owners and groups

You can also search based on permissions, owners or groups.

find -type f -user geoff
Find all files owned by user "geoff".

find -type f -group root
Find all files belonging to group "root".

find . -perm -o=w
Find all files in the current directory that are writeable by others

find . -perm /u=w,g=w
Find all files in the current directory that are writable by either their owner or their group.

find . -perm -g+w,u+w
Find all files in the current directory that are writable by both their owner and their group.

find . -perm -a+r -perm /a+w ! -perm /a+x
Find all files in the current directory that are readable by everybody (-perm -a+r), have at least one write bit set (-perm /a+w) but are not executable by anybody (-perm /a+x).


Tips & Tricks

If you're searching the entire system as an ordinary user, you'll get lots of "permission denied" messages from directories for which you don't have read permission. To avoid these, redirect the error messages to a null device and you won't see them:

find / -name myfile 2>/dev/null


Of course there's a great deal more to find -- check out man find for full details -- but it's only one tool in the command line arsenal. To my mind, if you're just seaching for files by name, you can't beat the updatedb/locate combination.

Got a useful find command? Leave a comment!



Previous Hidden Linux
Next Hidden Linux





Follow Geoff Palmer on Twitter

February 17, 2010

The daftness of data caps


I don't know about you, but I treat my download cap as a target. I have a 20GB-a-month TelstraClear plan, and I do my darndest to use every byte. Why? Because caps are essentially stupid. Or rather, they're badly designed.

If I'm away for a month and use 0 bytes, it costs just the same as if I'd used my full 20GB. This wouldn't be so galling if some allowance were made for under-utilising my capacity. It doesn't have to be a 1:1 thing or roll over from month to month, but some concession would be nice. Perhaps a 1:2 ratio that must be used within the next billing period; in this case an allowance to go up to 30GB in the next month seems reasonable.

That would also get around the other irritating thing about data caps, the punitive charges if you go a single byte over your limit. In the case of TelstraClear it's $2.95 per GB or part thereof.




That'll be $2.95 please!

What got me thinking about this was this BBC report. Apparently "the majority of broadband users in the UK pay for download limits that they come nowhere near reaching." Is this the case in NZ? How much of your limit do you use? Post a comment and let me know.


February 10, 2010

Hidden Linux : Example commands - df and du

The Linux command line is immensely powerful, especially if you combine it with handy popup access from an application like Yakuake, but sometimes you just want to know what string to type without having to wade through screens of man pages. That's what this occasional series is about.

(As always, you can type man <command_name> for full details about a command.)

df - Disk free space
Show disk usage and free space:
df

Show disk usage and free space in "human-readable" format (like 1K, 10M, 4.2G):
df -h

Show disk usage and free space in "human-readable" format but using powers of 1,000, not 1,024 (just like disk drive manufacturers do when quoting the sizes of their drives!):
df -H


du - Disk usage
Show file space usage in the current directory:
du

Show file space usage in the current directory in "human-readable" format:
du -h

Show file space usage in your home directory:
du -h ~

Sort usage to display the largest files last:
du -h | sort -n

Summarise the current directory's usage:
du -sh

Summarize usage by all level 1 sub-directories (not sub-sub-directories, etc.):
du -sh *

Show the file space used in /usr/share/icons:
du -sh /usr/share/icons/

Summarise the file space used by all sub-directories in /usr/share/icons:
du -sh /usr/share/icons/*

Show the size of all sub-directories in your home folder:
du -sh ~/*



Any more useful ones? Leave a comment!

Previous Hidden Linux
Next Hidden Linux


February 2, 2010

How trackable is your browser?

The Electronic Frontier Foundation have come up with an interesting toy called Panopticlick. With the tagline "How unique -- and trackable -- is your browser?" it checks the information you present to every website you visit -- and gives you a score! After clicking the Test Me button I discovered that only one in 165,415 browsers have the same fingerprint as mine and that my fingerprint converys 17.34 bits of identifying information. It then proceeds to list what it's learned.

The interesting figures (for me) were the Browser Plugin Details and System Fonts: 1 in 165,414 and 1 in 3,308 respectively. Though I knew quite a lot of stuff "leaks", I had no idea hosts could pick up such esoteric information.

Panopticlick of course works on a statistical basis, and with a dataset of only "several hundred thousand visitors" so far it's assessment isn't necessarily too sharp at present. Still, an interesting exercise. As they note in the FAQ;

"We'd have gotten better data by putting these tests in an invisible corner of a high-traffic website, but that simply isn't the EFF way when it comes to running an experiment like this: we wanted to make sure people knew they were participating, and let them know — even approximately — how rare/unique they were."

So how unique are you? Add a comment ...!

Subscribe
Newsletter & SubscriptionsPC World is New Zealand’s top selling computing and technology magazine.

It provides up-to-the-minute editorial, insight and buying advice for personal computing, cell phones, game consoles, digital entertainment and broadband.
SIGN UP
PCWorldUpdate
PC World's weekly round-up of tech news, gear and game reviews, software selections, and handy How Tos.