Useful Mac and *nix commands

I forget regularly useful commands and that’s why I list some of them here. The page is neither intended to be a complete reference nor a list of mac-only commands. Some are, others aren’t. I update this post now and then and as soon as I review the post I’ll mark everything what’s mac- or linux-only.

Mac

Change the computer name


# temporary change
sudo hostname new_computer_name

# permanent change
sudo scutil –set HostName new_computer_name

Show hidden files in Finder

By default Finder doesn’t show hidden files and there’s no way to change it in the preferences. You have to execute this in your shell:


defaults write com.apple.finder AppleShowAllFiles TRUE # show hidden files
killall Finder                                         # restart Finder

(Un) Installing program packages (.mpkg | .pkg)

Sometimes I want to know first what files are going to be installed. With pax I can take a look at the file list. To be sure to have completely uninstalled an application you can check it by using lsbom.


pax -z -f   # List files that will be installed in the system
lsbom           # List all files that should be removed to uninstall the package

Re-indexing folders or volumes

If you need to re-index folders or volumes you can do this with mdutil. Sometimes the index is damaged and therefore you have to delete .Spotlight-V100 first. You need the required root permission so sudo is prefixed.


sudo rm -r /Volumes//.Spotlight-V100
sudo mdutil -E /Volumes/

Get CPU information of your machine


sysctl -a | grep -i "dep.cpu"

I used this command to find out if my cpu supports vtx. And it does :)

The /proc file system on the mac

I alway forget that the macs don’t have a proc file system. Instead mac users can use the sysctl command to get some system and kernel information.


# list everything
sysctl -a

# list hardware information
sysctl -a hw

# print the cpu 64-bit capability
sysctl -a hw.cpu64bit_capable

Changing the MAC address


sudo ifconfig en0 lladdr aa:bb:cc:11:22:33

Linux

Copy data encrypted and exclude foldes


rsync -av --exclude dir2 --exclude dir2 -n user@server:/var/www/path/to/somewhere .

This transfers recursively all files from the directory /var/www/path/to/somewhere on the machine server into the local directory on the local machine. The files are transferred in “archive” mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer.

With --exclude directories which should not be copied can be skipped and -n tells rsync to run in the dry mode which simulates the operations which would take place.

Rescaling images

convert converts between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more.


convert -filter Cubic -resize 200 old.jpg new.jpg

In this example old.jpg is resized to 200px and saved in new.jpg.

Cubic is the filter used for rescaling. You don’t know what I mean with this filter? No problem, I don’t do either. The manpage just says “use this filter when resizing an image” which is a brilliant statement for somebody looking for some more details. Pfff.

Changing the keyboard layout at runtime


setxkbmap de # de for the German keyboard layout

All

Git commands


git ls-files   # list the files which are under version control

MySQL

I had trouble with phpMyAdmin loading a dump file. I found out that it’s a common problem that it get stuck on a Mac machine. So I had to do it in the shell by using the following line. Further details about the command can be found on the man page.


mysql --user=user_name --password=your_password db_name < dump_file.sql

Sending a mail in the shell


# mail the text that was piped to mail
echo "mail text" | mail johndoe@example.com -s "subject text"
# mail the file content
cat list.txt | mail johndoe@example.com -s "jar with dependencies"

List the content of zip/jar archives


unzip -l archive.zip
unzip -l archive.jar

Pack and unpack JAR files

I came across a .pack.gz file and didn't know what the hell this was and how to unpack it. At the coderanch I saw that I had to use pack200 and unpack200 to handle such a file.


# pack jar file
pack200 archive.pack.gz archive.jar
# unpack jar file
unpack200 archive.pack.gz archive.jar

Delete empty files


find /path/to/dir -type f -size 0 -print|xargs rm

Note: also the subdirectories of /path/to/dir are scanned.

Printing

For multiple page printing I use mpage and psbind (a smarter psnup).


mpage -2f -Pokiprinter textfile
mpage -2f -Pokiprinter textfile.ps

okiprinter is the name of my Oki printer ;-)

To print a header and page numbers genscript is useful.

Generating a GUID

A GUID (Global Unique ID), also known as UUID (Universally Unique ID), can be generated by uuidgen which should be installed by default by your Linux or Mac OS.


uuidgen

A unique ID is created using your MAC address amongst others. It looks like 8AF8EEEC-3472-48B0-A74B-B2B3820EA6FE.

Piping an echo to a file

Instead of opening vim to edit your shell configuration file you can pipe your configuration directly to it by using echo -e.


# Mac
echo -e " \nexport PATH=/usr/local/CrossPack-AVR/bin:$PATH" >> ~/.profile
#Linux
echo -e " \nexport PATH=/usr/local/whatever/bin:$PATH" >> ~/.bashrc

\n goes to a new line. There's no need to put a space between \n and the following string.

Creating a patch

If you are a developer you might come across bugs that you fixed yourself. To share the fix with others you can use the command diff which scans two files and lists the differences. If you output this into a file e.g. differences.patch the users can use your file as the input for the command patch.

Creating the patch file


# Run diff to get all the differences of two entities. These can be files but also folders.
# The result is piped into 
#   c => output NUM (default 3) lines of copied context
#   r => recursive
#   B => ignore changes whose lines are all blank
diff -crB Folder1 Folder2 > bugfix.patch

Copy the patch file into the directory of the file or folder you want to patch. If the patch was created of two folders change into the directory that should be patched and run the patch from there. Thus the patch command knows what folder should be patched.

Running the patch


# patching files  
patch –dry-run -p1 -i bugfix.patch # Patch simulation
patch -p1 -i bugfix.patch          # Patch in action

# patching folders
cd folder_to_be_patched
patch –dry-run -p1 -i ../bugfix.patch # Patch simulation
patch -p1 -i ../bugfix.patch          # Patch in action

Extract archives


# extract a tar.bz2 file, note the j option
tar -jxvf filename.tar.bz2

# extract a tar.gz file, note the z option
tar -zxvf filename.tar.gz

An alternative is to use dtrx.

New line in sed

Arg, sed is so awful. I wrote a java 6 installer (see this article) script for 32-bit macs and needed to modify the .profile. It was a fight to get the right regular expression that sed understood.

The first thing I figured out was that neither \s* (as I would use in javascript and php) nor \s\{3,\} worked for me.

The second thing is that \n is not recognized as a new line. To get a new line \'$'\n must be used.


  #sed -i .bak  's/^\(export[ ]*JAVA_HOME.*\)/#&\'$'\nexport\ JAVA_HOME=\/Users\/${USER}\/\.java_jdk_6\/soylatte16-i386-1\.0\.3/' ~/.profile
  #sed -i .bak  's/^\(export[ ]*JAVA_HOME.*\)/&\'$'\nexport\ PATH=${JAVA_HOME}\/bin:$PATH/' ~/.profile  

The two lines above did what I wanted but the second line did a backup of the modified ~/.profile. So I came up with the next solution.


  sed -i .bak  's/^\(export[ ]*JAVA_HOME.*\)/#&\
\export\ JAVA_HOME=\/Users\/${USER}\/\.java_jdk_6\/soylatte16-i386-1\.0\.3\
\export\ PATH=${JAVA_HOME}\/bin:$PATH/' ~/.profile

The new line is achieved by ending a regex with \ and continuing it with \ on the next line. If there are white spaces in front of \ they will also appear in ~/.profile.

Download files recursively with wget

You can download file recursively with the command wget and the option -r. By setting the -e option you can turn off the robots file check.


wget -r http://example.com
wget -e robots=off -r http://example.com

See also the post: Download files recursively with wget

Find files by content

No further explanations, use it as follows:


find . -type f | xargs -l grep -l "the content you are looking for"

Exclude files when listing files

Without enabled globbing files can be excluded by the second command of this code section:


# See all files ending in .html
ls | grep "\.html$"
# see all files not ending in .html
ls | grep -v "\.html$"

If extended globbing is turned on, one should be able to execute


printf "%s\n" *!(.html)

in ksh and bash but I wasn't. I have this command from a forum so there's a chance that either my shell isn't working properly or the syntax has changed in the meantime.

In zsh you enable globbing by inserting setopt extendedglob in .zshrc

In the zsh this should work


ls path/*!(.html)

but I get zsh: number expected.

Even though it's not working yet it's worth to add these commands to this list as the issue can be fixed by reading some more documentation like this one or this one. At the moment I have no time to continue fixing.

Control the volume using the alsa utils

The volume of sound cards can be controlled with amixer.


amixer set -c 0 Master 25 unmute
amixer set -c 0 PCM 25 unmute

See also the post: Unmute the sound card using the ALSA utils

What package does a binary belong to?

To find out what package a binary is installed from, this can be achieved by asking the package manager (pacman) itself with its option -Qo or by using the pkgfile util of the pkgtools package.


➜  ~  pacman -Qo alsamixer
/usr/bin/alsamixer is owned by alsa-utils 1.0.24.2-2
➜  ~  pkgfile alsamixer
extra/alsa-utils

Pacman-related tools

Pactree shows a dependency graph of packages and libraries. Pacman is the package manager in Arch Linux and offers some neat options. With -Q it lists installed packages and followed by one of the following sub-options, it's easy to keep track of what files belong to a certain package or what package a certain file is from.

  • -g a list of packages can be displayed which are bundled in a meta package
  • -l lists files that belongs to the given package
  • -o lists the origin package of the given file

Examples:


pactree 
pactree 
pacman -Q -g base-devel
pacman -Q -l freetype2
pacman -Q -o /usr/lib/libfreetype.so

Order file lists to their size

If you ever wanted to know how to order file lists concerning their file size here are some examples:


du --max-depth=1 | sort -n -r | perl -ne '($s,$f)=split(m{\t});for (qw(K M G)) {if($s<1024) {printf("%.1f",$s);print "$_\t$f";last};$s=$s/1024}'
du -csh === du -chd0
du -csh */ | sort -h
du -sh */ | sort -h
du -h -d1
ls -lhS

Arch Linux

Pacman & Yaourt

Update the packages:


pacman -Syu
yaourt -Syua #a means 'aur', if missing it's the same as the line above

SSH

Get the host's fingerprint:

ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub

If your key type is DAS use ssh_host_dsa_key.pub

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>