I still run an EC2 instance on AWS that I configured years ago to host some of my side projects. It’s using a Linux AMI (Amazon machine image) that is woefully out of date and also wonderfully under-performant when traffic on my site is heavy.
Recently my EBS (elastic block store) volume that I have attached to my EC2 instance became full. Not wanting to shell out a trivial amount of cash to upgrade the size of my EC2 instance or EBS volume, I began exploring how I could remove some unnecessary files on my machine that were taking up space.
Checking disk usage and free disk space
To check your machine’s disk usage and see how much free space is left, you can run the following command in your terminal:
df -h
This lists all the filesystems mounted on your machine, where they are located, their size, how much storage space is used, and how much storage space is available.
For me, this reported that my root filesystem was 100% full!
Source: https://www.howtogeek.com/409611/how-to-view-free-disk-space-and-disk-usage-from-the-linux-terminal/
Auto-removing unnecessary dependencies
When installing a dependency on a Linux machine, it’s common to use APT, the Advanced Package Tool. Over time, it’s possible that you may have packages installed that you no longer need. To auto-remove any unnecessary dependencies you may have installed, you can run:
sudo apt-get autoremove
Cleaning up cached packages
It’s also helpful to clean up the package files stored in the /var/cache directory on your machine. You can do so by running this command:
sudo apt-get clean
Source: https://www.networkworld.com/article/3453032/cleaning-up-with-apt-get.html
Finding and deleting files with filenames that match a pattern
Back in my early days of using Git, I didn’t fully understand the value of the .gitignore file, which tells Git to, well, ignore certain files or directories. Because I failed to use this file to my advantage, I had plenty of files like .DS_Store or NPM debug logs taking up space on my machine.
To find all the .DS_Store files in all subdirectories on my machine, I ran:
find . -name ".DS_Store"
This lists all the matching files but doesn’t do anything with them. It’s more of a dry run. If you want to then delete those files, you can run the same command but pass it the -delete flag like this:
find . -name ".DS_Store" -delete
Because these files were also still in my source control due to not listing them in the .gitignore file, I also needed to commit and push this change as well as modify my .gitignore file to ignore these pesky files.
Displaying all sub-directories and their sizes, sorted by size
I wanted to see which of my many projects were taking up the most space. To find that list, I ran the following command, which goes through all of the sub-directories from within your current directory, sorts them from largest to smallest, and prints their size in a human-readable format:
du -hs * | sort -hr
Searching for large files anywhere on your machine
After I had gone through each directory and cleaned up files I didn’t need or deleted projects I was no longer showcasing, I still had quite a bit of disk space being used up. I couldn’t easily identify where in my projects I had gone wrong or what was taking up all this space.
This next command was a life saver. This searches for large files over the specified size (50MB in my case) and prints them to the console.
sudo find / -type f -size +50M -exec ls -lh {} \;
I was surprised at what I found. The two biggest culprits were some cached files from snapd that I no longer needed and some pre-allocated storage space from MongoDB I wasn’t going to need in this development-like environment.
At this point it was as simple as running rm <filename> and rm -rf <directory name> to delete my unwanted files and directories.
Source: https://stackoverflow.com/questions/20031604/amazon-ec2-disk-full/20032145
Clean the journal logs
Es posible que su /var/log/journaldirectorio ocupe mucho espacio en disco.
¿Cómo hace para eliminar o eliminar todos estos archivos var log journalsin que el sistema se queje y se caiga?
¿Cómo saber cuánto espacio se está ocupando?
Puede preguntar el journalctlcomando directamente, usando el --disk-usageargumento:
journalctl --disk-usageLenguaje de código: Bash ( bash )
Informará algo como esto:
Journals take up 3.9G on disk.
Arreglo: Opción 1 (no ideal):
Vaya y ajuste su configuración en /etc/systemd/journald.conf, asegurándose de prestar atención a la SystemMaxUseclave; establezca esto en algo razonable, como 50Mquizás.
En este punto, puede forzar una rotación de registros emitiendo este comando:
sudo systemctl kill --kill-who=main --signal=SIGUSR2 systemd-journald.serviceLenguaje de código: Bash ( bash )
Recuerda reiniciar el systemctlproceso, así:
sudo systemctl restart systemd-journald.serviceLenguaje de código: Bash ( bash )
Corrección: Opción 2 (no recomendada):
Siempre puede ir y eliminar el /var/log/journal/**contenido del directorio ofensivo, pero esta no es la forma recomendada, ya que el diario del sistema podría estar escribiendo aquí, ¡lo que probablemente le causará problemas mayores!
Corrección: Opción 3 (¡RECOMENDADA!):
Simplemente ejecute el siguiente comando para limpiar el /var/log/journaldirectorio:
journalctl --vacuum-size=500MLenguaje de código: Bash ( bash )
Esto eliminará los archivos de registro antiguos hasta que el directorio alcance el tamaño de umbral estipulado, en nuestro caso, 500M.
¡Realmente es así de fácil borrar o limpiar su diario de registro de var!
Free space from delete a massive log files “/var/log/ folder”
find /var/log -type f -name “*.gz” -delete
find /var/log -type f -name “*.log.*” -delete
find /var/log/ -type f -name “*.1” -delete
Clear contents of all the files (*.log) using find command
To clear everything find /var/application-logs -type f -name "*.log" finds, use this:
find /var/application-logs -type f -name "*.log" -exec tee {} \; </dev/null
If your version of find supports it, use + instead of \; to use a single run of tee for all of the files. Alternately, if a shell glob is sufficient:
tee /var/application-logs/*.log </dev/null
Example: nano clean_logs_files.sh
#!/bin/bash
#
find /var/log -type f -name “*.gz” -delete
find /var/log -type f -name “*.log.*” -delete
#empty logs text into *.log
find /var/log/ -type f -name “*.log” -exec tee {} \; </dev/null
#empty content in specify & custom files
tee /var/log/dmesg </dev/null
tee /var/log/firewalld </dev/null
tee /var/log/lastlog </dev/null
tee /var/log/mail.err </dev/null
tee /var/log/syslog </dev/null
admin