Access Your OS X Clipboard from the Command Line

There may be times when you need to access whatever is on your clipboard in a Terminal; maybe a particularly long URL for example.  And, there may be times when you want to put things on to the clipboard from a Terminal.

Reading the Clipboard

As it’s going to be text on the clipboard (even if it’s an image, it’ll just be the file name if you use this command), you can access it using the command pbpaste:

$ pbpaste
http://droptips.com/

So in this case, I have the text “http://droptips.com” on my clipboard, but what if I want to access that from a string of commands?  (I’m using curl as an example from another post)

$ curl -Is `pbpaste` | head -n1

By placing ` ` around the pbpaste command, it will run, and whatever it returns will be used instead.  So, the above command in this instance will actually run:

$ curl -Is http://droptips.com | head -n1

Of course, you may want to just run a few stats on the contents of your clipboards, and you can also do this using the wc command I posted about.  You can run pbpaste and pipe it through the wc command as follows:

pbpaste | wc -w

This will return the amount of words currently on the clipboard.

Writing to the Clipboard

But what if you want to get content onto the clipboard?  We use the pbcopy command.

So if I want to get the contents of a text file onto the clipboard, I could cat the file and pipe it through to pbcopy as follows:

cat myfile.txt | pbcopy

Anything which gets passed to pbcopy this way, will be on the clipboard.

Likewise, if I want to put a single line of text onto the clipboard, I could use the echo command:

echo "I want this on the clipboard" | pbcopy

As this is the normal clipboard, this content is available using the normal paste commands and of course pbpaste.

So if you ever find yourself needing to use the clipboard from a Terminal, then pbpaste and pbcopy are the commands to do it!

Quick command to check the status of a URL (Linux, BSD, OS X)

There may be times when you just want to do a quick check to see whether a web site is up, and/or what status message the web server is showing. This won’t actually show you if the page it served up is correct or not, purely the web server status code on whether or not it served something/anything/nothing!

There are lots of status codes for web servers, and they are detailed here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

So, to be able to see the status message, and to check if a host is up or not, we can use the curl command.  curl is installed by default on OS X, but you may need to install it on your Linux distribution or BSD installation.

So, to do a quick check from a command line:

curl -Is http://droptips.com | head -n 1

And you’ll get something back similar to:

HTTP/1.1 200 OK

Status code 200 is “OK”.

HTTP/1.1 200 OK

As another example, to show you how this will display different status codes, you’ll notice that if you visit: http://www.droptips.com you are redirected to http://droptips.com, purely because I prefer not to have www in the URL.  I do this by implementing a 301 (Permanently moved) from within Apache for any client going to www.droptips.com:

$ curl -Is http://www.droptips.com | head -n 1
HTTP/1.1 301 Moved Permanently

As you can see, the web server returned a 301, and this is shown.

curl can also take full URLs, so you can check whether or not a particular page/path is being served or not:

curl -Is http://droptips.com/tag/tips/ | head -n1
HTTP/1.1 200 OK

So if you want to see if that new redirect is working, or just to see if your web server is up and serving requests, give curl a go!  This is just a basic overview of one thing you can do with curl – it’s a very flexible command, and can be used in a number of scenarios, some of which I will post about in the future.

Make your Mac Talk/Speak by using say (OS X)

This is a quick little tip, to turn text into speech on OS X.

Open a Terminal and use the command say as follows:

say droptips rocks

Remember to have your speakers turned up and you’ll hear whatever you’ve typed after say.

Interestingly, if you type:

say x

It’ll Say “exx” or similar.

But if you do:

say os x

.. it’s clever enough to say “Oh Ess Ten” which is pretty impressive.

What use is there for this?  Well.. you could use say in scripts to read out certain parts and to create alerts, I guess.  You could also SSH to a remote Mac, and get it saying things like “Bring me a coffee”, but your mileage may vary on whether this works or not (getting the coffee from whoever was in the room at the time, that is!)

There are a few options, which are detailed within the manpage here: http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/say.1.html

Command Help using Manual Pages (man) (OS X, Linux, BSD)

As you start using the command line, you’ll come across thousands of different commands, all with their own options and ways of taking options.  Every command is different, but help is on hand in the form of man pages.

Manual Pages (or man page for short) are the documentation distributed with every command (or, at least, should be with every command) which tells you how a particular command works.

From a Terminal in OS X, Linux, or BSD, you should have access to a command called man.  man is the reader for these man pages, and parses them nicely onto the screen for you.

So, for example, if you wanted help with the mount command, you would simply do:

man mount

… and you would be presented with the manual page for mount.

This is true of any command, and there should always be a manpage with the command. It’s good practice to do this as a developer, but you may find cases where people don’t.

There are also various web sites which have html versions of these manpages, and you can normally find these on the Linux or BSD distribution’s site you are using, or the Apple site.  For the sake of this post, the three below are for Ubuntu, FreeBSD, and OS X.

Ubuntu – http://manpages.ubuntu.com/

FreeBSD – http://www.freebsd.org/cgi/man.cgi

OS X – http://developer.apple.com/documentation/Darwin/Reference/ManPages/

So the next time you need help with a particular command, do a quick man thecommand and hopefully, you’ll have all the help you need!

Changing the Default Format Screenshots are Saved As (OS X)

When you take a screenshot in OS X, using the default button combination, you’ll notice that it’s saved in PNG (Portable Network Graphics) format.

You may prefer another format, such as JPEG, but it’s not obvious on how to change it.

From within a Terminal, you can change it pretty easily:

defaults write com.apple.screencapture type jpg

This would change the default format to JPEG, but you would need to log out and log back in for the change to take effect.

There are other formats too – Tiff, PDF, and the default PNG.  You can change to any of them by changing “jpg” on the command above to whichever you require, by using one of the following:

defaults write com.apple.screencapture type tiff
defaults write com.apple.screencapture type pdf
defaults write com.apple.screencapture type png

Remember after changing the format, you will need to log out and log back in to OS X, for the changes to take effect.

Changing the Default Location for Screenshots in OS X

When you take a screenshot in OS X, using the default keyboard shortcuts, it places the image file on the Desktop.  This might be OK in some instances, but what if you want to place it in a different directory, maybe one just for screenshots?

You can change the path using a Terminal.

Open Terminal and then type:

defaults write com.apple.screencapture location /Path/Goes/Here

So, if you want to send all Screenshots to a directory within Pictures, for a user called “daz”, the command would be:

defaults write com.apple.screencapture location /Users/daz/Pictures/Screenshots

You will need to log out and log back in to OS X for this change to take effect, but the next time you take a screenshot, it will place it in the directory of your choosing.

Enable and Disable the OS X Dashboard Completely

If like me you never use the Dashboard within OS X, you’ll be aware that it’s silentely in the background using up some resources.  You may have removed all of the widgets from the Dashboard, but it’s still there in the background, just not displaying anything.

This process definitely works in OS X Leopard (10.5), and it may work in other versions, too – please let me know via comments below!

You can disable it completely though, with a single command in a Terminal.

To Disable the Dashboard:

defaults write com.apple.dashboard mcx-disabled -boolean yes

You will need to log off and log back in to see the changes, but the Dashboard should no longer be available.

To Enable the Dashboard:

If you then decide you’ve made a mistake by disabling it, and you want it back, just swap the -boolean to “no” as follows:

defaults write com.apple.dashboard mcx-disabled -boolean no

Again, you will need to log off and then back on to see the changes.

Wuff —- Wuff!! from Mac OS X to a Screen Session on Linux

If you use OS X, and manage multiple machines over SSH, you’ve probably come across and used screen.

You may have noticed that if you SSH to a Linux host from Mac OS X, and try to use the backspace or tab key within a screen session, that you get the “Wuff —- Wuff!!” prompt at the bottom.

Thankfully, the resolution to this problem is easy.

On the remote machine, if you open your ~/.bash_profile file in a text editor (.bash_profile in your home directory), you need to add the following line:

alias screen='TERM=screen screen'

Add that, save the file and you’re done!  You’ll need to re-run bash or just log out and log back in, but when you do, the next screen session you open, you’ll find that the backspace/tab key works again as expected!

Mounting a Windows (SMB) Share from the OS X Command Line

There may be times when you would prefer to mount a Windows (smb) share from a Terminal in OS X; I certainly prefer to use the command line, and use it as much as possible – I just find it quicker than using Finder.

Mounting a share is pretty simple, using the mount command:

mount -t smbfs //servername/sharename mount/path

So, for example, if you have a server/PC called “WindowsPC” with a sharename on there of “Shared”, and you wanted to mount it at path /Users/daz/mnt, the command would be:

mount -t smbfs //WindowsPC/Shared /Users/daz/mnt

You may be prompted to supply a password for the share, but then it will mount.

Also note that if you don’t have DNS/Name resolution in place, servername can also be an IP address.

This will take your local OS X username, and use it to authenticate.  If you want to use a different username to authenticate as, you would put it before the machine name or IP like so:

mount -t smbfs //username@servername/sharename /mount/path

There are a few other options you may require, for some setups.  You can read the Apple manpage entry for mount_smbfs at the link below:
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/mount_smbfs.8.html

Of course, you aren’t just limited to mounting shares via smb on Windows machines – OS X also offers Windows File Sharing, which uses the same protocol, as does Samba in Linux/BSD. You can use the same commands here to mount shares on other systems utilising this protocol.

After you get used to using the command line, you’ll probably find it much quicker/better at certain tasks. I’ll be supplying lots of tips here on droptips, to help you get to grips with using a Terminal more.

How to Turn off IE Enhanced Security in Windows Server 2008

Internet Explorer Enhanced Security Configuration always gets turned off on servers I install – I just don’t get on with it.

You may now be installing Windows 2008 Server servers, and there’s a slightly different process to disable this feature than in Windows 2003 Server.

The configuration is now part of Server Manager, not Add/Remove (Programs and Features) as it was in 2003.

So, to turn it off in Windows Server 2008, if you open Server Manager (Start -> Server Manager by default), and then under “Server Summary” will be a box called “Security Information“.  If you look in there, on the right hand side will be an option “Configure IE ESC” – this will open a box which lets you turn the feature off and on for Admin, Users, or both.