请问如何将目录中的所有文件移动到同一目录中的新子目录?
像这样:
/volume1/TEST/file1.jpg to /Volume1/TEST/_NEW_FOLDER/file.jpg
/volume1/TEST2/file2.bmp to /Volume1/TEST2/_NEW_FOLDER/file2.bmp
/volume1/TEST3/file3.dwg to /Volume1/TEST3/_NEW_FOLDER/file3.dwg
/volume1/TEST4/file4.(*) to /Volume1/TEST4/_NEW_FOLDER/file4.(*)
答案1
查找 BaseFolder -mindepth 1 -type f | 读取路径时;执行 \ dest="$(dirname "$path")/NewFolder"; mkdir "$dest"; mv "$path" "$dest"; done
答案2
新答案
从命令行,您可以这样做:
for dir in /volume1/TEST* ; do cd "$dir" ; mkdir _NEW_FOLDER ; mv *.* _NEW_FOLDER ; done
或者写一个类似的脚本
#!/bin/bash
NFLDR=_NEW_FOLDER
for dir in /volume1/TEST*; do (
# for logging to syslog in a cron job, uncomment the following line
# logger moving contents of $dir to $dir/$NEWFLDR
cd "$dir" && mkdir $NFLDR && mv *.* $NFLDR
); done
# logger moving to subfolders finished
然后
chmod +x mvtosubfolders.sh
./mvtosubfolders.sh
编辑:如果您想通过 使用它cron
,请将脚本另存为 eg/usr/local/bin/mvtosubfolders.sh
并使其可执行。调整您的 crontab 以/usr/local/bin/mvtosubfolders.sh
定期调用。例如
# m h dom mon dow command
0 5 * * 1 /usr/local/bin/mvtosubfolders.sh
命令行的输出示例:
root@router ~/testfolder # tree
.
├── TEST1
│ └── file1.jpg
├── TEST2
│ └── file2.bmp
├── TEST3
│ └── file3.dwg
└── TEST4
├── file4.avi
├── file4.cr2
├── file4.mov
└── file4.tif
4 directories, 7 files
root@router ~/testfolder # for dir in /root/testfolder/TEST* ; do cd "$dir" ; mkdir _NEW_FOLDER ; mv *.* _NEW_FOLDER ; done
root@router ~/testfolder/TEST4 # cd ..
root@router ~/testfolder # tree
.
├── TEST1
│ └── _NEW_FOLDER
│ └── file1.jpg
├── TEST2
│ └── _NEW_FOLDER
│ └── file2.bmp
├── TEST3
│ └── _NEW_FOLDER
│ └── file3.dwg
└── TEST4
└── _NEW_FOLDER
├── file4.avi
├── file4.cr2
├── file4.mov
└── file4.tif
8 directories, 7 files
root@router ~/testfolder #
旧的错误答案
不考虑子文件夹
假设您的文件的文件名和扩展名之间有点,就像上面的例子一样:mv *.* _NEW_FOLDER
root@router ~/testfolder # touch file{1,2,3,4,5}.jgp
root@router ~/testfolder # mkdir subfolder
root@router ~/testfolder # ls -al
total 12
drwxr-xr-x 3 root root 4096 Jun 18 09:58 .
drwx------ 24 root root 4096 Jun 18 09:58 ..
-rw-r--r-- 1 root root 0 Jun 18 09:58 file1.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file2.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file3.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file4.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file5.jgp
drwxr-xr-x 2 root root 4096 Jun 18 09:58 subfolder
root@router ~/testfolder # mv *.* subfolder
root@router ~/testfolder # ls -al
total 12
drwxr-xr-x 3 root root 4096 Jun 18 09:59 .
drwx------ 24 root root 4096 Jun 18 09:59 ..
drwxr-xr-x 2 root root 4096 Jun 18 09:59 subfolder
root@router ~/testfolder # ls -al subfolder
total 8
drwxr-xr-x 2 root root 4096 Jun 18 09:59 .
drwxr-xr-x 3 root root 4096 Jun 18 09:59 ..
-rw-r--r-- 1 root root 0 Jun 18 09:58 file1.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file2.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file3.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file4.jgp
-rw-r--r-- 1 root root 0 Jun 18 09:58 file5.jgp
答案3
看到您使用正斜杠,我会假设一个 unix 衍生词:
cd /volume1/
for I in TEST*; do mkdir $I/_NEW_FOLDER; mv $I/* $I/_NEW_FOLDER; done
请注意,当它尝试将其移动_NEW_FOLDER
到自身时会出现错误,但它仍然可以工作。