Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

Today I was reading a discussion about mv. The linked gist includes a shell custom function that wraps the mv command and adds functionality if the user only provides one argument.

# wrapped mv command which accepts edits to 
# the provided file path if only one argument was provided
mv planet.png
planet2.png
planet.png -> planet2.png

It allows for editing the file path of provided first argument interactively. That is very neat by itself.

I went on a read the discussion about this command and learned that the mv command, or to make it clearer, bash/zsh make allow to rename a file with something that looks like a single command.

mv a/very/long/path/file.txt a/very/long/path/renamed.txt

You can shorten the above command to the following:

mv a/very/long/path/{file,renamed}.txt

Common shells will extract the {} pattern into its parts and create separate arguments.

mv a/very/long/path/{file,renamed}.txt
# ๐Ÿ‘† becomes ๐Ÿ‘‡
mv a/very/long/path/file.txt a/very/long/path/renamed.txt

Another example is the creation of files that include indices.

touch {1..3}.txt
# ๐Ÿ‘† becomes ๐Ÿ‘‡
touch 1.txt 2.txt 3.txt

Shell scripting. ๐Ÿ’™ There is always something new to learn.

Edited: Dominik pointed out that this shell feature is called brace expansion.

If you enjoyed this article...

Join 5.4k readers and learn something new every week with Web Weekly.

Web Weekly โ€” Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles