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

Move the Terminal cursor position with the mouse UNIX
Which Terminal gurus among us hasn't wasted too many minutes to count arrowing around the manipulate the cursor while deep within the confines of the command line?

The Terminal gurus who know this trick, I guess: Hold down the Option key and click where you'd like the cursor to move, and Terminal rushes the cursor that precise spot.
  Post a comment  •  Comments (18)  
  • Currently 4.25 / 5
  You rated: 5 / 5 (20 votes cast)
[12,708 views]  View Printable Version
Bash Function to open man pages in new terminal window UNIX

When I'm stumbling around the command line, I usually need to keep various man pages open for reference. I always forget I can get a dedicated man window from the Help or Contextual menus. This bash function allows you to open a man page in a new window directly from the command line.

function man { if [ $# -eq 1 ] ; then open x-man-page://$1 ; elif [ $# -eq 2 ] ; then open x-man-page://$1/$2 ; fi }

Just put this function in your .bashrc file, and when you use the man command, the information opens in Terminal's Man Page specific window setting style. If you don't want to override the default 'man' command operation, you can change the function name to something else.

This function expands on OS X's x-man-page url scheme. And of course, there are plenty of alternate ways to view man pages such as in preview, ManOpen, or Bwana.

Smallduck is the true author of this hint; I'm merely spreading the word.
  Post a comment  •  Comments (14)  
  • Currently 2.70 / 5
  You rated: 1 / 5 (10 votes cast)
[7,221 views]  View Printable Version
Prevent a Mac from sleeping from the command line UNIX
We've run a number of hints about preventing a Mac from sleeping, such as this and this.

I came across another really simple way to do this, using the pmset command. Just run this command in terminal:

pmset noidle

Terminal will display the following text:

Preventing idle sleep (^C to exit)...

To allow the Mac to go back to sleep, according to its current Energy Saver settings, just press Control-C.
  Post a comment  •  Comments (4)  
  • Currently 3.14 / 5
  You rated: 5 / 5 (7 votes cast)
[12,994 views]  View Printable Version
Peek inside zip archives from Terminal UNIX
Have you ever wanted to see what's in a Zip archive without unzipping it? Well, it turns out that there is a Terminal command you can use to do so: zipinfo. Just run zipinfo [filename] to get a list of files in an archive.

There are a number of interesting options to this command. The default behavior is to list the files in a short "ls -l" format, but you can also use the -m or -l options to have medium or long lists. You can use the -1 option to only show file names. And there are other useful options in the man zipinfo page.

One other useful item is the amount of space the archive takes up, and the amount of space saved. For example:

12 files, 10587791 bytes uncompressed, 9400060 bytes compressed: 11.2%

It's worth noting that this command has been around for a long time, and I was surprised to find that it's not mentioned here.
  Post a comment  •  Comments (1)  
  • Currently 3.22 / 5
  You rated: 1 / 5 (9 votes cast)
[7,539 views]  View Printable Version
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: 2 / 5 (11 votes cast)
[5,619 views]  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:

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)
[13,457 views]  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 <>
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:


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: 4 / 5 (10 votes cast)
[7,459 views]  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 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.80 / 5
  You rated: 1 / 5 (10 votes cast)
[11,700 views]  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:

# 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: 1 / 5 (10 votes cast)
[8,857 views]  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 1.47 / 5
  You rated: 2 / 5 (19 votes cast)
[4,856 views]  View Printable Version