Tag Archives: bash

Move iTunes Directories

This is probably not too helpful, since macOS Catalina has broken iTunes into an app for every media type, so the details of this post are mostly outdated if you've updated to Catalina. But I still have pre-Catalina (Mojave) on my MacBook Pro, and recently I searched and searched for an easy way to move my iTunes directory and couldn't find it.

To clarify what I mean when I say "move my iTunes directory": I don't allow iTunes to "Organize" my iTunes library. I choose where the files are located and how they are named and organized. There are lots of articles (including official Apple documentation) that explain how to move the iTunes-managed library directory. But none of those tutorials explain how to move your files from one directory to another when you're managing that location yourself.

This is a three-step process.

Step 1: Copy the files

The easy way to copy the files is using the macOS Finder, but if you have thousands of files this could be slow, as Finder seems to do a bunch of calculation up front so it can do fancy stuff like show progress bars. I'd prefer to do this quickly, so I wrote this bash script to safely copy my Music directory, subdirectory by subdirectory.

Script output

Step 2: Update iTunes track locations

This is the more challenging part, because your iTunes library is stored in a database file that isn't editable with external tools. A common work-around that is recommended is to export your iTunes library as XML and edit the XML, then re-import that edited XML as your new library. I've done that before, and found that it's very error-prone and leaves a lot of manual cleanup remaining.

So, I wrote an AppleScript to go through every track in your iTunes library and change its location from the old directory to the new directory.

On my iTunes library of about 10,000 tracks, it took quite a while to churn through everything: I didn't time it, but it felt like about 30 minutes. Maybe it was less.

Step 3: Remove the old files

After you're sure everything has been copied correctly and your iTunes library is correctly updated, you can remove the old copy of your files.

Yosemite Upgrade Changes Open File Limit

OSX has a ridiculously low limit on the maximum number of open files. If you use OSX to develop Node applications -- or even if you just use Node tools like grunt or gulp -- you've no doubt run into this issue.

To address this, I have this line in my $HOME/.bash_profile:

ulimit -n 1000000 unlimited

And a corresponding entry in /etc/launchd.conf:

limit maxfiles 1000000

That solved the problem until I upgraded to OSX Yosemite, after which I began seeing the following error every time I opened a terminal window:

bash: ulimit: open files: cannot modify limit: Invalid argument


Luckily, I a little Google foo yielded this Superuser post (and answer).

So it was a quick fix:

$ echo kern.maxfiles=65536 | sudo tee -a /etc/sysctl.conf
$ echo kern.maxfilesperproc=65536 | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -w kern.maxfiles=65536
$ sudo sysctl -w kern.maxfilesperproc=65536
$ ulimit -n 65536 65536    

Then I updated my $HOME/.bash_profile to change the ulimit directive to match that last command, above, and I was back in business.

Easily prune your ssh known_hosts file

At some point, you've probably seen this message when you try to log in to one of your servers:

ssh failure message

This is really common when you have Amazon EC2 instances behind Elastic IPs because the IP address stays the same (and probably the hostname, too), but as new instances replace old instances, the new instances' ssh keys are probably different.

But if you look carefully, you'll see that the failure message tells you how to resolve this problem:

Offending RSA key in /Users/[username]/.ssh/known_hosts:5

That means that line no. 5 of the known_hosts file contains the problematic key. So, assuming that you are sure this is NOT in fact a security breach, you can remove that line.

It's a bit of a pain-in-the-butt to manually edit this file, though. You can use sed to do it easily, but if you're like me and you don't use sed all the time, you need to look at the man pages every time you want to use it. That's why I wrote this quick bash script to do it automatically.

Drop that in your PATH and make it executable. Then you can simply type ssh-purge-host 5 to remove line 5 from your known_hosts file.

Hope that's useful!

New git alias: git last

I made a new git alias I'm loving. Maybe you have something similar.

I've added this to my .gitconfig:

    last = rev-parse --abbrev-ref @{-1}

This gets the name of the branch you had checked out prior to the current branch. It's like git checkout -, but you can use it all over, such as:

$ git merge `git last`

Using watch with a bash alias

I love the Unix watch command. On OSX, you can install it easily with Homebrew:

brew install watch

Something I didn't realize until 10 minutes ago is that if you want to watch the output of something in your bash aliases, watch will complain because it cannot find the command. This is because watch evaluates the command you pass to it with 'sh -c', which does not expand aliases. However, if you also create an alias for watch itself, aliases will work. So, you can add the following to your .bashrc:

alias watch='watch '

Note the trailing space inside the quotation marks.


WordPress Update Bash Script

I wrote this script some time ago. It's been working flawlessly for me, so I thought I'd share it here. It could use some progress messages, I suppose.

DIR= # Put the file system path to your WordPress installation here. E.g., /var/www/html/blog
rm -rf latest.zip ./wordpress # Clean up from the last run
wget -nd http://wordpress.org/latest.zip
unzip latest.zip
mv $DIR/wp-config.php $DIR/.config # Stash your configuration someplace safe
rm $DIR/*.{txt,html,php} # Delete the old install
rm -rf $DIR/{wp-admin,wp-includes} # Delete more. Don't delete plugins or themes.
cp -aR $WPDIR/* $DIR/
mv $DIR/.config $DIR/wp-config.php # Restore the configuration
# You may not need the last two lines. I like to give my web server the ability to write files.
chown -R .www-data $DIR/*.php $DIR/wp-admin $DIR/wp-includes
chmod -R g+w $DIR/*.php $DIR/wp-includes

Bash script: ted

I just wrote a little bash script, "ted" (for Tracking EDitor), which I am loving. You call ted like you would call your usual text editor, and ted backs up the file you're editing, appending the originial timestamp as a suffix. Then, when you're done editing the file, ted runs diff to keep a running log of changes you've made to the file. If you've made no changes, ted removes the backup file and exits without running diff.

Introducing: ted.