GNU/Linux >> Linux の 問題 >  >> Linux

ディレクトリ内のファイルとサブディレクトリの数を数える方法

こんにちは皆さん、この記事では、ツリーコマンドを使用してディレクトリ内のファイルとサブディレクトリの数をカウントする方法を紹介します。

ツリーコマンドでファイルとディレクトリの数を数える

  • ツリー 以下に示すように、引数が指定されていない場合でもコマンドは機能します。
tree -iLf 1
  1. 「-i」 :ツリーがインデント行を印刷できるようにします。
  2. 「-L」レベルを指定します 深さの 表示されるディレクトリツリーの例。上記の場合は1
  3. 「-f」 :ツリーにすべてのファイルのフルパスプレフィックスを出力させます。
  • 深度レベル2の引数なしで同じ情報を表示する場合:
tree -iLf 2
  • 同じ情報を表示したいが、指定したディレクトリについても表示したい場合。

例: / etc / systemd /

tree -iLf 1 /etc/systemd/

サンプル出力

[root@rhel-pc ~]# tree -iLf 1 /etc/systemd/
/etc/systemd
/etc/systemd/coredump.conf
/etc/systemd/journald.conf
/etc/systemd/logind.conf
/etc/systemd/resolved.conf
/etc/systemd/system
/etc/systemd/system.conf
/etc/systemd/user
/etc/systemd/user.conf

2 directories, 6 files
[root@rhel-pc ~]# 
  • 前のディレクトリの情報を表示するには、深度レベル2を使用します
tree -iLf 2 /etc/systemd/

出力例:

[root@rhel-pc ~]# tree -iLf 2 /etc/systemd/
/etc/systemd
/etc/systemd/coredump.conf
/etc/systemd/journald.conf
/etc/systemd/logind.conf
/etc/systemd/resolved.conf
/etc/systemd/system
/etc/systemd/system/basic.target.wants
/etc/systemd/system/bluetooth.target.wants
/etc/systemd/system/dbus-org.bluez.service -> /usr/lib/systemd/system/bluetooth.service
/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
/etc/systemd/system/dbus-org.freedesktop.Avahi.service -> /usr/lib/systemd/system/avahi-daemon.service
/etc/systemd/system/dbus-org.freedesktop.ModemManager1.service -> /usr/lib/systemd/system/ModemManager.service
/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
/etc/systemd/system/dbus-org.freedesktop.timedate1.service -> /usr/lib/systemd/system/timedatex.service
/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target
/etc/systemd/system/default.target.wants
/etc/systemd/system/dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants
/etc/systemd/system/display-manager.service -> /usr/lib/systemd/system/gdm.service
/etc/systemd/system/getty.target.wants
/etc/systemd/system/graphical.target.wants
/etc/systemd/system/local-fs.target.wants
/etc/systemd/system/multi-user.target.wants
/etc/systemd/system/network-online.target.wants
/etc/systemd/system/printer.target.wants
/etc/systemd/system/remote-fs.target.wants
/etc/systemd/system/snap.remmina.ssh-agent.service
/etc/systemd/system/sockets.target.wants
/etc/systemd/system/sysinit.target.wants
/etc/systemd/system/syslog.service -> /usr/lib/systemd/system/rsyslog.service
/etc/systemd/system/sysstat.service.wants
/etc/systemd/system/systemd-timedated.service -> /dev/null
/etc/systemd/system/timers.target.wants
/etc/systemd/system/var-lib-snapd-snap-bare-5.mount
/etc/systemd/system/var-lib-snapd-snap-core-11993.mount
/etc/systemd/system/var-lib-snapd-snap-core18-2284.mount
/etc/systemd/system/var-lib-snapd-snap-core20-1270.mount
/etc/systemd/system/var-lib-snapd-snap-discord-131.mount
/etc/systemd/system/var-lib-snapd-snap-gnome\x2d3\x2d28\x2d1804-161.mount
/etc/systemd/system/var-lib-snapd-snap-gnome\x2d3\x2d38\x2d2004-87.mount
/etc/systemd/system/var-lib-snapd-snap-gtk\x2dcommon\x2dthemes-1519.mount
/etc/systemd/system/var-lib-snapd-snap-remmina-5130.mount
/etc/systemd/system/var-lib-snapd-snap-snapd-14295.mount
/etc/systemd/system/var-lib-snapd-snap-spotify-56.mount
/etc/systemd/system/var-lib-snapd-snap-telegram\x2ddesktop-3544.mount
/etc/systemd/system/vmtoolsd.service.requires
/etc/systemd/system.conf
/etc/systemd/user
/etc/systemd/user/dbus-org.bluez.obex.service -> /usr/lib/systemd/user/obex.service
/etc/systemd/user/default.target.wants
/etc/systemd/user/sockets.target.wants
/etc/systemd/user.conf

20 directories, 30 files
[root@rhel-pc ~]# 

上記の出力からわかるように、すべてのファイルとサブディレクトリを一覧表示した後、ツリー 指定したディレクトリ内のディレクトリとファイルの総数が表示されます。深度レベルが異なる場合、値は同じになりません。

結論

それだけです…..

この記事では、ディレクトリ内のファイルとサブディレクトリの数をカウントする方法を説明しました。

ターミナルツール


Linux
  1. ディレクトリ内のファイル数を数える最良の方法は何ですか?

  2. Linux でファイルの行数を数える方法 (wc および nl コマンドの例)

  3. 各ディレクトリ内のファイル数を数える方法は?

  1. 見つかったファイルの総行数を数えるには?

  2. 特定のディレクトリ内に作成されたファイルのグループと権限を強制するにはどうすればよいですか?

  3. ディレクトリ内のすべてのファイル (ファイルのクラス) へのシンボリック リンクを作成する方法は?

  1. 特定のディレクトリ内のファイルとサブディレクトリの数をカウントする方法

  2. コマンドラインを使用してディレクトリ内のファイルの数を取得するにはどうすればよいですか?

  3. ディレクトリ内のファイルの数を数え、その数が5を超えた場合に最も古いものを削除するにはどうすればよいですか?