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?
15 Comments so far
Leave a commentThis 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
reported by FilCab on October 4, 2007 9:20 pm | Permalink
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.
reported by Jordan on October 4, 2007 10:50 pm | Permalink
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
voiced by Kenneth (SeoxyS) on October 4, 2007 11:57 pm | Permalink
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".I’ve never seen one of those before
Thanks, that’s fixed.
They are quite handy sometimes
posted by Ankur on October 5, 2007 11:07 am | Permalink
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.
voiced by Rick on October 8, 2007 7:47 pm | Permalink
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", ... )proclaimed by Rick on October 21, 2007 2:14 pm | Permalink
I use rsync -aE [source] [destination] to keep my stuff backed up.
revealed by Sean on October 30, 2007 3:39 pm | Permalink
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.
voiced by Hamish on October 31, 2007 9:41 am | Permalink
The ultimate way to avoid this is to scrap bash and use zsh. Extended globbing is fantastic!
mentioned by Ankur on October 31, 2007 12:25 pm | Permalink
Here’s a more complete curl snippet for downloading big files:
http://textsnippets.com/posts/show/1092
Pretty cool!
voiced by verno on October 31, 2007 10:29 pm | Permalink
Doesn’t curl already have retry functionality built in?
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.revealed by Ankur on November 1, 2007 5:51 pm | Permalink
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?
announced by Me2 on December 31, 2007 8:39 pm | Permalink
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
determined by brian ansorge on February 11, 2008 2:08 pm | Permalink
Trimmit does this.
That means you don’t have appropriate permissions to execute the file. You need to run a
chmodcommand and make the file executable. If you own the file,chmod 500 /usr/bin/trim-appwill be safe, otherwise you might need to allow group to execute (chmod g+x /usr/bin/trim-app).reported by Ankur on February 12, 2008 8:27 am | Permalink
Leave a comment