Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Find users that use the most disk space UNIX
Find which users take up the most disk space. This can of course be used to indentify any number of users so I picked 10 as an arbitrary number.

If you are already root (unlikely, but possible) you can remove the sudo -s part. Here's the command:
sudo -s du -sm /Users/* | sort -nr | head -n 10
Generally you would have to make sure that you use sudo -s or it will give a few Permission denied errors before finally spitting out the results, and they may be incorrect. The directory structure should start with the /Users Directory and then it will recursively perform the operation. This may miss any folders outside of the normal user space, but there shouldn't normally be any user data there.

[crarko adds: I tested this, and it works as described. There are third party utilities that will give the same results with a GUI, but this is pretty handy to have for use on remote machines over an ssh connection. It can take a while to complete, so be patient. Also I noticed in Activity Monitor the du process was using a lot of cpu, so it's best to do this while not running other cpu (or I suppose disk) intensive programs.]
  Post a comment  •  Comments (4)  
  • Currently 2.45 / 5
  You rated: 4 / 5 (11 votes cast)
 
[1,944 views] Email Article To a Friend View Printable Version
Flatten a directory structure UNIX
There may be times when you want to consolidate all the files in a directory and its sub-directories (or a folder and its sub-folders) into a single directory or folder. For example, you may have a folder with sub-folders for years, and other sub-folders in each year folder for months, and you may want to move files in the month folders all to the top level.

Doing this manually is a complex and time-consuming process. While you might be able to do this by using a search - for example, if all the files are, say, Excel files, you can search for Excel files in the top folder, then just copy them all to a new folder - if there are lots of different types of files, this wouldn't make things easier.

Fortunately, there's a way to do this from a command line. On the BedroomLAN blog, Alexios presents two commands that will do this:

cd $ROOT_DIRECTORY
find -type f -print0 | xargs -0 -I%%% cp %%% $FLAT_DIRECTORY


Replace $ROOT_DIRECTORY with the top level directory containing all the sub-directories and files, and replace $FLAT_DIRECTORY with the directory you wish to contain all the files. Note that this command will overwrite any files with the same name, so if you don't have uniquely named files, it's not ideal.

You can also use the ln command instead of the cp command, and this will not overwrite files, but will give error messages if there are duplicate file names. See the blog post for more details on this. H/t to robg for pointing this out.
  Post a comment  •  Comments (12)  
  • Currently 1.57 / 5
  You rated: 1 / 5 (7 votes cast)
 
[3,486 views] Email Article To a Friend View Printable Version
Extract names and emails from a text file UNIX
I have a recurring need to extract full names and email addresses from a plaintext archive of email messages. The archive is created by selecting a bunch of emails in Mail, copying them, pasting into TextEdit, and converting to plain text.

For each message in the file, the first line contains the information I wanted:
From: Joe Example <joe@example.com>
I wanted one email address per line, suitable for pasting into another location. I am far from an expert with the bash shell, but here's what I came up with—I imagine there are many more efficient ways to do this, as I'm sure experienced perl, sed, awk, etc. users may point out. Note that this is highly dependent on the format created by Apple's Mail app in OS X 10.8.

grep 'From:' /path/to/archive.txt | cut -f2 -d\< | cut -f1 -d\> | pbcopy

The grep bit pulls out the entire From: line, then the first cut command grabs the email address and the trailing close-bracket, by setting the delimiter to an open bracket. The second cut eliminates the closing bracket, by setting that as the delimiter. The output will be one email address per line, sitting on your clipboard ready for pasting. (To debug, just remove the | pbcopy bit to see the output.)

I also wanted to extract the names, and came up with a variant to do just that:

grep 'From:' ~/Desktop/testfile.txt | sed -e 's/: /:^/g' | sed -e 's/ \</^\</g' | cut -f2 -d^ | pbcopy

This one is messier, as names can contain one or more spaces. After getting the From: line, sed is used (twice) to add a carat delimiter immediately after From:, and immediately before the opening bracket of the email address. I then used cut, with the delimiter changed to the carat, to extract the full name (field two) from the found lines. Again, the results are copied to the clipboard; leave this bit off for debugging.

With the names and addresses extracted, it's fairly easy to do other stuff with them. In my case, I'm reading them into a couple of array variables in a bash script, so I can then output a name and email address pair to consecutive locations on my multi-pasteboard. If you want to use the names in an array in a bash script, you'll want to change the array delimiter from a space to a newline:

IFS='
'

Without this, your array will get split anywhere there's a space in the name values ... or so I've heard, not that it's ever happened to me!
  Post a comment  •  Comments (12)  
  • Currently 3.20 / 5
  You rated: 1 / 5 (10 votes cast)
 
[3,394 views] Email Article To a Friend View Printable Version
Control the Finder with the Terminal UNIX
There are several ways to open a Terminal window to the current directory in the Finder. But wouldn't it be useful if you could do the reverse and open a Finder window to the current Terminal directory? Well, you can, and you can completely control the Finder from the terminal.

I have put the code on github with full instructions on how to set it up. It works by using bash_completion, .bash_profile with some applescript to control the Finder, .inputrc and .bash_aliases.

Here are some of the features:
  • Changing a directory in the Terminal opens the same directory in the Finder.
  • You can change the Finder window view from the Terminal (column, list, icon views).
  • It is case insensitve, you can press Tab for menu completion, and Shift-Tab to expand bash aliases.
  • Open a Terminal directory to the current Finder window.
This code will work with both the Terminal.app and iTerm2 and should work with older macs as well

[kirkmc adds: I haven't tested this, but it sounds very useful.]
  Post a comment  •  Comments (11)  
  • Currently 2.67 / 5
  You rated: 1 / 5 (9 votes cast)
 
[5,938 views] Email Article To a Friend View Printable Version
See how long a given process has been running UNIX
I wanted to find out how long a certain background process had been running. There's a column for CPU Time in Activity Monitor, but that's not real clock time.

It turns out you can get this information with ps, via the etime keyword. So to get a list of every running process, in decreasing order of run time, just use this command:
ps -ax -o etime,command -c
To see the results for a single process, just add a grep at the end for the process' name. For example:
$ ps -ax -o etime,command -c | grep AppleVNCServer
03-08:09:16 AppleVNCServer
So on my Mac, the AppleVNCServer has been running for three days, eight hours, nine minutes, and 16 seconds. I have a need to do this pretty regularly, so I turned it into a simple command line app:
#!/bin/bash

# Display the time a given process has been running
# Use the process name when calling the command

ps -ax -o etime,command -c | grep $1
I saved that to a file named psup, and made it executable with chmod 755 psup. Now I can just type psup SomeProcess to see the uptime for SomeProcess.
  Post a comment  •  Comments (9)  
  • Currently 2.80 / 5
  You rated: 3 / 5 (10 votes cast)
 
[4,485 views] Email Article To a Friend View Printable Version
See details of an app's virtual size UNIX
If you use top in Terminal, you may occasionally see apps with huge VSIZE values. I know this because Witch, one of our apps, is an example of such—it's VSIZE can exceed 11GB.

In trying to figure out why this was so (short answer seems to be: we can't control it, but it's not a problem), I ran across an interesting command, vmmap. This command will spew out a ton of detail about virtual memory usage. Stringing a couple Unix commands together, though, you can extract just the summary portion of the report.
read more (186 words)   Post a comment  •  Comments (1)  
  • Currently 2.29 / 5
  You rated: 1 / 5 (7 votes cast)
 
[3,398 views] Email Article To a Friend View Printable Version
Quickly eject all disks from the command line UNIX
Instead of using the following to eject all disks:
osascript -e 'tell "Finder" to eject (every disk)' 
I've found that this is much faster:
find /Volumes -maxdepth 1 -not -user root -print0 | xargs -0 diskutil eject 
Run this command from Terminal, or in a shell script, to eject all your local disks.

To eject network volumes, this code works:
find /Volumes -maxdepth 1 -not -user root -print0 | xargs -0 umount 
[kirkmc adds: The problem with this hint is that each line of code works for different types of volumes. If you have both local and network volumes, you need to use both; you could, of course, stick them together with a semi-colon separating them so they are, in effect, a single command.. In my tests, it's not really faster than the osascript code above, but I'm posting it because some may find it useful. If you have different results in speed or efficacy, please post in the comments.]
  Post a comment  •  Comments (2)  
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[4,298 views] Email Article To a Friend View Printable Version
Send Terminal output to iCloud UNIX
Imagine that you run a script automatically on your Mac, and that you want to check the result of that script. There are many ways you could do this, such as remotely connecting to the Mac, or sending the results by e-mail. But with iCloud, you can also save the output to a file and put it on iCloud, where you can access it with your favorite iCloud-compatible text editor on another Mac, or on an iOS device.

To do so, simply send the output of the script to a file like this:

~/Library/Mobile\ Documents/com\~apple\~TextEdit/Documents/filename.txt

So, to save a list of a directory's contents, you'd use this:

ls -al > ~/Library/Mobile\ Documents/com\~apple\~TextEdit/Documents/list.txt

That saves a file called list.txt in TextEdit's Documents folder. Look inside the ~/Library/Mobile\ Documents folder for the paths to other apps you have that can use iCloud. Each folder in the Mobile Documents folder has a Documents sub-folder. Depending on the app, you may be able to access the files on another Mac or on iOS.
  Post a comment  •  Comments (4)  
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[2,991 views] Email Article To a Friend View Printable Version
Command-drag to Terminal to change directory UNIX
If you hold down the Command key while dragging an icon to a Terminal window, it will cd to either the directory you dragged (if it's a folder) or the directory containing the item you dragged (if it's a file).

I don't know when this feature appeared; it might not be specific to 10.8, but that's where I discovered it.

[kirkmc adds: This is a really neat feature. Also, if you Command-drag an application or other bundle, Terminal will cd to inside that bundle. This is great if you need to access files within an application bundle, for example. The comments are saying that this is indeed 10.8 only.]
  Post a comment  •  Comments (14)  
  • Currently 4.00 / 5
  You rated: 5 / 5 (16 votes cast)
 
[6,363 views] Email Article To a Friend View Printable Version
Script to delete huge sparsebundle images UNIX
I needed to restart a backup for one of my Macs, which had an 800 GB disk image on my Time Capsule. It turned out to be impossible to delete the disk image - in Finder, it would just not move to the Trash, even after waiting for several hours.

I was looking for advice and most of the suggestions involved reformatting the Time Capsule, but I couldn't do this, as I had other Macs being backed up there.

I looked for a way to do this from the command line. The Time Machine disk image - a sparsebundle, a special type of disk image that can increase in size as needed - is actually a package, which has "bands" inside, many smaller files that are named hexadecimally. In this case, there were thousands of them. So many, that even deleting them from the command line didn't work - commands like rm find limitations in the lengths of lists parsed by recursion or wildcards, so a basic command like rm -f didn't work.

I ended up writing a little script, which removes the files one by one. The biggest trick here is actually the %x switch which changes $i into a hexadecimal number.

You can then either copy and save something like this as a shell script:

for i in {0..1000000}
do
rm -rv /Volumes/Your Network\ Disk/Your\ Mac.sparsebundle/bands/$(printf "%x" $i)
Done


where you replace Your\ Network\ Disk and Your\ Mac with your details.

Or, you can copy and paste this line into Terminal:
for i in {0..1000000}; do rm -rv
then add a space, then drag and drop the disk image, and then (you might need to remove one space this time) add:

/bands/$(printf "%x" $i); done

Then just watch the bands being deleted, and when the script starts deleting non-existing bands, just kill it with ctrl-X.

With no bands, the sparsebundle gets deleted immediately.

[kirkmc adds: I haven't tested this. My first thought was that this seemed like overkill, but corresponding with the submitter made me realize the extent of the problem. Here is some more information on this limitation.]
  Post a comment  •  Comments (20)  
  • Currently 2.80 / 5
  You rated: 1 / 5 (10 votes cast)
 
[9,323 views] Email Article To a Friend View Printable Version