X

Bash Shell Tricks – The brace extension

When working with the Linux command line, there are situations where you need to type large paths. Even if you use the bash shell's autocomplete feature, this is a time-consuming process.

But do you know that there are times when you can get away with entering a path only once (even if it has to be entered, say, twice)? In this tutorial, we'll briefly 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 20.04 LTS.

Tricks for the Bash shell - bracket extension

Yes, there are certain tricks that can save you a lot of time when working on the command line. We will discuss one such trick here.

Let's say you want to rename a file that is located in a directory other than your current working directory. For example, imagine a file "file.txt" located in the directory "/home/himanshu/find-comm/examples/find/howtoforge".

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

Ideally, the command should be:

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.

Till Brehm:
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings