How To Clean Up Logs On Linux
I withal run an EC2 example on AWS that I configured years agone to host some of my side projects. It's using a Linux AMI (Amazon machine epitome) that is woefully out of date and too wonderfully nether-performant when traffic on my site is heavy.
Recently my EBS (elastic block shop) volume that I accept attached to my EC2 instance became full. Not wanting to shell out a piffling 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.
I'chiliad past no means an expert in Linux. Hence, I probably googled 20 unlike things searching for solutions that could help me. Stack Overflow, blog posts, and help forums came to my rescue.
Now I'm paying it forrard and sharing some commands that helped me.
Note: These commands work for Ubuntu/Debian Linux distributions. Mileage may vary for other flavors of Linux.
Checking disk usage and free disk infinite
To check your machine's deejay usage and meet how much gratis space is left, you lot can run the following command in your concluding:
df -h
This lists all the filesystems mounted on your auto, 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% total!
Source: https://world wide web.howtogeek.com/409611/how-to-view-free-disk-space-and-disk-usage-from-the-linux-last/
Auto-removing unnecessary dependencies
When installing a dependency on a Linux machine, it'due south common to apply APT, the Avant-garde Package Tool. Over time, it'due south possible that you may have packages installed that you no longer need. To auto-remove whatsoever unnecessary dependencies you may have installed, you can run:
sudo apt-get autoremove
Source: https://askubuntu.com/questions/527410/what-is-the-advantage-of-using-sudo-apt-get-autoremove-over-a-cleaner-app
Cleaning up cached packages
It's too helpful to clean upwards the packet files stored in the /var/cache
directory on your auto. You can do so past running this command:
sudo apt-become clean
Source: https://world wide web.networkworld.com/article/3453032/cleaning-up-with-apt-get.html
Finding and deleting files with filenames that friction match a blueprint
Back in my early on 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. Considering I failed to use this file to my advantage, I had enough 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 car, I ran:
discover . -name ".DS_Store"
This lists all the matching files but doesn't do anything with them. It's more than of a dry run. If yous want to then delete those files, you tin can run the same control but pass it the -delete
flag like this:
find . -name ".DS_Store" -delete
Because these files were too still in my source control due to not listing them in the.gitignore
file, I besides needed to commit and push this alter also as modify my.gitignore
file to ignore these pesky files.
Source: https://unix.stackexchange.com/questions/3672/how-do-i-delete-all-files-with-a-given-proper name-in-all-subdirectories
Displaying all sub-directories and their sizes, sorted by size
I wanted to encounter which of my many projects were taking up the near space. To find that listing, 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 man-readable format:
du -hs * | sort -hour
Source: https://superuser.com/questions/554319/display-each-sub-directory-size-in-a-list-format-using-one-line-control-in-bash
Listing all files and directories within a directory, sorted by size
Once I identified my largest directories, I needed to dig into which files inside those directories were taking up too much infinite.
You tin list all the files and directories within a given directory simply by running this command:
ls
Simply, you can also pass additional flags to that command to sort and format the output. You can listing all files and directories in a directory sorted by their size and with their size info shown in a human-readable format past using the post-obit:
ls -laSh
Source: https://www.tecmint.com/list-files-ordered-by-size-in-linux/
Searching for large files anywhere on your machine
After I had gone through each directory and cleaned upward files I didn't need or deleted projects I was no longer showcasing, I still had quite a flake 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 panel.
sudo find / -blazon f -size +50M -exec ls -lh {} \;
I was surprised at what I found. The 2 biggest culprits were some buried files from snapd
that I no longer needed and some pre-allocated storage infinite from MongoDB I wasn't going to need in this development-like environment.
At this betoken it was as simple equally running rm <filename>
and rm -rf <directory name>
to delete my unwanted files and directories.
Source: https://stackoverflow.com/questions/20031604/amazon-ec2-deejay-full/20032145
Good to Go!
Afterwards troubleshooting for what felt similar a few hours, I finally had my disk space usage back to a normal amount, and my EC2 instance was functioning properly again. Success!
How To Clean Up Logs On Linux,
Source: https://dev.to/thawkin3/cleaning-up-disk-space-on-a-linux-machine-from-the-command-line-nbn
Posted by: codythelint.blogspot.com
0 Response to "How To Clean Up Logs On Linux"
Post a Comment