How to Rename a Disk Partition in OSX

There are multiple ways to rename a disk partition in OSX, and both should work well depending on your situation.

Use Finder to rename disk partition

You can simply use the Finder to change the disk partition name. In Finder click on the name of your computer, which should be at the top of your file tree, find your partition there, and then just right click on it and click Get info. In the dialog that opens you can easily enter a new name.

Rename MacOS disk partition using command line

In OSX you can also use a command line diskutil tool to rename your partition. Open up the Terminal app, and enter the following command, replacing “old” with your old name, and “new” with your new desired name.

$ /usr/sbin/diskutil rename old new

For example you could do this to change a partition named “Macintosh HD” to My Mac:

$ /usr/sbin/diskutil rename "Machintosh HD" "My Mac"

As you can see you can use quotes in the name.

Note that depending on your set up, that is if your user home directory is not in the root partition, you may have to update your home directory path in System Preferences.

See Also




How To Create an Alias in Unix shell

When you want to save yourself from typing an unwieldy command over and over again you can create and use an alias for it. It will then act as a shortcut to the larger command, which you can type and run instead.

Creating aliases in UNIX (and Linux) is done with a simple alias command which follows this format: alias name=’command you want to run’.

Replace the “name” with your shortcut command, and “command you want to run” with the larger command you want to create an alias of. Here’s a simple example:

alias accesslog='tail -f /var/log/lighttpd/access.log'  

In this example I’ve effectively created a new accesslog command which is an alias of the tail -f /var/log/lighttpd/access.log command. What it does is follow the access.log file and display new entries in it as they happen. Now instead of having to write the whole tail -f command every time I want to look at what’s happening in the access.log file I can simply run the accesslog alias command instead, which is pretty nifty.

What if I want to unset the alias once I no longer need it or wish to set a new better alias? Well, simply run:

unalias accesslog  

Quite logical. Now the accesslog alias no longer exists.

One thing to keep in mind though is that aliases that are set this way get lost the moment you close the command line session, or in other words, they are temporary. If you want to save aliases permanently you will have to edit the bash configuration file, which is usually .bashrc or .bash_profile residing in your user home directory. You can edit whichever you prefer, or whichever exists on your system.

To edit .bashrc just open it in a command line text editor such as nano, or any other you might prefer, and add the same exact alias command as in the above example at the bottom of it, or find where other aliases are already set and add yours after them.

nano .bashrc  

Once you add your aliases save the file, which in the nano editor is done by pressing the Сtrl-x keyboard shortcut, answering “y” when asked to save, and hitting enter.

Now your alias is saved permanently, and it will therefore work even after you close the session and come back. Of course, to remove the permanent alias just edit the file again and remove the line you’ve just added. If it’s still set run the unalias command as shown above and it will be gone.

Note that aliases are set for the currently active user. So you have to edit the .bashrc file in the home directory of that user. If you’re logged in as root that would be /root/.bashrc, and if you’re logged in as joe, for example, it will be in /home/joe/.bashrc. If you try to run root’s alias while acting as joe or vice versa you’ll get a “command not found” error.

Also note that aliases added to .bashrc aren’t active immediately after you save the file since that file is read on user’s login. If you log out and log back in then it will work.

Finally, once you have a bunch of aliases set up you might want to check up on which aliases are available. To do that just run the alias command by itself:

alias  

And it will list something like this:

alias accesslog='tail -f /var/log/lighttpd/access.log' 
alias ls='ls --color=auto'  

The list represents all of the aliases that have been set in .bashrc, or on the command line during the current session. In the above example we see my accesslog alias, and another one for the ls command associating it with the ls –color=auto command, which simply adds some coloring to our ls lists.

That brings us to the final point worth a mention, as demonstrated by the above ls alias, and that is that you can alias an already existing real command. For example if we have a nmon command installed, which shows various system activity information, we can actually turn it into an alias for the top command, which also shows system activity.

You probably don’t want to do this, or at least, you don’t want to keep this alias, but for the sake of demonstration:

alias nmon='top'  

And now when you run nmon, instead of opening the actual nmon program it will open top. In other words the alias is masking the original command.

This serves as a word of caution when it comes to setting names of aliases; try to avoid setting names that match existing commands. Chances are you’ll want those commands doing what they’re supposed to do, except in special cases like the above ls alias, which simply aliases to its own coloring options.

And that’s how aliases work in UNIX (and Linux).




How To Confirm Mac OS Version from Command Line

Just a very quick tip today, I stumbled upon this command a while ago and think it may be handy for someone learning the OSX command line.

By using sw_vers command, you can easily confirm the exact version of your Mac OS and the product code (build version) of it:

macbook:~ root# sw_vers
ProductName: Mac OS X
ProductVersion: 10.8.2
BuildVersion: 12C2034



How To Mount DMG Files from Command Line in Mac OS

DMG files are proprietary disk image files used for software distribution in Mac OS. Providiing both password protection and bzip2-like compression, these files are perfect packaging medium.

Usually DMG files are opened automatically when you click them in Finder. They appear as a folder with files, but actually Finder mounts each DMG file as a separate filesystem and then shows you its contents. If you’re observant enough, you’ll see that in the left side panell of Finder you have all the active DMG filesystems listed and ready to be ejected once you finish copying the files or installing new software.

Sometimes you may want to download and mount DMG file using Mac OS command line, and in this post I’ll show you how to do it

Why would you want to mount DMG files manually?

I’ve been business traveling quite a bit lately which means I’m most of the time away from my home computer. Naturally, I have configured Remote Desktop access so that I can use my iPad to access my desktop whenever I need, but sometimes it takes forever to do some simple things just because of the graphics environment overhead.

If you’re like me, you’ll probably find Remote Desktop over 3G to be pretty boring, and will want to do as much as you can via command line.

Mounting DMG with hdiutil command

In order to manually mount DMG file, you’ll need to use hdiutil command. You don’t have to be a privileged user, so can do it as your own user.

For this example, I’m going to use the command line interface (CLI) for the excellent HandBrake tool, which is great for converting all sorts of videos into iPad and iPhone friendly resolution and mp4 format

Let’s mount the image from my dmg file:

MacPro:~greys$ hdiutil attach /Users/greys/HandBrake-0.9.8-MacOSX.6_CLI_x86_64.dmg/dev/disk4 Apple_partition_scheme/dev/disk4s1 Apple_partition_map/dev/disk4s2 Apple_HFS /Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86_64

As you can see from this output, the mount was successful and you now have the filesystem from DMG package available under the /Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86__64 directory.

Don’t want to to take my word for it? Let’s use the standard mount command to confirm that indeed we now have an new filesystem mounted:

MacPro:~ root# mount | grep HandBrake/dev/disk4s2 on /Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86_64 (hfs, local, nodev, nosuid, read-only, noowners)MacPro:~ root# cd /Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86_64/MacPro:HandBrake-0.9.8-MacOSX.6_CLI_x86_64 root# ls.Trashes HandBrakeCLI doc

Ejecting mounted DMG images from command line

Once you are done with whatever you were trying to do, there’s no longer a reason to keep your DMG image mounted, so you should unmount it. While it’s possible to use umount command, I think it makes more sense if you use the same hdiutil tool that helped you mouunt the DMG image in the first place.

Here’s how you can eject the DMG image using hdiutil:

MacPro:~ greys$ hdiutil eject /Volumes/HandBrake-0.9.8-MacOSX.6_CLI_x86_64/"disk4" unmounted."disk4" ejected.

 

That’s it for today, hope you liked the post! Let me know!