Hide in your shell


Your Mac has a UNIX core - learn to use it.

The “command line” on your Mac can be accessed using the Terminal in /Applications/Utilities.

If you’re not familiar with shell scripting, there’s a good tutorial here. You can also find, run or manage shell commands using the free CLIX utility from Rixstep.


These commands are ones I use frequently - in many instances they are given a pretty Cocoa front-end and a hefty price-tag when you already have all this functionality inside your computer.

For more information about any of these commands, type in man command-name, where command-name is the name of the command you want information about (type q to quit the man page once you’re done).

Downloading

The curl command is invaluable when it comes to downloading. To download a file from a specific URL (be it ftp or http), use the command:

curl -o local-file.ext http://www.url.com/file.ext

If you were downloading something, but your internet connection died, or you had to stop it for some other reason (even if you were downloading with some other program such as your browser), you can use curl to resume the download:

curl -C - -o local-file.ext http://remote.tld/file.ext

Very nifty!

Compression

A few different compression formats. Bzip2 generally provides the most compressions, but for text, gzip is often better. To compress multiple files (ie. folders), you’ll need to use either the tar or zip command.

bzip2 file
gzip file
zip output files
tar -cjf output.tbz files
tar -czf file.tgz file

Cleaning

If you download the trim-app shell script, you can do a lot of cleaning up of your system very easily. Move it to /usr/bin/, then you can do the following:

Remove .DS_Store files from the current directory:

trim-app -d

Trim universal binaries to a single architecture (use i386 instead of ppc for Intel):

trim-app -a ppc

Compress tiff images:

trim-app -t

Clean resource forks:

trim-app -r

Or all of the above:

trim-app --all ppc

Since trim-app is a shell script, you can open it in a text editor to see how it works. You can download an uncompressed version of trim-app here to dissect it.

Misc

If you need to search the contents of files for a particular string, use the following:

grep -r "search-query" *

Find out the size of the current directory in kilobytes:

du -csk

Delete all languages other than English in applications in the current folder.

find . -name \*.lproj -and \! \( -name En\* -or -name en\* \) -exec rm -rf {} \;

Create a symbolic link:

ln -s source dest

Create an .icns file from a tiff image:

tiff2icns image.tiff

These are just a few of the useful shell commands available - explore a bit and you’ll find plenty of fantastic tools that will make your life much, much easier.

Now it’s your turn - know any super time-saving commands?


Back to Top ↑

15 Comments so far

Leave a comment
  1. 1

    This is wrong:

    find . -type f | while read line; do echo “$line” | grep “search-query” > /dev/null && echo “$line”; done

    This is just a bad way of searching file names. If you want to search the contents of files you just do:
    grep -R “search-string” .

    Also, the *.lproj in “find . -name *.lproj …” should be between quotes so it won’t capture an *.lproj file you may have on the current directory

    BTW: grep also accepts regexes with the -E flag, which is nice ;)

  2. 2

    The search in the contents of files won’t work at all. You’re finding the list of files, then you’re reading the list of file names and grep’ing through that. So really what you’re doing is searching for a file that contains “search-query” in the file name. If you wanted to do that, it’d be much faster to do:

    find ./ -iname ‘search-query

    If you want to search the file contents instead, just use:

    grep -r “search-query” *

    Or, since that won’t search files beginning with a dot:

    find ./|xargs grep “search-query”

    (or similarly with -exec like used in the later line)

    Also, be careful when copying/pasting the lines above. Your blog turned some of the quotes into smart-quotes which won’t copy and paste very easily.

  3. 3

    The ones I use all the time are hdiutil, ssh, svn, php, ruby, irb, rails, rake, mysql, defaults, killall / kill, grep, ps…

    but I think the most useful to the readers here would be hdiutil and defaults

  4. 4

    This is wrong:
    find . -type f | while read line; do echo “$line” | grep “search-query” > /dev/null && echo “$line”; done

    The search in the contents of files won’t work at all

    You’re both right. I just pasted that in without looking (from the wrong place).

    Searching for file names is just as easy as find . -name "query".

    the *.lproj in “find . -name *.lproj …” should be between quotes so it won’t capture an *.lproj file

    I’ve never seen one of those before :)

    Your blog turned some of the quotes into smart-quotes

    Thanks, that’s fixed.

    I think the most useful to the readers here would be hdiutil and defaults

    They are quite handy sometimes :)

  5. 5

    Especially defaults is a cornucopia. Most ’system maintenance’ tools rely on this extensively. And it’s really cool because of the underlying file format expected: the property list aka a ‘dictionary’. Not everything is easily accessible/modifiable from the command line (arrays et al) but it can’t be denied it’s totally cool. The CLIX mentioned above has hundreds upon hundreds of good examples.

  6. 6

    I wouldn’t move trim-app to /usr/bin. That may be just me but I wouldn’t do it. I prefer to leave that stuff alone. It’s all locked down and I want to keep it that way. Some people create a ‘bin’ in their own home area. Whatever. The system is set to automatically look in a number of user specific locations.

    Paths
    -----
    NSAllDomainsMask, NSAllApplicationsDirectory
    (
        "~/Applications",
        "~/Applications/Utilities",
        "~/Developer/Applications",
        "~/Applications/Demos",
        ...
    )
  7. 7

    I use rsync -aE [source] [destination] to keep my stuff backed up.

  8. 8

    Whoever uses find and grep on Mac OS X should probably use -print0/-0:

    $ find . -type f -print0 | xargs -0 grep searchstring

    That way it won’t fail where there are spaces in filenames.

  9. 9

    $ find . -type f -print0 | xargs -0 grep searchstring
    That way it won’t fail where there are spaces in filenames.

    The ultimate way to avoid this is to scrap bash and use zsh. Extended globbing is fantastic!

  10. 10

    Here’s a more complete curl snippet for downloading big files:

    http://textsnippets.com/posts/show/1092

    Pretty cool!

  11. 11

    Here’s a more complete curl snippet for downloading big files:
    http://textsnippets.com/posts/show/1092
    Pretty cool!

    Doesn’t curl already have retry functionality built in?

    curl -C - –retry 10 -O http://url.com

    When curl is about to retry a transfer, it will first wait one second and then for all forthcoming retries it will double the waiting time until it reaches 10 minutes which then will be the delay between the rest of the retries. - You can change this by setting --retry-delay.

  12. 12

    OK, I just found the -all i386 command.
    But how would one format trim-app to keep ONLY
    en/english/English.lproj files?
    One command for all 2-3?

  13. 13

    Keep getting a “Permission denied” message. Can’t do any “trim-app” commands. I’m not a poweruser and I’ve never used Terminal except to run AppleJack.

    Please help!

    Mahalo . . .

    brian

  14. 14

    But how would one format trim-app to keep ONLY
    en/english/English.lproj files?
    One command for all 2-3?

    Trimmit does this.

    Keep getting a “Permission denied” message.

    That means you don’t have appropriate permissions to execute the file. You need to run a chmod command and make the file executable. If you own the file, chmod 500 /usr/bin/trim-app will be safe, otherwise you might need to allow group to execute (chmod g+x /usr/bin/trim-app).

RSS feed for comments on this post. TrackBack URI

Leave a comment

Comments may be edited for formatting.