【发布时间】:2019-10-06 02:09:00
【问题描述】:
我想根据文件的数量(这里:50)将文件从文件夹(名称:1)复制到多个文件夹。
下面给出的代码有效。我根据文件数将文件夹中的所有文件传输到子文件夹,然后将目录中的所有文件复制回初始文件夹。 但是,我需要更清洁、更高效的东西。为下面的混乱道歉,我是nube。
bf=1 #breakfolder
cd 1 #the folder from where I wanna copy stuff, contains 179 files
flies_exist=$(ls -1q * | wc -l) #assign the number of files in folder 1
#move 50 files from 1 to various subfolders
while [ $flies_exist -gt 50 ]
do
mkdir ../CompiledPdfOutput/temp/1-$bf
set --
for f in .* *; do
[ "$#" -lt 50 ] || break
[ -f "$f" ] || continue
[ -L "$f" ] && continue
set -- "$@" "$f"
done
mv -- "$@" ../CompiledPdfOutput/temp/1-$bf/
flies_exist=$(ls -1q * | wc -l)
bf=$(($bf + 1))
done
#mover the rest of the files into one final subdir
mkdir ../CompiledPdfOutput/temp/1-$bf
set --
for f in .* *; do
[ "$#" -lt 50 ] || break
[ -f "$f" ] || continue
[ -L "$f" ] && continue
set -- "$@" "$f"
done
mv -- "$@" ../CompiledPdfOutput/temp/1-$bf/
#get out of 1
cd ..
# copy back the contents from subdir to 1
find CompiledPdfOutput/temp/ -exec cp {} 1 \;
需要的目录结构是:
parent
________|________
| |
1 CompiledPdfOutput
| |
(179) temp
|
---------------
| | | |
1-1 1-2 1-3 1-4
(50) (50) (50) (29)
“()”里面的数字表示文件的个数。
顺便说一句,我的代码的最后一步给出了这个警告,如果有人能解释正在发生的事情和解决方案,我会很高兴。
cp: -r not specified; omitting directory 'CompiledPdfOutput/temp/'
cp: -r not specified; omitting directory 'CompiledPdfOutput/temp/1-4'
cp: -r not specified; omitting directory 'CompiledPdfOutput/temp/1-3'
cp: -r not specified; omitting directory 'CompiledPdfOutput/temp/1-1'
cp: -r not specified; omitting directory 'CompiledPdfOutput/temp/1-2'
我也不想复制目录,只复制文件,所以给 -r 会很糟糕。
【问题讨论】:
-
尝试更改您的
findcmd 以包含-type f。否则,如果这给出了您需要的结果,请将其标记为“已关闭”。或者至少把改进放在次要位置。最好将时间花在可以使您的流程或其他任务更轻松、更高效的其他代码上。一个合理(和好的)代码和一个很好的问题,考虑到这种性质的类似的写得很差的问题在这里出现的频率。继续发帖,祝你好运!