Software & AppsOperating SystemLinux

How To Show Hidden Files and Folders in Disk Usage (du) Command on Ubuntu

Ubuntu 17

In this article, we will delve into the topic of how to show hidden files and folders when using the Disk Usage (du) command on Ubuntu. The du command is a standard tool in Unix-like operating systems that is used to estimate file and directory space usage.

Quick Answer

To show hidden files and folders in the du command on Ubuntu, you can use glob patterns with the command du -sch .[!.]* * | sort -h to include hidden files and directories in the current directory. Alternatively, you can use the find command in combination with du to display hidden files and folders, using the command find ./ -maxdepth 1 -name '.*' -exec du -hs {} \;.

Understanding the du Command

The du command, short for Disk Usage, is used to estimate and report the disk space usage of files and directories. The command traverses the file system to calculate the total size of all files and directories specified in the command line.

The syntax of the du command is du [options] [files|directories]. The options can be used to modify the behavior of the command, while the files or directories specify the paths to estimate the disk usage for.

Displaying Hidden Files and Folders

By default, the du command does not include hidden files and folders in its output. In Unix-like operating systems, files and directories that start with a dot (.) are considered hidden.

Using the du Command with Glob Patterns

Glob patterns are a way of selecting multiple files based on wildcard characters. In the context of the du command, we can use glob patterns to include hidden files and folders.

  • To include hidden files and directories in the current directory, use the command du -sch .[!.]* * | sort -h. This command first lists all files starting with a single dot (hidden files) and then lists all other files. The -s option is for summarizing the disk usage of each argument and the -c option is for producing a grand total. The -h option makes the output sizes human readable.
  • Another option is du -sch * .*, which works in shells like zsh but may not expand properly in bash.
  • If you want to include hidden files inside directories as well, you can use du -sch .[^.]* *. This command lists all hidden files excluding . and ...

Using the find Command

The find command is a powerful tool for searching files and directories based on different criteria. We can use it in combination with the du command to display hidden files and folders.

  • To find and display hidden files in the current directory, use the command find ./ -maxdepth 1 -name '.*' -exec du -hs {} \;. The -maxdepth 1 option limits the search to the current directory. The -name '.*' option matches all hidden files, and the -exec du -hs {} \; option executes the du command for each file.
  • If you only want to include hidden directories, you can modify the command to find ./ -maxdepth 1 -type d -name '.*' -exec du -hs {} \;. The -type d option limits the search to directories.
  • To search for hidden files and directories recursively, you can remove the -maxdepth 1 option from the command.

Using Shell Options

If you are using bash, you can enable the dotglob shell option to include hidden files when using glob patterns. For example, (shopt -s dotglob; du -shc *) enables dotglob temporarily for the du command.

Conclusion

In this article, we have explored different ways to show hidden files and folders when using the du command on Ubuntu. Depending on your specific needs and the shell you are using, you can choose the method that suits you best. Always remember to check the manual pages (man du or man find) for more information about the commands and their options.

How can I show hidden files and folders using the `du` command on Ubuntu?

To show hidden files and folders using the du command on Ubuntu, you can use glob patterns or the find command. With glob patterns, you can use the command du -sch .[!.]* * | sort -h to include hidden files and directories in the current directory. Another option is du -sch * .*, which works in shells like zsh. If you want to include hidden files inside directories as well, you can use du -sch .[^.]* *. Alternatively, you can use the find command with a command like find ./ -maxdepth 1 -name '.*' -exec du -hs {} \; to find and display hidden files in the current directory.

What are glob patterns?

Glob patterns are a way of selecting multiple files based on wildcard characters. In the context of the du command, glob patterns can be used to include hidden files and folders. The * wildcard character matches any number of characters, while [!.] matches any character except a dot. By combining these patterns, you can include hidden files and directories when using the du command.

Can I show hidden files and folders recursively using the `du` command?

Yes, you can show hidden files and folders recursively using the du command. To do this, you can remove the -maxdepth 1 option from the find command. For example, the command find ./ -name '.*' -exec du -hs {} \; will search for hidden files and folders recursively in the current directory and display their disk usage.

How can I enable the `dotglob` shell option in `bash`?

To enable the dotglob shell option in bash, you can use the command (shopt -s dotglob; du -shc *). The dotglob option allows glob patterns to match hidden files and directories. By enclosing the shopt -s dotglob command in parentheses and separating it from the du command with a semicolon, you can enable dotglob temporarily for the du command.

Leave a Comment

Your email address will not be published. Required fields are marked *