• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to find all the sparse files in Linux

By admin

Sparse files are files that have large amounts of space preallocated to them, without occupying the entire amount from the filesystem. They are useful for reducing the amount of time and disk space involved in creating loop filesystems or large disk images for virtualized guests, among other things. The term “sparse file” is used to mean one containing “holes”; it is easy to recognize one on a running system because its disk usage is less than its size. We can see this behavior with /var/log/lastlog file.

# ls -lh /var/log/lastlog
-rw-r--r--. 1 root root 286K Dec  3 04:50 /var/log/lastlog
# du -sh /var/log/lastlog
12K     /var/log/lastlog

Finding sparse files

Now the above process can identify the sparse files in the system, but it becomes cumbersome to find all the sparse files in a filesystem or directory, especially when they are many. Don’t worry, there is an option in find command which helps us to find all the sparse files in one go. Let’s see an example below.

1. Use find command with “%S” to find each file’s sparseness.

# find /var/log -type f -printf "%S\t%p\n"
# find /var/log -type f -printf "%S\t%p\n"
1       /var/log/tallylog
1.00095 /var/log/audit/audit.log.1
0.0419982       /var/log/lastlog
....

2. Value displayed in the leftmost column is (BLOCK-SIZE*st_blocks / st_size) which is normally less than 1.0 in case of sparse file.

3. If you want to find all sparse files on the system, we can filter out all the files with the leftmost column values less than 1.

# find / -type f -printf "%S\t%p\n" | gawk '$1 < 1.0 {print}'
0.0139994       /var/log/lastlog
0.959592        /usr/lib/locale/locale-archive
...

Filed Under: Linux

Some more articles you might also be interested in …

  1. Cannot Increase “nproc” Value More Than 1024 in CentOS/RHEL 6
  2. How To Add Standard Linux Users To Manage Print Jobs And Services in CentOS/RHEL
  3. How to Change Default Permission of /var/log/messages in CentOS/RHEL
  4. Installing CentOS / RHEL 7 (step by step with screen shots)
  5. When to use rescan-scsi-bus.sh -i (LIP flag) in CentOS/RHEL
  6. RPM command examples to query, install, remove and upgrade packages
  7. How to scan newly Assigned LUNs in Multipathd under CentOS / RHEL
  8. How to recover from a corrupt RPM database (rebuilding an RPM database)
  9. CentOS / RHEL 6 : How to completely remove device mapper multipath (dm-multipath)
  10. DNS configuration file /etc/named.conf explained

You May Also Like

Primary Sidebar

Recent Posts

  • How to Disable IPv6 on Ubuntu 18.04 Bionic Beaver Linux
  • How to Capture More Logs in /var/log/dmesg for CentOS/RHEL
  • Unable to Start RDMA Services on CentOS/RHEL 7
  • How to rename a KVM VM with virsh
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary