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.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    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.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "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, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like: