<p>I want to delete all but the 4 newest directories in my parent directory. How would you do this in Bash?</p>

[{'answer_id': 71891, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 4, 'selected': True, 'text': "<pre><code>ls -atrd */ | head --lines=-4 | xargs rm -rf\n</code></pre>\n\n<p><strong>Edit:</strong> added 'a' argument to ls</p>\n"}, {'answer_id': 71897, 'author': 'Alexey Feldgendler', 'author_id': 10682, 'author_profile': 'https://Stackoverflow.com/users/10682', 'pm_score': 1, 'selected': False, 'text': '<p>Please clarify if you mean “delete all directories but the four newst ones” or “delete everything (files and directories) except for the four newest directories”.</p>\n\n<p>Please also note that creation times are not known for directories. One can only tell when a directory was last modified, that is, had files added, removed or renamed.</p>\n'}, {'answer_id': 71972, 'author': 'mana', 'author_id': 12016, 'author_profile': 'https://Stackoverflow.com/users/12016', 'pm_score': 1, 'selected': False, 'text': '<p>you could do the following:</p>\n\n<pre><code>#!/bin/bash\n\n#store the listing of current directory in var\nmydir=`ls -t`\nit=1\n\nfor file in $mydir\n    do\n        if [ $it -gt 5 ]\n        then\n            echo file $it will be deleted: $file\n            #rm -rf $file\n        fi\n        it=$((it+1))\n    done\n</code></pre>\n\n<p>(remove the # before rm to make it really happen ;) )</p>\n'}, {'answer_id': 72115, 'author': 'mdxi', 'author_id': 11164, 'author_profile': 'https://Stackoverflow.com/users/11164', 'pm_score': 0, 'selected': False, 'text': '<p>Another, BSD-safe, way to do it, with arrays (why not?)</p>\n\n<pre><code>#!/bin/bash\nARRAY=( `ls -td */` )\nELEMENTS=${#ARRAY[@]}\nCOUNTER=4\nwhile [ $COUNTER -lt $ELEMENTS ]; do\n  echo ${ARRAY[${COUNTER}]}\n  let COUNTER=COUNTER+1\ndone\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/71864', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12015/']