You might already know about this one, but if not,
which allows it to recurse into any child folders and remove them as well if they are empty (it will throw an error if not empty). Handy if you have a folder structure a few levels deep that is empty and want to quickly remove all the folders in one hit. By the way if you didn’t know about
it will let you create directories recursively as many levels deep as you need e.g.
mkdir -p level1\level2\level3
You can specify a specific file to extract from a zip archive by specifying the filename after the zip file e.g.:
unzip myarchive.zip targetfilename.txt
You can use the ls command to get a list of all the files in the current directory as a comma separated list using the following:
If you want to exclude any subdirectories, try this:
ls -mp | grep -v "/"
Note you may end up with a trailing comma depending on whether the last item is a directory or not.
A nifty command to access the last result is $(!!).
For example you can do something like this:
$ find . -type f -name example.txt*
$ vim $(!!)
As I only had one result from the find, I can that result and call vim on that file.
Just something handy to put in your script if you need to check for root access:
# Do we have root access?
if [[ $(/usr/bin/id -u) -ne 0 ]];
then echo "Sorry, this script needs to run with root or sudo access"
If you get this when you try
bunzip2 filename.ext.bz2, then try bzcat using
bzcat filename.ext.bz2 > filename.ext.
One thing that’s annoying with sudo is that if you don’t own or have relevant permissions on the file you are trying to redirect to, you will get an error.
$ sudo cat /dev/null > filenotownedbyyou.log
This will fail, because even though you are using sudo, it doesn’t apply to the redirect portion of this command. So you will get “Permission denied”.
The solution is to call the entire command via bash and sudo that:
$sudo bash -c 'cat /dev/null > filenotownedbyyou.log'
This little trick can also be applied to number of situations like this.
You can use the command:
To get the current user’s username in Linux. Handy if you are shell scripting and need to know who is logged in dynamically as part of your script.
If you need to install any range of open source software on your mac (particularly command line tools and servers), then MacPorts is a great way to go. It’s a package manager (much like yum or rpm for Linux) and there are over 15,000 ports available.
As you may have noticed, I’m a big fan of the command line. In fact the main reason I like Mac OSX over Windows is because it is BSD (Unix) based and has a proper terminal.
One cool thing you can do is get the calendar for the current month from the command line like so:
Su Mo Tu We Th Fr Sa
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
You can also grab the calendar for the whole year like this:
$ cal 2012