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

A script to add icon previews and retain time stamps UNIX
sips is a fantastic command line tool which can add icons to image files so that they don't have to be auto-generated when you open a folder that has Show Icon Preview enabled. However, I never used it on my important images because it updates the files' time stamps as well. This means that if I generate icons today for images I took last year, those files would show up in the Finder as having been created today.

I finally couldn't take it any more, so I wrote a bash script to generate icons while preserving file times. It works on a variety of test files that I've used, including filenames with spaces, but since I'm no bash guru, it could contain some pitfalls.

[robg adds: This worked for me in testing on a few different image files; the icons were created without changing the files' time stamps.]
  Post a comment  •  Comments (7)  
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,324 views] Email Article To a Friend View Printable Version
Fix broken SSH Public Key Authentication UNIX
Ever since I learned of SSH, I have wanted to set it up so that I could automatically run remote commands (like rsync) on the other Macs on our home network. The proper way to do this, of course, is with passphrase-free public key authentication. But try as I might, I simply could not convince SSH to use public key authentication! Eventually I discovered the reason in an obscure mailing list: If permissions are set incorrectly on the home directory, SSH will refuse perfectly good authentication keys.

The solution: Open Terminal and type chmod g-w ~/.

SSH is now entirely happy to authenticate using keys.

[robg adds: Running Disk Utility's permissions repair should also fix home folder permissions, though I'm not 100% positive about that.]
  Post a comment  •  Comments (10)  
  • Currently 2.80 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[16,714 views] Email Article To a Friend View Printable Version
A shell script to upload SSH keys to remote machines UNIX
This is a script that I initially found online and have modified to be a bit more useful. Basically it automates the process of uploading your SSH key to a remote host that you SSH into by doing the following:
  1. Creates a .ssh directory if there is not one already there, and sets the correct permissions on it.
  2. Puts your key in the authorized_keys file (and creates it if it was not there already), and changes the permissions on it.
Here's the code:

#!/bin/sh

KEY="$HOME/.ssh/id_dsa.pub"

if [ ! -f ~/.ssh/id_dsa.pub ];then
    echo "private key not found at $KEY"
    echo "* please create it with "ssh-keygen -t dsa" *"
    echo "* to login to the remote host without a password, don't give the key you create with ssh-keygen a password! *"
    exit
fi

if [ -z $1 ];then
    echo "Please specify user@host.tld as the first switch to this script"
    exit
fi

echo "Putting your key on $1... "

KEYCODE=`cat $KEY`
ssh -q $1 "mkdir ~/.ssh 2>/dev/null; chmod 700 ~/.ssh; echo "$KEYCODE" >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys"

echo "done!"

If you SSH into many machines, the script can save you a lot of manual work.

[robg adds: I tested this, and it works as described -- I edited it to reflect the fact that I have an RSA key, not a DSA key (so I just changed id_dsa.pub to id_rsa.pub). When you run the script, you'll be prompted for the password on the remote machine; after it runs, you can connect without a password (if you're not using a passphrase).]
  Post a comment  •  Comments (15)  
  • Currently 2.40 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[47,496 views] Email Article To a Friend View Printable Version
A simple way to open files in X11 applications UNIX
Here is the easiest way, to my knowledge, to associate file extensions with X11 applications: just use the open source X11 Extension program. On the disk image, you'll find a new ExtManager System Preferences panel. Install it, then open it in System Preferences. The next step is to define file extensions and commands associated with them (e.g. /sw/bin/gv for EPS files).

There might be a small problem if you're using applications from outside standard PATHs (e.g. from Fink); in that case, you have to pass the PATH environmental variable to the X11 window manager. For some reason, the Apple xterm fails to assign the proper values to PATH if they're included in the .xinitrc file, and it doesn't read the .bash_profile file either. As a workoround, I suggest creating a .bashrc file with a single line (assuming the paths are already defined in the .bash_profile file, which is a routine for Fink and MacPorts users):
source .bash_profile
You should also set up X11 to launch at login, and you can then assign X11 apps simply by right-clicking (luckily Apple people reinvented the wheel and found out that the mouse can have multiple buttons...) the file and choosing ExtManager as the application to use to open the file.

Another small hint: to get the mouse support in mc, add this line to .bash_profile:
export TERM=dtterm
[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (9)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[9,679 views] Email Article To a Friend View Printable Version
A perl script to find large directories in a given folder UNIX
Here's a little perl script that can be used to find the top ten largest directories from the argument directories. To use it, save it as dirsize in your user's .bin folder (and make it executable with chmod a+x dirsize), modify your .bashrc to add ~/bin to your path, then run something like this in Terminal: dirsize ~/*. Here's the code:

#!/usr/bin/perl
use strict;
die "usage: $0 <directories>\n" unless @ARGV;
@ARGV = map { "'$_'" } @ARGV;
my @results = `du -hs @ARGV`;
@results = sort human_sort @results;
@results = @results[0..9];
print @results;

#---------------------------------------------------------------------------
sub human_sort {
	my ($size_a) = $a =~ /^(\S+)/;
	my ($size_b) = $b =~ /^(\S+)/;

	$size_a = $1 * 1024 if $size_a =~ /^(.*)k$/;
	$size_a = $1 * 1024 * 1024 if $size_a =~ /^(.*)M$/;
	$size_a = $1 * 1024 * 1024 * 1024 if $size_a =~ /^(.*)G$/;

	$size_b = $1 * 1024 if $size_b =~ /^(.*)k$/;
	$size_b = $1 * 1024 * 1024 if $size_b =~ /^(.*)M$/;
	$size_b = $1 * 1024 * 1024 * 1024 if $size_b =~ /^(.*)G$/;

	return $size_b <=> $size_a;
}

[robg adds: I tested this, and it seems to work as expected -- it will take a while to run if you set it to work on a large tree structure.]
  Post a comment  •  Comments (9)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[10,518 views] Email Article To a Friend View Printable Version
A script to ease scp for files with odd names UNIX
Here's a perl script that will escape filenames and send the files to a remote server using scp. I save it as scp-to-coppit.org in my user's bin folder, and make it executable (chmod a+x scp-to-coppit.org). Then from the command line, I can do this: scp-to-coppit.org File with weird char's.txt. Here's the code:
#!/usr/bin/perl

@ARGV = map { s/'/\\'/g; $_; } @ARGV;
my $files = "'" . join("' '", @ARGV) . "'";
my $results = `/usr/bin/scp -rBq $files dcoppit\@coppit.org:.`;
print "Output: $results" if $results ne '';
[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (12)  
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,516 views] Email Article To a Friend View Printable Version
A basic command and diagnostics shell script UNIX
I wrote a shell script at work to automate some of our Zenworks imaging solutions, since it wasn't very intuitive on how you interact with them. Since then, I have been making more scripts just to get some practice in. Here's one example.

I wrote a basic script that will allow users to run some Terminal commands that may help them with their problems. For instnace, if you are trying to get help on the forums here or elsewhere, you can run the script to help those trying to help you. It will display a monthly calendar, the number of logged in users, and then a menu of commands to run; just enter the number of the item you'd like to run and press Return. Enter 0 and Return to exit. Here's the code:

#!/bin/bash

#This is a menu for basic diagnostics by Thomas Larkin

echo "Hello $USER, Welcome to the Tom's Diagnostic script!"
echo "Today is  ";date
echo "Number of user logged in : " ; who | wc -l
echo "Calendar"
cal

selection=
until [ "$selection" = "0" ]; do
    echo ""
    echo "Select an option please"
    echo "1 - Display all objects in /Volumes"
    echo "2 - Display total disk usage of /, this may take a while.  You will be prompted for admin access"
    echo "3 - Display the disk usage of my Home Directory, this may take a while"
    echo "4 - Print the contents of /var/log/system.log"
    echo "5 - List all current users logged in this computer"
    echo "6 - Display my Network Settings and Information"
    echo "7 - Display my BASH command paths"	
    echo "8 - Display all the current running processes"
    echo "9 - Display current resources being used"	
    echo "10 - Display the print error log"
    echo "11 - Display the crash reporter log"
    echo "12 - Run Verify permissions on the boot volume"	
    echo "13 - Run Repair Permissions on the boot volume"
    echo "14 - Run verify volume on the boot volume"
    echo "15 - list all information of boot volume"
    echo "0 - exit program"
    echo ""
    echo -n "Enter selection: "
    read selection
    echo ""
    case $selection in
       1 ) ls -al /Volumes ;;
       2 ) sudo du -h / | sort ;;
       3 ) du -a -h /Users/$USER | sort ;;
	   4 ) cat /var/log/system.log ;;
	   5 ) finger -h ;;
	   6 ) ifconfig ;;
	   7 ) echo $PATH ;;
	   8 ) ps -A ;;
	   9 ) top -s5 20 ;;
       10 ) cat /var/log/cups/error_log ;;
       11 ) cat /var/log/crashreporter.log ;;
       12 ) diskutil verifyPermissions / ;;
       13 ) diskutil repairPermissions / ;;
       14 ) diskutil verifyVolume / ;;
       15 ) diskutil list / ;;
	   0 ) exit ;;
       * ) echo "Please enter a valid option"
    esac
done

[robg adds: I tested this hint, and it works as described. Copy and paste the text into your favorite pure text editor (or any Terminal text editor, such as vi or pico. Save it, then open Terminal and cd to the spot where you saved it. Make it executable via chmod a+x scriptname.]
  Post a comment  •  Comments (4)  
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[12,028 views] Email Article To a Friend View Printable Version
Advanced readline settings for bash in Terminal UNIX
The bash shell used in the Terminal application can be tweaked a little for better interactive experience, by saving the following lines as .intputrc in your home directory:
set editing-mode vi
set show-all-if-ambiguous on
set completion-ignore-case on
set meta-flag on
set convert-meta off
set output-meta on
set bell-style visible
To be more precise, the .inputrc file changes some default settings for the readline library, which the bash shell uses when being used in interactive mode. For more options, see the readline man page. If you are more used to the emacs keybindings to edit the command line, omit the editing-mode line in the .inputrc file.

[robg adds: We've run a number of these before in separate hints, but there are a couple of new ones in this list. For a list of all the possible variables and an explanation of what each controls, type man bash, then press the forward-slash (/) to search, and type Readline Variables as the search term (capitalization counts).]
  Post a comment  •  Comments (8)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[18,712 views] Email Article To a Friend View Printable Version
Use a free program to simplify dragging from Terminal UNIX
For a long time I've really liked that you can drag files from the finder (or elsewhere) to a Terminal window, where they appear as a path. Unfortunately, there has never been a nice way to drag from the Terminal. For example, if I have a new tarball that I want to send someone, I have to click Attach in Mail, and then find the file using the Open sheet.

I've just written a program -- Drag.app -- that does allow you to drag from Terminal, and it is available for free, with source. It adds a new command (shell script), drag , which is usable from Terminal. When executed, it opens a new window with a file icon that can be dragged as normal. When the drag is complete, the app quits so your desktop isn't cluttered and you can get back to the command line.

The closest I've seen to this before is the open command. If you open the enclosing directory of a file, then it will be displayed in the Finder -- you can find and drag a file from there. That approach has the downside that you'll sometimes get a .DS_Store file, and it can still be hard to find the file you want if the directory has a lot of files in it. My command cuts to the chase and lets you drag the file(s) you want, then gets out of your way.

[robg adds: I tried this, and it worked well. The download includes a compiled Intel Mac version; you'll have to compile your own for PowerPC, and that's left as an exercise to the reader...]
  Post a comment  •  Comments (27)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[7,227 views] Email Article To a Friend View Printable Version
Batch rename image files sequentially via perl script UNIX
I wanted to use the QuickTime "Image Sequence" feature to make rapid MPG movie out of a folder of JPGs. The problem was there were 600 pictures taken with my iSight over seven months, so I used a simple perl script to bulk-rename the files (it actually uses cp, so your original files are untouched). After running the script, open QuickTime, choose File » Open Image Sequence, and select 1.jpg, then the frame rate, etc. Here's an example of the finished product. (I used the iSight auto-capture hint I submitted earlier this year.)

Here is the perl (rename.pl):
#!/usr/bin/perl

$iteration=1;

foreach my $file (`ls *.jpg`) {
    chop($file);
    system("cp $file $iteration.jpg;");
    $iteration++;
    }
[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (12)  
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[17,886 views] Email Article To a Friend View Printable Version