How to show the disk usage of each subdirectory in Linux?
Use asterisk to get info for each directory, like this:
sudo du -hs *
It will output something like the below:
0 backup
0 bin
70M boot
0 cfg
8.0K data
0 dev
140K docs
calculate total used disk space by files older than 180 days using find
@PeterT is right. Almost all these answers invoke a command (du) for each file, which is very resource intensive and slow and unnecessary. The simplest and fastest way is this:
find . -type f -mtime +356 -printf '%s\n' | awk '{total=total+$1}END{print total/1024}'
How to gather facts about disks using Ansible
This information is automatically gathered via ansible's fact gathering mechanism.
See Variables discovered from systems: Facts.
For example:
#!/usr/bin/env ansible-playbook
- name: Lets look at some disks
hosts: localhost
become: false
gather_facts: true
tasks:
- name: Output disk information
debug:
var: hostvars[inventory_hostname].ansible_devices
If we instead use the gather_subset configuration on the setup module instead we can speed up the fact gathering and only gather information about system hardware.
We can then combine this with the python keys()
method and the jinja2 list
filter to produce your desired output.
#!/usr/bin/env ansible-playbook
- name: Lets look at some disks
hosts: localhost
become: false
gather_facts: false
tasks:
- name: Collect only facts about hardware
setup:
gather_subset:
- hardware
- name: Output disks
debug:
var: hostvars[inventory_hostname].ansible_devices.keys() | list
It is also possible to configure which facts to gather in the ansible configuration file ansible.cfg using the gather_subset
key in the [defaults]
section.
EDIT:
If you wanted to filter out various disk types the easiest way would be to use map('regex_search', '*search string*')
to extract the values you want. You can the remove the nulls via select('string')
.
For example with disks of the form sd*:
#!/usr/bin/env ansible-playbook
- name: Lets look at some disks
hosts: localhost
become: false
gather_facts: false
tasks:
- name: Collect only facts about hardware
setup:
gather_subset:
- hardware
- name: Output disks
debug:
var: hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'sd.*') | select('string') | list
Related Topics
How to Make a Cross Compiler Using Gcc
Vue Npm Run Serve Failed to Load Resource: Net::Err_Content_Length_Mismatch
Make Uses "Cc" Instead of "Arm-None-Eabi-As"
Nagios Plugin to Check Files Are Created Within X Minutes
Hide Information During Bash Debug Run
Arduino Upload Error "Stk500_Recv(): Programmer Is Not Responding" in Fedora
Error While Using Make to Compile Glibc-2.11.1 for Linux from Scratch
Parallel Processes: Appending Outputs to an Array in a Bash Script
How to Get a Linux Coredump That Only Contains Callstack, Threads, and Local Variables
Code for Wait_Event_Interruptible
How to Use Named Mutex at Linux