15 Most Useful Linux Commands For File System Maintenance
15 Most Useful Linux Commands For File System Maintenance
15 Most Useful Linux Commands For File System Maintenance
system maintenance
01/10/2014 Linux, Tips & Tricks 4 Comments
One of the most common and tedious tasks of a sysadmin is to prevent file systems become
completely full, because when a server runs out of space the consequences are unpredictable.
Depending on how you structured the root file system and if it is divided into different
partitions or volumes, those consequences will be more or less severe, but in any case
undesirable.
In any case it is always better to be safe than sorry, so use tools that perform automatic log
rotation as logrotate and custom scripts to monitor and conduct periodic emptying actions to
prevent file systems to get full. However, still using these prevention methods it is for sure it will
be many times when you will have to act manually to troubleshoot problems.
Below I gather a collection of Linux commands that I find most useful on my day to day work
freeing up disk space and keeping my file systems in optimal health from command line.
Tamao Usados
3,9G
842M
2,0G
4,0K
791M
956K
5,0M
0
2,0G
72M
118M
91M
31G
6,8G
23G
12G
9,6G
1,5G
2,9G
62M
102G
11G
/dev/sda1
230G
93G
125G
43% /var/datos
Tamao Usados
31G
6,8G
To display the file systems in order of occupation and thus know which ones are fuller:
$ df -h | awk '{print $5 " " $6}' | sort -n | tail -5
22% /
23% /home
43% /var/datos
52% /usr
77% /boot
The file size must be displayed in Kilobytes (parameter -k). You must use this parameter instead
of -h otherwise sort -n command will not sort the list the way you expect.
Its important to limit the number of files you want to display with tail -X, where X is that
number, because if the directory at issue has hundreds or thousands of files, the command output
may take too much input/output overhead to your terminal and slow down the command
response too much, especially if you are connecting remotely via telnet or ssh and the connection
is not very fast.
adm
root
root
adm
118616
149012
160128
499400
If -r parameter is removed files listed would be the smaller instead of the larger ones.
root
utmp
adm
adm
adm
4,2K
11K
13K
13K
1000
sep
sep
sep
sep
sep
30
30
30
30
30
12:27
13:03
13:03
13:03
13:03
.
wtmp
syslog
kern.log
auth.log
The first command locates files only, the second one also removes them.
Or:
$ find . -type f -empty -ls
To know the number of free inodes available in a file system use the df -i command.
Last command compresses all log files into a single file with .tar.gz extension and todays date to
make it easier to locate them in the future. Lets see how to save space, passing in this example
from 468 MB to 35 MB:
# du -ch /var/log/*.log | grep total
468M
total
# ls -lh var_log.20140930.tar.gz
-rw-r--r-- 1 root root 35M sep 30 13:36 var_log.20140930.tar.gz
After that we can proceed and empty all log files as in section #3.
find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1
find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --allrepeated=separate | cut -f3-100 -d ' ' | tr '\n.' '\t.' | sed 's/\t\t/\n/g' |
cut -f2-100 | tr '\t' '\n' | perl -i -pe 's/([ (){}-])/\\$1/g' | perl -i -pe
's/'\''/\\'\''/g' | xargs -pr rm -v