SplitPDF.py input.pdf 3 5 Assuming input.pdf has 10 pages, you will get three files back: input.part1.13.pdf contains page 1-3, input.part2.45.pdf contains page 4-5, and input.part3.610.pdf contains page 6-10 (note how the page ranges are part of the output filenames). I should mention that each splitPageNumi should be an integer between 1 and the number of pages of input.pdf (inclusive), and the entire sequence must be strictly increasing. Lastly this script should work on both Panther and Tiger (Mac OS X 10.3.x/10.4.x). Just for completeness sake, if you have installed (possibly via ), this is how you can extract pages from a pdf file (all in one line). Gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dFirstPage=3 -dLastPage=5 -sOUTPUTFILE=input.35.pdf input.pdf This will extract page 3-5 (inclusive) from input.pdf into a new file input.35.pdf. (10.4.x+ only) How do I join multiple PDFs into one from command-line? (Thanks to for pointing this script to me) For some of us, sometimes we need to join/combine/concatenate multiple PDF files into one PDF file for some reason.
There have been multiple ways to achieve this without buying extra piece of software. If you're a Tiger (OS X 10.4.x) user, things are even a bit easier - it turns out a script has already been written for us by those kind Apple engineers - this script is located at./joinPDF.py -o final.pdf input1.pdf input2.pdf final.pdf then is a concatenation of input1.pdf and input2.pdf, in that order. There is another option available, if you look at the source code (note the option -preview and -append are not really implemented, but I guess the latter is just equivalent to the script default): that's -shuffle. I'll just quote the explanation from the code: Take a page from each PDF input file in turn before taking another from each file.
If this option is not specified then all of the pages from a PDF file are appended to the output PDF file before the next input PDF file is processed. Just for completeness sake, if you have installed (possibly via ), this is how you can join multiple PDF files (all in one line). #!/usr/bin/env python import urllib, re, sys, os # if this changes we need to revise the code to get the external IP iptellingurl = 'if len(sys.argv) 1: # get the external IP mo = re.search(r' d+. D+', urllib.urlopen(iptellingurl).read) if mo: print mo.group else: print 'Cannot get the external IP!' Else: # get the internal IP of an interface targetInt = sys.argv1 output = os.popen('ipconfig getifaddr%s 2&1'% targetInt).read.strip if re.match(r' d+.
D+', output): print output else: print 'Cannot get the internal IP for interface '%s '% targetInt As usual save this script to a file say getip.py and do a ' chmod a+x getip.py' to make it executable. To get the external IP, do this./chgext doc txt To keep the tip short I'll only mention one more thing: you can use wildcard '.' in pattern - that will match all possible strings. But pattern is not a regular expression - '.' Won't be interpreted as it would in a regex (so it'll only match a dot character). How do I backup my stuff to an external drive using rsync? Not everyone knows that in Mac OS X you don't need to buy expensive software to do incremental backup.
Using the UNIX command rsync, you can perform intelligent incremental backup, meaning you only update your backup files with their latest versions - no wasteful copying of the same files. Rsync is a very flexible and powerful tool - you can even do backup with a remote server. But I'll only show how you can backup your files to an external drive (or to a different directory) - do a ' man rsync' to learn the other goodies it offers.
Here is the little script for this purpose. #!/bin/sh SOURCEDIRS='Documents:Music:Pictures:Library/Mail:Downloaded stuff' TARGETDIR='/Volumes/External Drive' # if the external drive is not there, complain and stop if ! -e '$TARGETDIR' then echo Target directory does not exist! Exit fi IFS=: pushd.
Unix Read Example
Cd / /usr/bin/rsync -E -delete -progress -av $SOURCEDIRS '$TARGETDIR' popd The SOURCEDIRS is a list of folders you want to backup - they are specified relative to your home folder, and are separated using colon (`:') - so in the script the directory ' /Downloaded stuff' (note that space in a directory's name is okay) will be backed up. The TARGETDIR is the place where you want to store the backup files: in this case an external drive with name ' External Drive' is used (again note that space in the path is okay) - the backup files will be deposited directly under the root directory of that drive. Feel free to customize both variables to suit your needs. (thanks to Paul Henrich for pointing me to the space-related problems) Note a crucial option is added to the rsync line (thanks to Patrick Cunningham and Brian Ashe): the ' -E' switch is a special addition to the Mac's built-in rsync, which copies that are used in the HFS/HFS+ filesystem. To make sure the right version of rsync is used, I hard coded the path of rsync in the script.
To run it, save the file into say ' backup', and make it executable ( chmod a+x./backup). After running it you should expect to have an exact replica of the specified folders on your external drive. How do I find the pid of a process by its name?
In UNIX many chores involving processes require that you know the pid (process ID) of the targeting processes before you can do anything with them. Here is a simple script you can use to find out the pid of a process by its name. /System/Library/Frameworks/ApplicationServices.framework/ Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user Running lsregister with no argument will tell you what those options are for. Reloading Cisco VPN kernel extension From time to time my Cisco VPN client just gives me crap like 'cannot load kernel extension' or 'cannot find a valid IP address' etc although my connection is perfectly fine. In this case try the following in Terminal.app.
Sudo kextunload /System/Library/Extensions/CiscoVPN.kext sudo kextload /System/Library/Extensions/CiscoVPN.kext Setting environment variables for GUI apps As UNIX users we all know that the way to set up PATH variable (or other environment variables) is to do that either in our.bashprofile (if the default shell is bash) or.tcsh file (if the default shell is tcsh). Unfortunately the graphical apps do not get their paths from those settings. To do that you need to create a file /.MacOSX/environment.plist and add your settings there. At Apple will tell you the details.
Create a disk image file for a folder, using hdiutil Do this. Ps -p -o pid tail -n 1 grep -v PID If the process is still alive, you'll get the process ID back.
Otherwise nothing will be printed. This is useful in building a larger script where monitoring a process is necessary.
Send an email to a bunch of people from command-line This tip is actually a simple script written in - a powerful scripting language shipped with Mac OS X.3 Panther. The script allows you to send an email to multiple people on the command-line. This would be useful, for example, in sending a periodic reminder to a bunch of people with the help of the system scheduler, cron.
Note this tip is useful not only for Panther users, but also for the users of the other platforms that Python supports (e.g., Linux) as well. First, you need to create the following script: copy and paste the content below into a file, say, smtp.py (` py' is the default extension for Python scripts).
Make sure you change the line ' smtpHost=.' To point to an SMTP server you are allowed to use. Now make it executable by doing ' chmod a+x smtp.py'./smtp.py firstname.lastname@example.org bbPlayers.txt 'Don't forget to play BB!' BbMsg.txt A note about the sender argument: it has to be an email address from a valid domain (so the line above won't work - it's deliberately garbled), but other than that, there's no safety net to prevent you from impersonating others - BUT DON'T. That's what spammers do; besides, in the email header it'll clearly mark the originating IP address of the message, so if someone WANTS to track you down, she/he WILL. Clean up.DSStore files Ever want to remove all those hidden.DSStore files under some directory? You can do this.
Defaults write com.apple.terminal FocusFollowsMouse -string YES The next time you start Terminal.app, when the mouse is over any Terminal.app window, that window will receive the input focus (type away and you'll know). Do the above with YES replaced by NO to turn it off. By the way, the defaults command actually writes to an app's preference file (.plist); in this case, the file modified is /Library/Preferences/com.apple.Terminal.plist. How do I avoid automatic launching of Xterm whenever I start Apple's X11? If you look at /etc/X11/xinit/xinitrc, you'll find that by default xterm is launched whenever you start Apple's X11. This could be quite annoying if you don't want that.
Fortunately the fix is very simple. Just create your own.xinitrc file under your home directory, like this. #!/bin/sh exec quartz-wm How do I add an alias to an IP address? (updated 20031123: as of Panther (OS X.3), BSD flat files such as /etc/hosts are enabled again. So you no longer need to use Netinfo.app for this now) This is another Netinfo-related tip. Sometimes we want to type a shortened name of a machine to do various business with it; for example, instead of typing ` ssh 126.96.36.199' we want to type ` ssh mymachine'. For Linux/UNIX guys we know how to do this - just open up /etc/hosts and add an entry to it.
In Mac OS X you need to fire up Netinfo.app (in /Applications/Utilities) instead: navigate yourself to /machines, and add a new entry per machine. For each entry you also need to provide ipaddress and name properties. (Of course for the ssh example given above, you could just modify your /.ssh/config file - but that's another story) How do I change my default shell? If you come from Linux/UNIX world like me, we all know where to change the setting of a user's default shell ( /etc/passwd). But OS X does things a little differently.
Read Pdf Learning Unix For Mac Os X
To do that you need to fire up Netinfo.app (in /Applications/Utilities). Navigate yourself to /users/ in Netinfo's window, and find a property named 'shell'. The rest should be obvious. (Thanks to Robin Breathe) It turns out you can also achieve this by simply using the command-line utility chsh. Say you want to change your default shell from /bin/bash (the default for OS X) to /bin/tcsh, just type this into the terminal. Chsh -s /bin/tcsh Other nifty things are possible using chsh - again, ' man chsh' for more. How do I suspend/resume a process (even the GUI ones)?
This works both to a command-line or a GUI process. The latter case is particularly needed, for example, when your video transcoding process takes up almost 100% of CPU power and you want to do something that requires at least some attention from the CPU. Here you go: the first thing you need to do is to figure out the pid of the process you want to suspend: see.
After determining the pid (say it's 2209), I use the following command to suspend that process. Killall -SIGSTOP iTunes Although from my own experience this sometimes is less robust than the methods given above. To its credit you can even use regular expressions with option ' -m' to specify the processes you want to send the signal to (but be careful - you don't want to end up with a completely frozen system!). Building/installing the latest Emacs for OS X (native) from CVS (this assumes you already, and you'll do this entirely under root) Are you an Emacs user? If yes, don't you want to use a native OS X version of Emacs instead of being trapped inside the terminal? And, how about using the latest version of Emacs, directly shipped to you from the CVS? (Thanks to the who have devoted in porting Emacs to OS X) Here is a screenshot of Emacs OS X to motivate you.
So here is how you check out a copy of Emacs source code (you might want to think about where you want to put this - it's about 90MB in size). #!/bin/sh cp -Rp emacs emacs.build cd emacs.build CFLAGS='-O3 -faltivec' CXXFLAGS='-O3 -faltivec'./configure -enable-carbon-app=/Applications/Development -without-x make bootstrap Note the line starting with ` CFLAGS' is too long so I have to break it into several segments (connected with ' '; if you want to type them on one line, just remove the ' ' - this applies to the rest of this part), just note that the last line should be ` make bootstrap'. Also, if you want to change the target folder you want to install your Emacs into, change the ` -enable-carbon-app=' setting to the correct folder (here I chose to install it in /Applications/Development). Lastly, note that the building process starts by copying the source tree to another directory called emacs.build - this will prevent polluting the source tree.
Ok now make emacsbuild executable by issuing chmod a+x./emacsbuild in the terminal. Then execute it. After about 30-40 minutes (depending on the speed of your machine), the building process should finish. You can then do cd emacs.build; make install to install the whole thing. But better yet, create a shell script emacsinstall with the following content. #!/bin/sh rm -rf /Applications/Development/Emacs.app/ /usr/local/bin/emacs. /usr/local/share/emacs/21.3.50/ cd emacs.build make install cd.
Rm -rf emacs.build Again the line 'rm -rf' is broken down into two segments using ' '. Make this executable, and execute it. This script will remove the old stuff first, install the new build, and wipe the build directory clean. But what if, from time to time, you want to update the Emacs source tree with the CVS, so you'll have the latest bugfixes? Create a shell script called emacsupdate like the following.
#!/bin/sh rm -f emacs/lisp/loaddefs.el. cd emacs cvs -z3 -d:pserver:email@example.com:/sources/emacs update cd. Again make it executable and then run it to update your emacs source tree. Of course this assumes you still keep the source tree around (in emacs directory) - the CVS update will only download the necessary files. How do I enable the root account?
Unix Basics Pdf
By default the root account is disabled in Mac OS X - you have to do everything that requires root privilege using sudo instead, and this could become quite annoying after a while. To enable the root account, fire up the Netinfo.app (in /Applications/Utilities), and click on the Security menu - you'll see an item Enable Root Account. Philip Bruce sent in an alternative: just do a ' sudo su' and you'll be dropped in a shell as root. Firewall and iTunes sharing - what ports to open? If you have a firewall running on Mac, either configured via the built-in Sharing Preferences panel, or from a third-party tool such as, make sure you open these ports to let iTunes traffic through:. Multicast-DNS (mDNS): this is UDP port 5533. This port is necessary to be able to see all the shared playlists (and to be able to automagically discover all the Rendezvous-enabled services).
DAAP: this is TCP port 3689. This port is for the actual iTunes data traffic. Enabling only one of the above and not the other, you might only be able to see the shared playlists but not able to play them, or vice versa. If you're using BrickHouse, which I highly recommend over Apple's built-in firewall configuration interface, what you might end up with is something like this: And these two rules are at the beginning of my firewall rules. You might also notice I only allow connections to/from 188.8.131.52 (CMU). Who are listening to my tunes? Ever wondering who those ' n users connected' are when you look at the Sharing part of the Preferences of your iTunes?
Well wonder no more - it turns out fairly easy to figure out in Terminal.app; just type this line. Lsof -r 2 -n -P -F n -c iTunes -a -i TCP@`hostname`:3689 and it will tell you the IP address of each connection, together with the music file the connection is listening to. Lsof is a UNIX tool which is capable of listing the files a particular process opens - including even the files being accessed from remote connections, such as NFS, and iTunes in our case. Some short explanations of the parameters used:.r 2: list the opened files repeatedly, and refresh the list every 2 seconds.n: show numeric IP address instead of domain names.P: show port info in numbers instead of names (e.g., 3689 vs.F n: display field `name' (n).c iTunes: only list files opened by process with name 'iTunes'.a: logical `and' to connect more than one conditions.i TCP@`hostname`:3689: only list files involving the specified address; in this case the protocol must be TCP, connecting to my machine, and connecting to port 3689.
Of course based on the result you can do all kinds of fancy things, like accumulating statistics about which tunes get listened to most often, and so on. I'm too lazy to write an app like that - let me know if you write one though!