Table of Contents

Linux Commands#File, Directory

tree - How to use tree command in Linux with examples



man tree

NAME
       tree - list contents of directories in a tree-like format.

OPTIONS
      -a     All files are printed.  By default tree does not print hidden files
             (those beginning with a dot ‘.’).
      -p     Print the protections for each file (as per ls -l).
      -D     Print the date of the last modification time for the file listed.
      -u     Print the username, or UID # if no username is available, of the file.
      -g     Print the group name, or GID # if no group name is available, of the file.
      -d     List directories only.

       -I pattern
              Do not list those files that match the wild-card pattern.


Example

# tree -Dpuga /etc |more
/etc
|-- [-rw------- root     root     Apr 23  2014]  .pwd.lock
|-- [-rw-r--r-- root     root     Jul 22  2011]  DIR_COLORS
|-- [-rw-r--r-- root     root     Jul 22  2011]  DIR_COLORS.xterm
|-- [drwxr-xr-x root     root     Apr 23  2014]  NetworkManager
|   |-- [drwxr-xr-x root     root     Jul 22  2011]  VPN
|   |-- [drwxr-xr-x root     root     Jul 22  2011]  dispatcher.d
|   `-- [-rw-r--r-- root     root     Jul 22  2011]  nm-system-settings.conf
|-- [drwxr-xr-x root     root     Apr 23  2014]  X11
|   |-- [-rw-r--r-- root     root     May 24  2008]  Xmodmap
|   |-- [-rw-r--r-- root     root     May 24  2008]  Xresources
|   |-- [drwxr-xr-x root     root     May 11  2011]  applnk
|   |-- [drwxr-xr-x root     root     Apr 23  2014]  fs
|   |   `-- [-rw-r--r-- root     root     Apr 23  2014]  config
|   |-- [-rwxr-xr-x root     root     Aug 19  2011]  prefdm
|   |-- [drwxr-xr-x root     root     May 11  2011]  serverconfig
|   |-- [drwxr-xr-x root     root     May 11  2011]  sysconfig
|   |-- [drwxr-xr-x root     root     Apr 23  2014]  twm
|   |   `-- [-rw-r--r-- root     root     Jan  7  2007]  system.twmrc
|   |-- [drwxr-xr-x root     root     Apr 23  2014]  xinit
(abbr)
$ tree -a -I '.git'
.
|-- README.md
|-- bin
|   `-- .gitkeep
|-- conf
|   |-- httpd
|   |-- named
|   `-- os
|-- deb
|   `-- .gitkeep
|-- fabfile
|   |-- __init__.py
|   |-- auth.py
|   |-- check.py
|   |-- get.py
|   |-- httpd.py




Ref.

Count the number of files

find /usr/local/apache2/htdocs  -type f | wc -l


Check permissions before and after work

Sample1

# rsync  -avz --delete /etc/ /etc.bk
# tree -Dpuga /etc/ > /tmp/tree1
# tree -Dpuga /etc.bk/ >  /tmp/tree2
# diff /tmp/tree1 /tmp/tree2

Sample2

# tree -Dpuga /usr/local/apache2/htdocs/ > /tmp/tree.out
# tree -Dpuga /tmp/htdocs/ | diff - /tmp/tree.out


Alternative commands when the tree command is not available

$ find .
.
./aliases.db
./w3m
./w3m/config
./w3m/mailcap
./rc5.d
./rc5.d/S01console-setup.sh
./rc5.d/S01fcgiwrap
./rc5.d/S01ntp
./rc5.d/S01postfix
./rc5.d/S01ssh
./rc5.d/S01irqbalance
./rc5.d/S01rsync
./rc5.d/S01multipath-tools
./rc5.d/S01unattended-upgrades
./rc5.d/S01nginx
./rc5.d/S01cron
./rc5.d/S01php7.4-fpm
./rc5.d/S01sysstat
./rc5.d/S01qemu-guest-agent
./rc5.d/S01dbus
./rc5.d/S01acpid
./rc5.d/S01apport
./rc5.d/S01plymouth
./rc5.d/S01lvm2-lvmpolld
./rc5.d/S01open-vm-tools
./rc5.d/S01grub-common
./rc5.d/S01rsyslog
./rc5.d/S01uuidd
./rc5.d/S01atd
./crypttab
./pam.d
./pam.d/su
./pam.d/chsh
./pam.d/chfn
./pam.d/sudo
./pam.d/cron
./pam.d/atd
./pam.d/common-auth
./pam.d/systemd-user
./pam.d/su-l
(abbr)
$ find . -exec ls -lh {} \;
total 976K
drwxr-xr-x 3 root root      4.0K Aug 31  2020 acpi
-rw-r--r-- 1 root root      3.0K Aug  1  2020 adduser.conf
-rw-r--r-- 1 root root        51 Feb 13  2021 aliases
-rw-r--r-- 1 root root       12K Feb 13  2021 aliases.db
drwxr-xr-x 2 root root      4.0K Feb  8 01:37 alternatives
drwxr-xr-x 3 root root      4.0K Feb 13  2021 apache2
drwxr-xr-x 3 root root      4.0K Aug  1  2020 apparmor
drwxr-xr-x 7 root root      4.0K Jun  1  2023 apparmor.d
drwxr-xr-x 3 root root      4.0K Apr 15  2023 apport
drwxr-xr-x 7 root root      4.0K Aug 27  2020 apt
-rw-r----- 1 root daemon     144 Nov 13  2018 at.deny
-rw-r--r-- 1 root root      2.3K Feb 25  2020 bash.bashrc
-rw-r--r-- 1 root root        45 Jan 26  2020 bash_completion
drwxr-xr-x 2 root root      4.0K May  2  2023 bash_completion.d
-rw-r--r-- 1 root root       367 Apr 15  2020 bindresvport.blacklist
drwxr-xr-x 2 root root      4.0K Apr 22  2020 binfmt.d
drwxr-xr-x 2 root root      4.0K Aug  1  2020 byobu
drwxr-xr-x 3 root root      4.0K Aug  1  2020 ca-certificates
-rw-r--r-- 1 root root      7.4K May 25  2023 ca-certificates.conf
drwxr-xr-x 2 root root      4.0K Aug  1  2020 calendar
drwxr-xr-x 5 root root      4.0K May 24  2023 cloud
drwxr-xr-x 2 root root      4.0K Feb 13  2021 console-setup
drwxr-xr-x 2 root root      4.0K Jun  8  2023 cron.d
drwxr-xr-x 2 root root      4.0K Jun  8  2023 cron.daily
drwxr-xr-x 2 root root      4.0K Aug  1  2020 cron.hourly
drwxr-xr-x 2 root root      4.0K Aug  1  2020 cron.monthly
-rw-r--r-- 1 root root      1.1K Feb 14  2020 crontab
drwxr-xr-x 2 root root      4.0K Feb 16  2021 cron.weekly
drwxr-xr-x 2 root root      4.0K Feb 16  2022 cryptsetup-initramfs
-rw-r--r-- 1 root root        54 Aug  1  2020 crypttab
drwxr-xr-x 4 root root      4.0K Aug  1  2020 dbus-1
drwxr-xr-x 3 root root      4.0K Aug  1  2020 dconf
-rw-r--r-- 1 root root      2.9K Aug  3  2019 debconf.conf
-rw-r--r-- 1 root root        13 Dec  5  2019 debian_version
drwxr-xr-x 3 root root      4.0K Apr 19 06:13 default
-rw-r--r-- 1 root root       604 Sep 16  2018 deluser.conf
drwxr-xr-x 2 root root      4.0K Aug  1  2020 depmod.d
drwxr-xr-x 4 root root      4.0K Oct  7  2022 dhcp
drwxr-xr-x 4 root root      4.0K May 28  2022 dpkg
(abbr)