简体   繁体   中英

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. 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.

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.

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. 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.

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 ./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.

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM