1

I have the following hierarchy for how my Python applications are stored:

PyCharmProjects/app/versions/v1
PyCharmProjects/app/versions/v2
PyCharmProjects/app/versions/v3
PyCharmProjects/app/versions/v4
PyCharmProjects/app/versions/v5
PyCharmProjects/app/versions/v6

How can I write a bash script to remove all directories that do not correspond to the latest 2 versions and are older than 7 days?

Like I want to remove v4, v3, v2 and v1 as well since it was created 9 days ago.

Is it possible to do this using regex?

Also is there any way to do this within PyCharm itself?

  • Try logrotate, seems more suited to this purpose. – steve Aug 18 '18 at 18:37
1

We can exclude the path in find command using ! -path as below. which will exclude the v5 directory in finding.

find directory/* ! -path directory/v5 

To exclude multiple directories, repeat the same ! -path again in find command as below. which will exclude both v4 and v5 in finding.

find directory/* ! -path directory/v4 ! -path directory/v5 

In your scenario, we don't Know the folder name to exclude. So we can implement another find command in place of exclude directory.

The below code will get you the latest folder.

find PyCharmProjects/app/versions ! -path PyCharmProjects/app/versions  -type d -printf '%T@ %P\n' | sort -nr | head -n1 | awk '{print $2}'
  • -printf '%T@ %P\n' will list the folders with timestamp
  • sort -nr will do the reverse sort, so that latest folder will be on top.
  • head -n1 will extract the latest folder

And for the second latest folder.

find PyCharmProjects/app/versions ! -path PyCharmProjects/app/versions  -type d -printf '%T@ %P\n' | sort -nr | head -n2 | tail -n1 | awk '{print $2}'
  • head -n2 will get you the latest two folder.
  • tail -n1 will get you the second latest folder.

Implement the above two codes in place of v4 and v5 respectively.

 find PyCharmProjects/app/versions/* -maxdepth 1 ! -path PyCharmProjects/app/versions/`find PyCharmProjects/app/versions ! -path PyCharmProjects/app/versions  -type d -printf '%T@ %P\n' | sort -nr | head -n2 | tail -n1 | awk '{print $2}'` ! -path PyCharmProjects/app/versions/`find PyCharmProjects/app/versions ! -path PyCharmProjects/app/versions  -type d -printf '%T@ %P\n' | sort -nr | head -n2 | tail -n1 | awk '{print $2}'` -type d -mtime +7 -delete
  • ! -path to exclude the path
  • -type -d to find directories
  • -maxdepth 1 in current directory
  • -mtime +7 directories which modified 7 days ago.
  • -delete delete the found directories.
  • how does sort by timestamp work? Is the timestamp dependent on time of creation of directory? Isn't there a find command line argument that allows us to do the same? Also, do you think there is a possible solution involving regex? @SivaPrasath – walksignison Aug 19 '18 at 13:02
  • @walksignison its last modified, not created. – msp9011 Aug 19 '18 at 13:03
  • what do you think about a regex based solution? – walksignison Aug 19 '18 at 14:19
1

Also is there any way to do this within PyCharm itself?

Assuming that the directories represent different versions of the program you're working on, the standard and space-efficient way to handle this is by using a version control system such as Git.

Once Git is set up, switching to a new version should be as easy as typing something like

$ git add .
$ git commit -m 'Commit message, e.g. "Rewrote the FooBar class, added logging."'
$ git push

This sequence will be tantamount to copying your project into a new directory, except for it being much more space-efficient and not require deletion ever.

You will always be able to roll back to any previous version if need be with a simple git checkout COMMIT_ID command.

Pycharm has integrated Git support, see the detailed information here and here. However, you can also use the standard command line tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.