Move files with extension in diffrent structure

I need move every file.tif to dir TIFF inside his parent dir.

    ├── 1
    │   ├── 240_01.tif
    │   ├── 240_02.TIF
    │   └── TEST
    │       └── syg_240_test_1.tif
    ├── 2-3
    │   ├── 2
    │   │   ├── 240_01.tif
    │   │   ├── 240_02.TIF
    │   │   └── TEST
    │   │       └── syg_240_test_1.tif
    │   └── 3
    │       ├── 240_01.tif
    │       ├── 240_02.TIF
    │       └── TEST
    │           └── syg_240_test_1.tif
    └── 4
        ├── 240_01.tif
        ├── 240_02.TIF
        └── TEST
            └── syg_240_test_1.tif

For example it's should be look like this:

├── 1
│   ├── TEST
│   │   └── syg_240_test_1.tif
│   └── TIFF
│       ├── 240_01.tif
│       └── 240_02.TIF
├── 2-3
│   ├── TEST
│   │   └── syg_240_test_1.tif
│   └── TIFF
│       ├── 2
│       │   ├── 240_01.tif
│       │   └── 240_02.TIF
│       └── 3
│           ├── 240_01.tif
│           └── 240_02.TIF
└── 4
    ├── TEST
    │   └── syg_240_test_1.tif
    └── TIFF
        ├── 240_01.tif
        └── 240_02.TIF

I try to use mv /path/*/*.tif/ /path/*/TIFF/*.tif but it doesn't work.


Solution 1:

Again, find may come to the rescue. You can selectively find tiff files in the first level subfolders, then use an -execdir command that moves the found file to a TIFF folder in the current directory.

You could call a little script that tests for the existence of the TIFF folder, or else creates it before moving all tiff files, but for this single time, it is probably easier to work in two steps: 1) make the TIFF folder in any of the folders you need and 2) move the TIFF files there

find . -maxdepth 1 -type d -path '*/*' -exec mkdir {}/TIFF \;

will find the folders "1", "2" etc provided your current directory is the one containing these folders, and create a TIFF directory in each of them. There will be an error message if the folder TIFF already exists.

A second command can then move all tiff files out to the newly created folders:

find . -type f -ipath '*/*/*.tif' -execdir mv {} TIFF \;

Here, we search for files only (-type f) in the folders "1", "2" etc, but not below, because of the file pattern (-ipath). -ipath as opposed to -path indicates that the match is case insensitive. The -execdir action performs the subsequent command, however while the current folder is that of the found file. {} stands for the found file. Because of the -execdir, this is the file's basename only. The file will be moved to the TIFF folder in the current folder.

Spaces in the file name will be properly handled by the {} token. There is no need to insert quotes, although you could.