0

I have an automated deployer script, which takes a git commit to a certain branch, compiles and uploads the archived binary objects to cloud (S3). Later on, the script logs into an EC2 instance and downloads the archives and executes/starts the project/server.

So far, this is working flawlessly. Recently, I've run into disk space issues. Since I am also storing logs and maintaining current/previous working commits, my directory structure is something like:

/path
  /current/       <- symlink
  /prev/          <- symlink
  /HASH00001/
  /HASH00002/
  /HASH00003/
  /HASH00004/
  /HASH00005/
  /.../
  /.../
  /HASH0000n/
  /some.log
  /some.log.1
  /some.log.2.gz
  /...

For the sake of maintenance, I want to delete all the subdirectories in /path which are the specific git commit's SHA value. But, there is a caveat that the directories which are being pointed to by current and prev symlinks should not get deleted.

So far, I am doing this manually on whichever machine facing the storage issue. I'd prefer my deploy script to take care of this step as well.

I was using ls command and sorting the results based on last access timestamp:

ls -drt | head -n -4

and piping xargs rm -rf to it.

I am now thinking of modifying it so that only the directory names of length exactly 40 characters would be listed, and then remove the dirs. symlinked to current and prev.

Can I get some pointers as to how I can achieve the same!

hjpotter92
  • 670
  • 1
  • 10
  • 20

1 Answers1

1

This should list every folder of 40 chars long just below your PATH directory excluding the folders pointed by current and prev symlinks

cd PATH
find . -type d -name '????????????????????????????????????????' ! -name $(readlink current) ! -name $(readlink prev) -prune

Then either -delete if your find supports it or pipe it through xargs rm -rf

NOTE: The pattern should be 40 ?.

hjpotter92
  • 670
  • 1
  • 10
  • 20
NuTTyX
  • 1,168
  • 5
  • 10