Unless you provide a safe limitation (ie: "Only files in /mnt/volatile"), it's not a good idea to completely automate the removal of files. You can look into tmpwatch
/tmpreaper
on how Linux and different distributions create temporary files and directories, to hook into, or take advantage of the existing setup.
For example, if you can put unimportant files into /tmp/
, they will be removed at reboot, and automatically by the system, over time, if space is needed.
GUI
Depending on your distribution and setup, you can try using baobab
aka Disk Usage Analyzer.
baobab
allows you to look into where data is located, either by disk, partition or directory. The visual nature makes it easier to investigate and find removable data than CLI solutions.
CLI
If you're interested in using the CLI, you can take a few approaches:
- Use
find
to find files larger than a certain size.
- Use
du
to look at files/directories.
-c
for total
-d
for depth
path/to/inspect/*
Trailing *
for directories
| sort -n
Put largest files/directories at bottom
| tail -n 100
to limit the results
To look at a path and find the '20 'largest directories:
du /path/to/inspect/* | sort -nr | tail -n 20
You can drill down into directories you find:
$ du /path/to/inspect/* | sort -nr | tail -n 20
...
/path/to/inspect/LargestDirectoryFound
$ du /path/to/inspect/LargestDirectoryFound/* | sort -n | tail -n 20
Once you determine how you like to find the files/directories, you will want to turn it into a bash function for easier use:
$ # Put this function in .bashrc after testing and tuning
$ function du-search(){
# $@ is everything after the command
du $@* | sort -n | tail -n 20
}
$ du-search /var/
...
$ du-search /var/INTERESTING_PATH/
By looking into du
and the available options, you can show fewer duplicates and 'navigate' more quickly:
$ function du-search(){
# $@ is everything after the command
du -S -d0 $@* | sort -n | tail -n 20
}
$ du-search /var/
...
/var/log
$ du-search /var/log/
...
1820814528 /var/log/some_huge_logfile.log
man df
. You can call that from whatever program you're writing.df
from Python and parse the result. The SD Card has nothing to do with it nor does the wisdom of (or lack thereof) automating deletion. The OP has told us nothing about which files will be freed nor how / if it's know that those files are ok to go.df
becausedf
is available in BASH, which can also be used programatically.