Skip to content

Instantly share code, notes, and snippets.

@gacyun
Last active December 6, 2021 08:03
Show Gist options
  • Select an option

  • Save gacyun/e7117e5a3b10553b49be28c719abb372 to your computer and use it in GitHub Desktop.

Select an option

Save gacyun/e7117e5a3b10553b49be28c719abb372 to your computer and use it in GitHub Desktop.

Revisions

  1. gacyun revised this gist Dec 6, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions aws-ami-clean.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    #!/bin/bash

    # Run this script with:
    # bash <(curl -L -s https://gist.github.com/gacyun/e7117e5a3b10553b49be28c719abb372/raw/aws-ami-clean.sh)

    function print_green {
    echo -e "\e[32m${1}\e[0m"
    }
  2. gacyun created this gist Dec 6, 2021.
    34 changes: 34 additions & 0 deletions aws-ami-clean.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/bin/bash

    function print_green {
    echo -e "\e[32m${1}\e[0m"
    }

    function clean_package_cache {
    which yum && {
    yum clean all
    return
    }
    which apt-get && {
    apt-get -y autoremove
    aptitude clean
    aptitude autoclean
    return
    }
    }

    print_green 'clean yum cache'
    clean_package_cache

    print_green 'remove ssh keys'
    [ -f /home/ec2-user/.ssh/authorized_keys ] && rm /home/ec2-user/.ssh/authorized_keys

    print_green 'clean logs'
    find /var/log -type f | while read f; do echo -ne '' > $f; done

    print_green 'clean bash history'
    unset HISTFILE
    [ -f /root/.bash_history ] && rm /root/.bash_history
    [ -f /home/ec2-user/.bash_history ] && rm /home/ec2-user/.bash_history

    print_green 'ami clean finished!'