简体   繁体   English

将具有指定数量文件的所有目录移动到另一个目录

[英]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.

相关问题 在pwd中查找目录中所有目录的文件数 - Finding the number of files in a directory for all directories in pwd shell 脚本:我有一个包含几个其他目录的主目录,我想打印这些目录中的所有文件 - shell scripting : I have a main directory which contain few other directories and I want to print all files inside those directories 对于所有子目录,将目录中指定扩展名的所有文件递归转换为pdf - Convert all files of a specified extension within a directory to pdf, recursively for all sub-directories 删除所有文件和目录-除非指定 - Delete all files and directories - except specified 如何编写将一个目录下的所有文件和目录复制到另一个目录的linux脚本? - How to write a linux script that copies all the files and directories under one directory to another directory? 在目录树中查找每个目录中的文件数 - Find number of files in each directory in a tree of directories linux目录中的文件和目录数,仅在2级 - Number of files and directories in linux directory, only in level 2 使用shell查找文件夹中没有指定文件模式的目录 - Using shell to finds directories that do not have a specified pattern of files in the folder shell脚本:将文件移动到另一个目录 - shell scripting: moving files to another directory 如何在 Linux C 程序中显示用户指定名称为“ls dir_name”的目录中的所有文件 - How to display all files in directory of which name is specified by the user as “ls dir_name” in Linux C program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM