如何在Linux命令行上只读取可读文件?对不可读的文件没有显示权限错误

时间:2011-10-13 15:33:57

标签: linux command-line

所以我在Linux课程中并且无法弄清楚这个问题。我需要提出一个命令,用户可以执行该命令来显示文件夹中所有可读文件的字数,而不显示任何错误消息。我确信这很简单,但我可以在任何地方找到它。

1 个答案:

答案 0 :(得分:1)

find /home/jon/ -maxdepth 1 -readable -type f -exec wc -w {} \;

您可以将find-type f仅用于-maxdepth 1文件,以便find不会在子目录-readable中进行搜索搜索可读文件和wc -w来计算文件中的单词:

[ 08:36 jon@host ~ ]$ find /home/jon/ -maxdepth 1 -readable -type f -exec wc -w {} \;
6 /home/jon/.screenrc
27 /home/jon/.bash_profile
418 /home/jon/.xsession-errors
105 /home/jon/.lesshst
3 /home/jon/.bash_logout
49 /home/jon/.toprc
102 /home/jon/.zshrc
1916 /home/jon/.viminfo
2661 /home/jon/.bash_history
17 /home/jon/.bashrc

要表明wc -w是正确的:

[ 08:37 jon@host ~ ]$ cat .screenrc
multiuser on
acladd root
altscreen on

[ 08:40 jon@host ~ ]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH

[ 08:40 jon@host ~ ]$ cat .bash_logout
# ~/.bash_logout
/usr/bin/clear

[ 08:40 jon@host ~ ]$ cat .bashrc
# .bashrc
# User specific aliases and functions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

来自man find

   -maxdepth levels
          Descend  at  most levels (a non-negative integer) levels of directories below the command line
          arguments.  -maxdepth 0
           means only apply the tests and actions to the command line arguments.


   -readable
          Matches files which are readable.  This takes into account access control lists and other per-
          missions artefacts which the -perm test ignores.  This test makes use of the access(2)  system
          call, and so can be fooled by NFS servers which do UID mapping (or root-squashing), since many
          systems implement access(2) in the client's kernel and so cannot make use of the  UID  mapping
          information held on the server.

   -type c
          File is of type c:

          b      block (buffered) special

          c      character (unbuffered) special

          d      directory

          p      named pipe (FIFO)

          f      regular file

          l      symbolic link; this is never true if the -L option or the -follow option is in  effect,
                 unless  the  symbolic link is broken.  If you want to search for symbolic links when -L
                 is in effect, use -xtype.

          s      socket

          D      door (Solaris)