Tech Reflect
  • Home
  • About This Site
  • Contact me
  • Search Icon
Curly braces on the command line

Curly braces on the command line

2017-06-09

Table of Contents

Toggle
  • Create a complex folder hierarchy
  • Rename files in a hierarchy
  • Rename variation
  • Shortcut for adding extensions
  • Add extensions with wildcard matching
  • List files with multiple extensions

You can do some really interesting and time saving things using curly braces on the bash command line. Essentially, curly braces with a list of things separated by commas means run this command once for each item in the list.

This is best illustrated by a number of examples.

Create a complex folder hierarchy

mkdir -p folder/{1, 2, 3}/{a, b, c}

This creates this folder hierarchy:

folder/
1/
a/
b/
2/
a/
b/
3/
a/
b/

Rename files in a hierarchy

This renames foo/bar/baz/1 to foo/bar/baz/2

mv foo/bar/baz/{1,2} 

Rename variation

This renames /foo/bar/baz/1 to foo/bar/tweedle/2

mv foo/bar/{baz/1, tweedle/2} 

Shortcut for adding extensions

This renames file to file1.backup

mv file1{.,backup}

Add extensions with wildcard matching

This will find files containing the word “banana” and slap an extension on them. This is an interesting case in that {,.bak} means add an extension.

find . -name "banana*" -exec mv \{\}{,.bak} \; 

List files with multiple extensions

This lists all files with txt and doc extensions. Note that wildcards can only be outside the curly braces.

ls *.{txt,doc}

geeky, macOS tips

Post navigation

NEXT
Command-line search with Spotlight
PREVIOUS
Jumping between Terminal and Finder
Comments are closed.

Get Monthly Updates

Recent Posts

  • Inserting random email sigs in Mail on iOS
  • Keep Instagram open to finish posting…
  • How I predicted the rise of Twitter, barely used it, and amassed 35,000 followers
  • Apple Books 2022, in pictures
  • Killing one bird with two-and-a-half stones in Mac OS X Mail

Categories

  • analog (1)
  • apple career (12)
  • apple inside (19)
  • apple stories (20)
  • bertrand serlet (3)
  • bugs (3)
  • essays (15)
  • geeky (21)
  • interviews (4)
  • iOS tips (4)
  • Mac OS X (7)
  • macOS tips (36)
  • personal (8)
  • predictions (1)
  • products (5)
  • prototypes (6)
  • scott forstall (7)
  • scripting (2)
  • siri (2)
  • steve jobs (16)
  • tim cook (1)
  • workplace (15)

Get Monthly Updates

About cricket


Me with Guiness the owl

25 years in tech. I like to write manifestos. I like to offer interesting tips. I like making fun of things. Everyone copes differently.

My Other Blogs

  • Free Range Parrots
  • Plucky Tree (personal)
© 2025   All Rights Reserved.