[英]Moving all directories which have a specified number of files, to another directory
Apologies if the question is unclear - 如果问题不清楚,我们深表歉意-
I have a large number (~1000) of directories, each with about 4000 files in them. 我有大量(〜1000)目录,每个目录中包含约4000个文件。 I was running a script that made a copy (with some modifications) of each file in each directory, but my system crashed before it could complete. 我运行的脚本为每个目录中的每个文件创建了一个副本(进行了一些修改),但是我的系统在完成之前崩溃了。 Now I see that around 200 of the directories are 'done', but I need to run the script again on the remaining 800 directories. 现在,我看到大约200个目录已“完成”,但是我需要在其余800个目录上再次运行该脚本。
My question is - is there a way to find out (and perhaps move) all directories that have more than a specified number of files? 我的问题是-有没有办法找出(也许移动)具有超过指定数量文件的所有目录? I was hoping to find a way to move all directories that have ~8000 files in them (ie those that are 'done') to some other path, and run my script again on the remaining directories. 我希望找到一种方法,将其中包含约8000个文件的所有目录(即“完成”的目录)移动到其他路径,然后在其余目录上再次运行我的脚本。
I suppose there should be a way to write a bash script for this, but I am not very familiar with bash scripting, and I am unable to find a solution online. 我想应该有一种方法可以为此编写bash脚本,但是我对bash脚本并不十分熟悉,因此无法在线找到解决方案。 So far I have found how to find out the number of files in each directory, but how to perform some action on a directory based on the number of files it has? 到目前为止,我已经找到了如何找出每个目录中文件的数量,但是如何根据目录中的文件数量对目录执行某些操作呢?
Any advice/pointers appreciated. 任何建议/指针表示赞赏。
EDIT - I also read about the seq
utility, which I could have used to specify the directories I wanted moved, but unfortunately the directories that are 'done' do not belong to any sequence. 编辑-我还读了seq
实用程序,该实用程序可以用来指定我想要移动的目录,但是不幸的是,“完成”的目录不属于任何序列。
Sure it is unclear! 当然还不清楚!
Let's say you have 假设您有
./base
./base/dir1
./base/dir2
./base/dir3
...
You should use find
, only as a way to start your search 您应该使用find
,仅作为开始搜索的一种方式
find ./base -type d
Them you will need a script to count dirs into each entry, and a test to decide when take an action into a directory. 他们将需要一个脚本来将Dirs计入每个条目,并需要一个测试来确定何时对目录进行操作。
#make a list of dirs, and put one ata time in ONE_DIR variable
find ./base -type d | while read ONE_DIR
do
# count how many files/dirs there are into the current dir
CONTENTS_NUM=$(ls -1 ${ONE_DIR} | wc -l)
# check if it is a upper 8000 elements dir
if [[ "${CONTENTS_NUM}" -ge 8000 ]]
then
#execute here whetever you want with this directory, you can add more lines if you need
make_something ${ONE_DIR}
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.