遍历获取 Linux 某目录下所有子目录及文件信息
最近有一个需求,需要遍历某一目录下的所有子目录及文件信息,包括:类型(文件或目录)、名称、建立时间、最后一次修改时间、大小(文件)。
开始想得比较复杂,又是循环又是遍历。后来找到了 find 和 xargs ,一行命令搞定。如下:
find . -print0 | xargs -0 stat --printf="%f %N %W %Y %s\n"
在这里, stat 后面的参数中一定要使用 %N 而不是 %n ,这样就可以把文件引起来,可以有效解决文件或目录带空格的问题了。后面调用的是 stat 来进行查看,它的完整参数如下:
stat --help
Usage: stat [OPTION]... FILE... Display file or file system status. Mandatory arguments to long options are mandatory for short options too. -L, --dereference follow links -f, --file-system display file system status instead of file status -c --format=FORMAT use the specified FORMAT instead of the default; output a newline after each use of FORMAT --printf=FORMAT like --format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, include \n in FORMAT -t, --terse print the information in terse form --help display this help and exit --version output version information and exit The valid format sequences for files (without --file-system): %a access rights in octal (note '#' and '0' printf flags) %A access rights in human readable form %b number of blocks allocated (see %B) %B the size in bytes of each block reported by %b %C SELinux security context string %d device number in decimal %D device number in hex %f raw mode in hex %F file type %g group ID of owner %G group name of owner %h number of hard links %i inode number %m mount point %n file name %N quoted file name with dereference if symbolic link %o optimal I/O transfer size hint %s total size, in bytes %t major device type in hex, for character/block device special files %T minor device type in hex, for character/block device special files %u user ID of owner %U user name of owner %w time of file birth, human-readable; - if unknown %W time of file birth, seconds since Epoch; 0 if unknown %x time of last access, human-readable %X time of last access, seconds since Epoch %y time of last data modification, human-readable %Y time of last data modification, seconds since Epoch %z time of last status change, human-readable %Z time of last status change, seconds since Epoch Valid format sequences for file systems: %a free blocks available to non-superuser %b total data blocks in file system %c total file nodes in file system %d free file nodes in file system %f free blocks in file system %i file system ID in hex %l maximum length of filenames %n file name %s block size (for faster transfers) %S fundamental block size (for block counts) %t file system type in hex %T file system type in human readable form NOTE: your shell may have its own version of stat, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Full documentation at: <http://www.gnu.org/software/coreutils/stat> or available locally via: info '(coreutils) stat invocation'
顺便也列一下平时也用得比较多的一个命令:
# 在 /data/codedir/ 这个目录及其子目录下面查找包含 'keyword' 字符串的文件并列出来 grep -rn 'keyword' /data/codedir/