如何列出文件夹的所有属性

如何列出文件夹的所有属性

如何列出目录中文件夹的属性。例如,我想“find ./ -type d”并同时“ll”它们。

答案1

您可以find直接使用来打印您想要查看的任何详细信息:

对于类似于的信息ll- 您可以ls -dils使用以下-ls选项显示信息:

find . -type d -ls

例如:

$ find /boot -type d -ls
3407873    4 drwxr-xr-x   3 root     root         4096 May  8 09:58 /boot
3407874    4 drwxr-xr-x   5 root     root         4096 Jul 31 03:32 /boot/grub
3408122    4 drwxr-xr-x   2 root     root         4096 Nov 27  2012 /boot/grub/fonts
3407884   12 drwxr-xr-x   2 root     root        12288 Apr 18 05:57 /boot/grub/i386-pc
3408117    4 drwxr-xr-x   2 root     root         4096 Apr 18 05:57 /boot/grub/locale

如果您想显示更多详细信息:该选项-printf采用格式字符串,允许您显示目录的任何属性。

例如,显示 inode 编号和纯文件名:

find . -type d -printf '%i %f\n'

格式字符串描述如下man find

    -printf format
           True;  print  format on the standard output, interpreting `\' escapes and `%' directives.  Field widths
           and precisions can be specified as with the `printf' C function.  Please note that many of  the  fields
           are  printed  as  %s rather than %d, and this may mean that flags don't work as you might expect.  This
           also means that the `-' flag does work (it forces fields to be left-aligned).  Unlike  -print,  -printf
           does not add a newline at the end of the string.  The escapes and directives are:

[... literal chars ...]

           A  `\'  character followed by any other character is treated as an ordinary character, so they both are
           printed.

           %%     A literal percent sign.

           %a     File's last access time in the format returned by the C `ctime' function.

           %Ak    File's last access time in the format specified by k, which is either `@' or a directive for the
                  C  `strftime'  function.   The possible values for k are listed below; some of them might not be
                  available on all systems, due to differences in `strftime' between systems.

[... many more time formats ...]

           %b     The amount of disk space used for this file in 512-byte blocks. Since disk space is allocated in
                  multiples  of  the filesystem block size this is usually greater than %s/512, but it can also be
                  smaller if the file is a sparse file.

           %c     File's last status change time in the format returned by the C `ctime' function.

           %Ck    File's last status change time in the format specified by k, which is the same as for %A.

           %d     File's depth in the directory tree; 0 means the file is a command line argument.

           %D     The device number on which the file exists (the st_dev field of struct stat), in decimal.

           %f     File's name with any leading directories removed (only the last element).

           %F     Type of the filesystem the file is on; this value can be used for -fstype.

           %g     File's group name, or numeric group ID if the group has no name.

           %G     File's numeric group ID.

           %h     Leading directories of file's name (all but the last element).  If the  file  name  contains  no
                  slashes (since it is in the current directory) the %h specifier expands to ".".

           %H     Command line argument under which file was found.

           %i     File's inode number (in decimal).

           %k     The  amount of disk space used for this file in 1K blocks. Since disk space is allocated in mul‐
                  tiples of the filesystem block size this is usually greater than %s/1024, but  it  can  also  be
                  smaller if the file is a sparse file.

           %l     Object of symbolic link (empty string if file is not a symbolic link).

           %m     File's  permission  bits (in octal).  This option uses the `traditional' numbers which most Unix
                  implementations use, but if your particular implementation uses an  unusual  ordering  of  octal
                  permissions  bits, you will see a difference between the actual value of the file's mode and the
                  output of %m.   Normally you will want to have a leading zero on this number, and  to  do  this,
                  you should use the # flag (as in, for example, `%#m').

           %M     File's  permissions  (in  symbolic  form,  as for ls).  This directive is supported in findutils
                  4.2.5 and later.

           %n     Number of hard links to file.

           %p     File's name.

           %P     File's name with the name of the command line argument under which it was found removed.

           %s     File's size in bytes.

           %S     File's sparseness.  This is calculated as (BLOCKSIZE*st_blocks / st_size).  The exact value  you
                  will get for an ordinary file of a certain length is system-dependent.  However, normally sparse
                  files will have values less than 1.0, and files which use indirect blocks may have a value which
                  is  greater  than  1.0.    The  value used for BLOCKSIZE is system-dependent, but is usually 512
                  bytes.   If the file size is zero, the value printed is undefined.  On systems which  lack  sup‐
                  port for st_blocks, a file's sparseness is assumed to be 1.0.

           %t     File's last modification time in the format returned by the C `ctime' function.

           %Tk    File's last modification time in the format specified by k, which is the same as for %A.

           %u     File's user name, or numeric user ID if the user has no name.

           %U     File's numeric user ID.

           %y     File's type (like in ls -l), U=unknown type (shouldn't happen)

           %Y     File's type (like %y), plus follow symlinks: L=loop, N=nonexistent

           A `%' character followed by any other character is discarded, but the other character is printed (don't
           rely on this, as further format characters may be introduced).  A `%' at the end of the format argument
           causes  undefined  behaviour  since there is no following character.  In some locales, it may hide your
           door keys, while in others it may remove the final page from the novel you are reading.

           The %m and %d directives support the # , 0 and + flags, but the other directives do not, even  if  they
           print  numbers.   Numeric  directives that do not support these flags include G, U, b, D, k and n.  The
           `-' format flag is supported and changes the alignment of a field from right-justified  (which  is  the
           default) to left-justified.

           See  the  UNUSUAL  FILENAMES section for information about how unusual characters in filenames are han‐
           dled.


如果您需要 find 命令的其他变体,您可以使用它来做很多事情find- 手册页很长。

例如,如果您想要忽略隐藏目录(名称以 开头的目录).,并且仅显示第一级目录,则可以使用此命令:

find . -maxdepth 1 -type d -name '[^.]*' -printf '%i %p\n'

$ find /boot -maxdepth 1 -type d -name '[^.]*' -printf '%i %p\n' 
3407873 /boot
3407874 /boot/grub

(查看命令中的两个新部分 - 两者都很有用。)

答案2

如果您有“stat”命令,它可以很好地完成此操作。如果没有,您可以安装名为“stat”的包。

 [myuser@server bin]$ stat /var/log
   File: ‘/var/log’
   Size: 4096            Blocks: 16         IO Block: 4096   directory
 Device: fd00h/64768d    Inode: 67109000    Links: 30
 Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
 Access: 2021-10-08 09:36:40.777992839 -0500
 Modify: 2021-10-03 03:08:04.665752925 -0500
 Change: 2021-10-03 03:08:04.665752925 -0500
  Birth: -

此外,Linux 支持与 Windows NTFS 类似的文件和目录的 ACL 扩展属性。默认情况下,这些属性未设置。例如,ACL 允许文件或目录有多个所有者,每个所有者具有不同的权限。
修改 TestFile 的 ACL 的示例命令是“setfacl -mu:tomcat:w TestFile”读取文件 ACL 的命令是“getfacl Testfile”。以下输出显示用户 myuser 拥有该文件并具有 rw 权限,tomcat 用户具有使用上述命令设置的 w 权限,rpcuser 具有执行权限。lp 组具有 r 权限(可能的选择为 rwx)。

[myuser@server bin]$ getfacl TestFile
# file: TestFile 
# owner: myuser  
# group: lp

user::rw-
user:rpcuser:--x
user:tomcat:-w-

group::r--
mask::rwx

other::r-- 

而且在 Linux 中,太多永远不够,因此文件或目录也存在“属性”(lsattr 和 chattr)和“扩展属性”(setfattr 和 getfattr)的概念。
属性包括不可变属性 - 即使是 root 也不能​​在不删除该标志的情况下删除文件,s = 安全删除等。不过,这些并不常用。扩展属性甚至更奇怪 - 比如 user.checksum 等。不过,很少看到它们被使用。

答案3

这显示了根目录中包含的文件夹的所有属性 ls-全部/ 。希望这是你想要的

答案4

如果你的 shell 是zsh,并且你有扩展的全局变量(setopt extended_glob在你里面.zshrc),你可以使用

ls -ld *(/)

扩展的 glob 模式意味着:列出所有目录条目。如果您需要像 中一样进行递归find,则可以使用

ls -ld **/*(/) 

我确信bash也有同等的东西……

相关内容