Skip to main content
`-empty`, `-not`, `{}` not on its own are not standard.
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

From Ryan Armstrong's blog, here's how you do it with GNU find or compatible:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursively) looks for directories under current path
  • -empty filters out directories that already contain something
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files 

From Ryan Armstrong's blog, here's how you do it

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursively) looks for directories under current path
  • -empty filters out directories that already contain something
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files 

From Ryan Armstrong's blog, here's how you do it with GNU find or compatible:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursively) looks for directories under current path
  • -empty filters out directories that already contain something
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files 
added 18 characters in body
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

From Ryan Armstrong's blog, here's how you do it

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursilveyrecursively) looks for directories under current path
  • -empty filters-out intermediate out directories that already contain something
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files 

From Ryan Armstrong's blog, here's how you do it

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursilvey) looks for directories under current path
  • -empty filters-out intermediate directories
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files 

From Ryan Armstrong's blog, here's how you do it

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursively) looks for directories under current path
  • -empty filters out directories that already contain something
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files 
Source Link

From Ryan Armstrong's blog, here's how you do it

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 
  • find . -type d (recursilvey) looks for directories under current path
  • -empty filters-out intermediate directories
  • -not -path "./.git/*" ensures no files are created inside .git directory
  • -exec touch {}/.gitkeep \; creates empty .gitkeep file in each directory matching above criteria

The resulting structure looks like

$ tree -a -I .git . ├── .gitignore ├── README.md ├── pom.xml └── src ├── main │   ├── java │   │   └── .gitkeep │   └── resources │   └── .gitkeep └── test ├── java │   └── .gitkeep └── resources └── .gitkeep 7 directories, 7 files