While working on the Linux command line, there are situations where-in you need to type large paths. Even if you use the Bash shell's auto-complete feature, it's a time consuming process.

But do you know, there are times when you can get away with typing a path only once (even if it's required to be entered, say, twice)? In this tutorial, we will quickly discuss how you can do that.

But before that, it's worth mentioning that all the commands/examples/instructions mentioned here have been tested on Ubuntu 14.04LTS with Bash version 4.3.11.

Bash shell tricks - Brace Expansion

Yes, there are certain tricks that help you save a lot of time while working on the command line. We'll discuss one such trick here.

Suppose you want to rename a file kept in some directory (other than your present working directory). For example, imagine a file 'file.txt' kept in the directory '/home/himanshu/find-comm/examples/find/howtoforge'.

And the requirement is to rename it to 'newfile.txt' with a condition that you don't have to change your current working directory (which is say '/etc/').

Ideally, the command should have been:

mv /home/himanshu/find-comm/examples/find/howtoforge/file.txt /home/himanshu/find-comm/examples/find/howtoforge/newfile.txt

But you can save time here by typing the complete path only once in the following way:

mv /home/himanshu/find-comm/examples/find/howtoforge/{file.txt,newfile.txt}

or better:

mv /home/himanshu/find-comm/examples/find/howtoforge/{file,newfile}.txt

Here's another example:

touch verbigfilename1 verybigfilename2 verybigfilename3

The above command can be written as:

touch verbigfilename{1,2,3}

This technique is known as Brace Expansion. You can read more about it here.

How to avoid typing complete path (or large file names) multiple times in Bash shell

Leave a Reply

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

*