Mac backups: How to handle spaces in filenames with find, tar, and xargs

This morning I decided to take a few minutes to backup all the songs I've purchased over the last half-year. These are all on my Mac OS X system, under the Music folder in my home directory.

The problem with trying to do this with standard Unix tools is that all these subdirectories and filenames have spaces in their names. Just looking at the Music folder, it contains many directory names like this:

Linux find command: How to find files not matching a pattern

Unix/Linux find "patterns" FAQ: How do I find files or directories that don't match a specific pattern (files not matching a regex pattern, or filename pattern)?

In my case I just ran into a situation where I needed to find all files below the current subdirectory that are NOT named with the filename pattern *.html . Fortunately with the newer Unix/Linux find syntax this solution is pretty easy, you just include the -not argument, like this:

The Linux `chown` command

Unix/Linux file ownership FAQ: How do I use the chown command?

The chown command is most commonly used by Unix/Linux system administrators who need to fix a permissions problem with a file or directory, or many files and many directories.

For instance, suppose you want files to be owned by the user "nobody", but when you issue an ls command, you see that they're owned by the user "fred", like this:

Move Linux files and directories with the mv command

Linux move/rename files FAQ: How do I rename or move Linux files and directories?

You use the Linux mv command to rename or move Linux files and directories. Let's look at some move/rename examples.

Using Linux mv to rename files and directories

At its most basic, here's how you rename a Linux file:

mv Chapter1 Chapter1.old

This mv command renames the file Chapter1 to the new filename Chapter1.old. (Renaming a file is the same as moving it.)

The Linux 'rm' command (remove files and directories)

Linux FAQ: How do I delete files (remove files) on a Unix or Linux system?

The Linux rm command is used to remove files and directories. (As its name implies, this is a dangerous command, so be careful.)

Let's take a look at some rm command examples, starting from easy examples to more complicated examples.

Unix/Linux rm command examples - Deleting files

In its most basic use, the rm command can be used to remove one file, like this:

The Linux copy command (cp)

Linux file copy FAQ: How do I copy Linux files and directories? (Or, Can you share some cp command examples?)

You use the cp command to copy files and directories on Linux systems. Let's look at some copy examples to see how this works.

Using Linux cp to copy files

At its most basic, here's how you copy a Linux file:

The Linux file command

Linux file information FAQ: How can I tell what type of file a file is on a Unix or Linux system?

The Linux file command shows you the type of a file, or multiple files. It's usually used when you're about to look at some type of file you've never seen before. When I first started working with Unix and Linux systems I used it a lot to make sure I wasn't about to open a binary file in the vi editor, amongst other things.

You issue the Linux file command just like other commands, like this:

Git: How do I add an empty directory to a Git project/repository?

Git empty directories FAQ: How do I add an empty directory to a Git repository?

Short answer - you can't. (See below for the workaround.) The design of the Git staging area only accounts for files, as described in the Git FAQ, and other books like Pro Git.

Git empty directories FAQ

Here's the text from the Git FAQ section, "Can I add empty directories to a Git repository":

Linux find: How to search multiple directories with find

Problem: You need to use the Unix/Linux find command to search multiple folders. Specifically, you'd like to search several folders beneath your current location, but not all folders.

For example, your current directory may have 20 subdirectories, and you don't want to search them all like this:

find . -name "*.java"

or this: