Uncategorized

Copying dot files from the shell

This isn’t rocket science, more of a note to self.

Found myself moving to a new work machine at work. Part of that involves copying dot files that Mac OSX finder can’t see. I really only want to move the ones I absolutely need, because I’m moving from a machine that I’ve had for nearly 5 years, so a lot of cruft has accumulated.

I have to return the old machine soon though. So the first thing I did was use Carbon Copy Cloner to back up the entire image to a USB drive. Now, I don’t want to carry that USB drive around everywhere. Once I get the code repositories, etc copied I don’t want to be tethered to it. But I want to hang on to all those dot files and directories in case I miss something and need them.

So what I’ve done in the past is a bunch of ls -la commands, and pick out the ones I know I need, manually copying them one by one. Found myself about to do the same thing again, even though there are some I know for sure I don’t need, and while I could just delete those and copy everything, I still want to keep them on the backup because I’m paranoid.

Copying dot files from the terminal can be a little tricky though. Try doing ls -a .*. You get everything in the current directory and subdirectories. But a couple of grep commands can make it pretty easy:

for x in `ls -a | egrep '^\.\w' | egrep -v 'atom|Web|DS_Store|cache|vagrant'`; 
   do 
   cp -rpv $x ~/dotfiles; 
done

So let’s break this down:

for x in `somecommand`

(note: it’s a backtick, not a single quote!) iterates over the lines of output of somecommand

In this case, somecommand is:

ls -a | egrep '^\.\w' | egrep -v 'atom|Web|DS_Store|cache|vagrant'

The ls command with the -a option lists all files in the current directory including hidden (dot) files, and at this point I’ve already cd’d to the directory containing the dot files on my backup drive.

We then pipe that to:

egrep '^\.\w'

that filters the output of ls to only include filenames that start with a ‘.’ (the backtick must be used to escape the period, since otherwise it means match any character) followed by a word character. We match on the word character because otherwise the output would include entries for “.” and “..” which we don’t want to include.

Finally, that output is piped to egrep again, and in this case we’re telling it to exclude output that matches the given patterns, separated by the | character. In this case, I know I don’t want the .DS_Store directory, or the .cache directory, and I haven’t used atom, WebStorm or vagrant in years, so I don’t want to bother copying those files.

Inside the do/done section, we use the copy command to copy each file from the list to a directory (previously created) called ‘dotfiles’ underneath my home directory. We use -r for recursive (so we copy dot directories and everything underneath them, not just the top level files), -p to preserve the permissions and -v so that we see output seeing the files as they are copied (otherwise the output from cp is silent).

Hopefully someone else finds this useful; if nothing else I will be referring back to this myself the next time I have to do this!

Leave a Reply

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