Simple TRICKs to check size of hidden files in Linux


Tips and Tricks, Linux

Author: Omer Cakmak
Reviewer: Deepak Prasad

Hidden files are often used to store user preferences or maintain the state of an application and are often created indirectly using various utilities. They are not a security mechanism as there are no access and authorization restrictions.

They exist in every operating system.  In Linux operating systems (Debian, Ubuntu, Centos etc) hidden files are mostly created by the operating system. The names of hidden files start with point (".")

 

How to list only hidden files?

To list only hidden files in your current directory, the ls command should be used as follows:

foc@fedora:~$ ls -ld .?* 
drwxr-xr-x.  8 root root  4096 Feb 23 13:35  ..
-rw-------.  1 foc  foc  41737 Aug 18 22:05  .bash_history
-rw-r--r--.  1 foc  foc    220 Feb 12  2021  .bash_logout
-rw-r--r--.  1 foc  foc   4144 Jul 31 20:53  .bashrc
drwxr-xr-x. 30 foc  foc   4096 Jul 29 01:30  .cache
drwxr-xr-x.  3 foc  foc   4096 Jul 31 20:53  .cargo
drwxr-xr-x.  2 foc  foc   4096 Jun  2  2021  .conda
drwxr-xr-x. 28 foc  foc   4096 Aug 16 17:47  .config

Parameters used in the ls command:

  • -l : use a long listing format
  • -d, --directory: list directory entries instead of contents, and do not dereference symbolic links
  • .?* : will only state hidden files

 

How to check size of hidden files?

Let's show two methods to check the size of the hidden file.

 

Method-1: Using "ls" Command

Adding the "h" parameter to the ls command will list the size information for each file/directory in a more readable way:

foc@fedora:~$ ls -lhd .?* 
drwxr-xr-x.  8 root root 4.0K Feb 23 13:35  ..
-rw-------.  1 foc  foc   41K Aug 18 22:05  .bash_history
-rw-r--r--.  1 foc  foc   220 Feb 12  2021  .bash_logout
-rw-r--r--.  1 foc  foc  4.1K Jul 31 20:53  .bashrc
drwxr-xr-x. 30 foc  foc  4.0K Jul 29 01:30  .cache
drwxr-xr-x.  3 foc  foc  4.0K Jul 31 20:53  .cargo
drwxr-xr-x.  2 foc  foc  4.0K Jun  2  2021  .conda
drwxr-xr-x. 28 foc  foc  4.0K Aug 16 17:47  .config

Here,

  • -h, --human-readable with -l and -s, print sizes like 1K 234M 2G etc.

If you run the ls command as follows, it will display the size of the files and subdirectories under the directories, along with the size of that file if it is just a file:

foc@fedora:~$ ls -lSh .?* 

.cache:
total 112K
drwxr-xr-x.   2 foc foc 4.0K Apr 14 22:40 abrt
drwxr-xr-x.   2 foc foc 4.0K Aug 18 22:05 appstream
drwx------.   3 foc foc 4.0K Apr 21 01:14 BraveSoftware
drwx------.   3 foc foc 4.0K Jul 17 01:01 chromium
drwx------.   8 foc foc 4.0K Apr 14 22:40 evolution
drwxr-xr-x.   3 foc foc 4.0K Apr 14 22:40 flatpak
drwxr-xr-x.   2 foc foc 4.0K Jul 26 19:50 fontconfig
drwxr-xr-x.   3 foc foc 4.0K Jul 29 01:30 git
drwxr-xr-x.   2 foc foc 4.0K Apr 21 01:27 gnome-boxes
drwxr-xr-x.   2 foc foc 4.0K Jul 28 21:19 gnome-calculator
drwx------.   3 foc foc 4.0K Apr 14 22:40 gnome-desktop-thumbnailer
drwx------.   2 foc foc 4.0K Jul 20 02:00 gnome-screenshot
drwxr-xr-x.  10 foc foc 4.0K Apr 21 01:09 gnome-software
drwxr-xr-x.   2 foc foc 4.0K Jul 18 09:07 gstreamer-1.0
drwx------.   2 foc foc 4.0K Apr 14 22:40 ibus
drwxr-xr-x.   3 foc foc 4.0K Apr 21 01:27 libvirt
drwxr-xr-x. 193 foc foc 4.0K Jul 31 21:19 mesa_shader_cache
drwx------.   3 foc foc 4.0K Apr 20 23:27 mozilla
drwx------.   2 foc foc 4.0K May 17 11:16 obexd
drwx------.   3 foc foc 4.0K Jun  7 13:02 rhythmbox
drwx------.   2 foc foc 4.0K Apr 14 22:43 samba
drwx------.   5 foc foc 4.0K May 23 10:38 thumbnails
drwxr-x--x.   3 foc foc 4.0K Aug 15 10:22 virt-manager

.dbus:
total 4.0K
drwx------. 2 foc foc 4.0K Feb 12  2021 session-bus

 

Method-1: Using "du" Command

The du command shows the sizes of hidden files in the directory you are using below:

foc@fedora:~$ du -hs .[^.]*
44K	.bash_history
4.0K	.bash_logout
8.0K	.bashrc
1.4G	.cache
16M	.cargo
8.0K	.conda
642M	.config

The du command parameters:

  • -h, --human-readable: print sizes in human readable format (e.g., 1K 234M 2G)
  • -s, --summarize: display only a total for each argument

To check the size of a specific file or directory:

foc@fedora:~$ du -hs .gconf/
4.0K	.gconf/

 

Summary

In this article, we tried to show you how to find hidden files and check their size. You can get help from du command manual page to show file/directory sizes with different properties:

foc@fedora:~$ man du

        -B, --block-size=SIZE
              scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576  bytes;  see
              SIZE format below

       -b, --bytes
              equivalent to '--apparent-size --block-size=1'

       -c, --total
              produce a grand total

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

       -k     like --block-size=1K

       -l, --count-links
              count sizes many times if hard linked

       -m     like --block-size=1M

       -S, --separate-dirs
              for directories do not include size of subdirectories

       --si   like -h, but use powers of 1000 not 1024

       -s, --summarize
              display only a total for each argument

       -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

 

References

Stackoverflow - How to get size of hidden files with "du"

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment