We're working on something new! Hook Relay gives you Stripe-quality webhooks in minutes. Sign up for free today! Check out Hook Relay

Move a file in *nix without retyping whole path

How to use brace expansion to move a file in *nix without the unbearable tedium of typing a full directory name.

If you've been using the Unix/Linux command line for any length of time, you're certainly familiar with time-saving techniques like tab completion and reverse-i-search. Chances are you use these darling keystroke-savers daily.

These features of course make a ton of sense—why should *nix force you through the unbearable tedium of typing a full directory name when the computer is perfectly capable of guessing what you're about to type?

Well, today I'm about to (hopefully) blow your mind with another time-saver of whose existence you might not have been aware. It's called brace expansion.

The problem

Tell me if this looks familiar: you want to make just a minor change to a filename, but since the file is so deeply nested in your directory structure, you have to type SO...MUCH...STUFF just to slap a few characters onto the beginning:

mv app/views/appointments/_services.html.erb app/views/appointments/_service_list.html.erb

Totally un-DRY.

The solution

Thanks to brace expansion, you can type only the unique parts. The following command is functionally equivalent to the first one:

mv app/views/appointments/{_services.html.erb,_service_list.html.erb}

Isn't that wonderful? Go ahead and pop open a console right now to try it out. It's an absolute delight.

It works everywhere

The utility of brace expansion is not limited to the mv command. You can use it withcp or anywhere else you think makes sense. If you're a Vim user, you can try something like this, which will open three files in tabs:

vi -p app/views/appointments/{new.html.erb,edit.html.erb,show.html.erb}

In addition to useful things you can do silly things: touch file{1..5}.html Why would you ever want to do that? I don't know, but you can!

That's it. Hopefully that seems useful. You'll thank me when you're 90 years old and thinking back to all the wonderful things you did with those extra 12 minutes you saved over the years, courtesy of brace expansion.

Honeybadger has your back when it counts. We're the only error tracker that combines exception monitoring, uptime monitoring, and cron monitoring into a single, simple to use platform.

Our mission: to tame production and make you a better, more productive developer. Learn more

author photo

Starr Horne

Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

“We’ve looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release.”
Michael Smith
Try Error Monitoring Free for 15 Days