将文件从当前目录移动到子目录

将文件从当前目录移动到子目录

我在许多目录中有多个文件,并且想将特定文件从其当前目录移动到子目录。当我尝试 find

find . -type f -name '*.json' -prune -type f | xargs mv -t JSONS/

它不起作用,因为路径不正确。它想将文件移动到 ./JSONS/。那么我如何将目录中的文件移动到子目录?

├── subdirectory-A
│   ├── 1.mp4
│   ├── 1.json
│   ├── 2.mp4
│   ├── 2.json
│   ├── 3.mp4
│   └── 4.json
└── subdirectory-B
    └── subdirectory-C
        ├── 1.mp4
        ├── 1.json
        ├── 2.mp4
        ├── 2.json
        ├── 3.mp4
        └── 4.json

├── subdirectory-A
│   ├── JSON
        ├──1.json
        ├──2.json
        ├──3.json
│   ├── 1.mp4
│   ├── 2.mp4
│   ├── 3.mp4
└── subdirectory-B
    └── subdirectory-C
        ├── JSON
            ├──1.json
            ├──2.json
            ├──3.json
        ├── 1.mp4
        ├── 2.mp4
        ├── 3.mp4

答案1

我假设因为您的原始find命令包含-prune(尽管放错了位置),所以您还必须处理某些文件.json已经在JSON子目录中并且不应移动到JSON/JSON子目录中的情况。

因此,您需要 (1) 跳过JSON子目录中已有的文件,以及 (2)JSON在需要时创建子目录。因此

find . -type f -name '*.json' ! -path '*/JSON/*.json' -execdir sh -c '
        mkdir -p ./JSON
        for f; do 
          mv -vt ./JSON/ "$f"
        done
      ' find-sh {} +

例如:

$ tree subdirectory-*
subdirectory-A
├── 1.json
├── 1.mp4
├── 2.json
├── 2.mp4
├── 3.json
└── 3.mp4
subdirectory-B
└── subdirectory-C
    ├── 1.json
    ├── 1.mp4
    ├── 2.mp4
    ├── 3.json
    ├── 3.mp4
    └── JSON
        └── 2.json

2 directories, 12 files

然后

$ find . -type f -name '*.json' ! -path '*/JSON/*.json' -execdir sh -c '
    mkdir -p ./JSON
    for f; do 
      mv -vt ./JSON/ "$f"
    done
  ' find-sh {} +
renamed './1.json' -> './JSON/1.json'
renamed './3.json' -> './JSON/3.json'
renamed './nodes.json' -> './JSON/nodes.json'
renamed './test.json' -> './JSON/test.json'
renamed './test2.json' -> './JSON/test2.json'
renamed './file.json' -> './JSON/file.json'
renamed './data.json' -> './JSON/data.json'
renamed './1.json' -> './JSON/1.json'
renamed './2.json' -> './JSON/2.json'
renamed './3.json' -> './JSON/3.json'

导致

$ tree subdirectory-*
subdirectory-A
├── 1.mp4
├── 2.mp4
├── 3.mp4
└── JSON
    ├── 1.json
    ├── 2.json
    └── 3.json
subdirectory-B
└── subdirectory-C
    ├── 1.mp4
    ├── 2.mp4
    ├── 3.mp4
    └── JSON
        ├── 1.json
        ├── 2.json
        └── 3.json

3 directories, 12 files

您可以-v从中删除标志mv(该标志仅用于说明目的)。

答案2

(移动)命令mvcp(复制) 命令不会像在 Windows 中那样创建目录。

但这是可行的:

# loop through all subdirectories
for d in $(find . -type d) ; do
    echo $d
    myarray=( $(find $d -maxdepth 1 -name "*.json") )
    if [ ${#myarray[@]} -gt 0 ]; then 
        mkdir -p $d/JSON/
        mv $d/*.json $d/JSON/
    fi
done

你应该保持怀疑态度,先进行测试。下面是测试脚本:

#!/bin/bash

# NAME: move (move file pattern to new sub-directory name)
# PATH: $HOME/askubuntu/
# DESC: Answer for: https://askubuntu.com/questions/1182501/move-files-from-its-current-directory-into-subdirectory
# DATE: October 20, 2019.

CreateFiles () {
mkdir -p subdir-A
mkdir -p subdir-B/subdir-C

echo "1 json" >> subdir-A/1.json
echo "2 json" >> subdir-A/2.json
echo "3 json" >> subdir-A/3.json
echo "4 json" >> subdir-A/4.json
echo "1 file" >> subdir-A/1.mp4
echo "2 file" >> subdir-A/2.mp4
echo "1 file" >> subdir-A/3.mp4

echo "1 json" >> subdir-B/subdir-C/1.json
echo "2 json" >> subdir-B/subdir-C/2.json
echo "3 json" >> subdir-B/subdir-C/3.json
echo "4 json" >> subdir-B/subdir-C/4.json
echo "1 file" >> subdir-B/subdir-C/1.mp4
echo "2 file" >> subdir-B/subdir-C/2.mp4
echo "1 file" >> subdir-B/subdir-C/3.mp4
}

tree -h subdir*

CreateFiles       # Only do this once then comment it out for next time.

# loop through all subdirectories
for d in $(find . -type d) ; do
    echo $d
    myarray=( $(find $d -maxdepth 1 -name "*.json") )
    if [ ${#myarray[@]} -gt 0 ]; then 
        mkdir -p $d/JSON/
        mv $d/*.json $d/JSON/
    fi
done

运行它时会发生以下情况:

$ move

subdir-A
├── [   7]  1.json
├── [   7]  1.mp4
├── [   7]  2.json
├── [   7]  2.mp4
├── [   7]  3.json
├── [   7]  3.mp4
└── [   7]  4.json
subdir-B
└── [4.0K]  subdir-C
    ├── [   7]  1.json
    ├── [   7]  1.mp4
    ├── [   7]  2.json
    ├── [   7]  2.mp4
    ├── [   7]  3.json
    ├── [   7]  3.mp4
    └── [   7]  4.json

1 directory, 14 files
.
./subdir-B
./subdir-B/subdir-C
./subdir-A
subdir-A
├── [   7]  1.mp4
├── [   7]  2.mp4
├── [   7]  3.mp4
└── [4.0K]  JSON
    ├── [   7]  1.json
    ├── [   7]  2.json
    ├── [   7]  3.json
    └── [   7]  4.json
subdir-B
└── [4.0K]  subdir-C
    ├── [   7]  1.mp4
    ├── [   7]  2.mp4
    ├── [   7]  3.mp4
    └── [4.0K]  JSON
        ├── [   7]  1.json
        ├── [   7]  2.json
        ├── [   7]  3.json
        └── [   7]  4.json

3 directories, 14 files

相关内容