0

I have the following directory and sub directories structure:

eva tree
.
├── 1061
│   └── 2022
│       └── 09
│           └── 21
│               └── a0e51f58-5057-4002-b4c4-d3fb870e9b3a.json
├── 1769
│   └── 2022
│       └── 08
│           └── 30
│               └── e36d8e21-5184-489f-89b5-eb1fd5eba5f6.json
├── 1991
│   └── 2022
│       └── 09
│           └── 16
│               └── 1d0a4162-7e66-44c8-8b61-f3bc5dbdb107.json

I need to move all .json files to root folder eva.

Expected output:

.
│a0e51f58-5057-4002-b4c4-d3fb870e9b3a.json
│e36d8e21-5184-489f-89b5-eb1fd5eba5f6.json
│1d0a4162-7e66-44c8-8b61-f3bc5dbdb107.json

How can I do it using bash?

cigien
  • 57,834
  • 11
  • 73
  • 112
Marcelo Gazzola
  • 907
  • 12
  • 28

1 Answers1

3

You can use find . -type f -name '*.json' -exec mv {} . \;

  • find . searches across the current directory;
  • -type f all objects of type f (aka file);
  • -name '*.json' file names that ends with the .json;
  • -exec [COMMAND]\; for each file found, run [COMMAND];
  • mv {} ., the curly brackets {} represent the found file, the command mves it to the current directory (.)
ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
  • 3
    A more secure and efficient version would be `find . -type f -name "*.json" -exec mv -t . '{}' +` which will build a command line from the files in each directory invoking `mv` on a group of files instead of once per-file. (if there is only one .json file per-dir, that's not an improvement) – David C. Rankin Oct 07 '22 at 04:08