Useful Linux Commands for Database Housekeeping Tasks

LINUX_www.basisguru.com

In today’s article we will see some of the useful Linux commands for repetitive housekeeping work in database systems like:

  • Log/Backup files deletion older than X days,
  • Listing files by size for cleanup purpose,
  • Counting the files older than X days etc.
  • Deleting files older than X minutes/hours.

Let us discuss each case in detail:

List Top X Files By Size :

We can display top files sorted by size using below command :

du -a |sort -n -r |head -n 10

here,

  • du: This command in Linux denotes the ‘Disk Usage’ i.e. amount of disk space used by particular files or directories.
  • a : This option will provide the disk space usage of individual file within the directory.
  • sort: This command is used to sort files /records in a particular order specified by further options.
  • -n: It will sort the files numerically.
  • -r: This option used in sort command will sort files in reverse order i.e. descending order by default.
  • head -n : This will print the top N lines,in our case first 10 results.

Example:

bash-3.2$ du -a |sort -n -r |head -n 10
8200    .
2584    ./Backup/XI7_BO_SLLPNFFFE_10.0.tpz
2588    ./Backup
2580    ./XI7_11_SAPBO_SLE_10.0_31_00.mf
2572    ./SA10031_0-10005013.mf
212     ./XI6_ELSTER_1.1_01_12.tpz
208     ./ETER2101_12-10004121.sql
20      ./XISFTP_ADAPTER_1.0_05_00.tpz
20      ./SFPTER1004_1-10011814.ZIP
16      ./SLE12123_0-10082636.sql

Counting Files Older Than X Days:

Command:

find -type f -mtime +n |wc -l
  • find: As name denotes, it is used to find the files and directories and along with other options can be used to perform relevant activities.
  • -type f : specifies that the input type is file.
  • -mtime +n: It is a property of file which tells the last time file was modified in n days. (in our case lets assume n=1).
    find uses mtime option to identify files based on when they were modified.
  • wc -l : counts number of lines .

Output:

bash-3.2$ find -type f -mtime +1 |wc -l
7

Delete Files Older Than X Days:

Command:

find -type f -mtime +n -exec rm -f {} \
  • -exec: As name denotes, it will execute command from bash itself i.e it will not create a new process and will return output to the calling process.
  • rm -f : removes file forcefully ,will not ask for the confirmation even if file is write protected.
  • {} \ : In above example, this will remove the every file ‘FOUND’ by find command forcefully.
  • +n: denotes number of days

Delete Files Older Than X Hours:

Command:

find -type f -mmin +n -exec rm -f {} \

This command will delete files found by find command forcefully which are older than +n minutes.

Hope you find above Linux commands useful for database admin related activities.