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

发布于 2024-12-10 03:57:50 字数 97 浏览 0 评论 0原文

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

So I am in a Linux course and having trouble figuring out this question. I need to come up with a command that a user can execute to display word counts for all readable files in a folder, without displaying any error messages. I'm sure this is simple but I can find it anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2024-12-17 03:57:50

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

您可以将 find-type f 一起使用code> 仅适用于文件,-maxdepth 1 以便 find 不会在子目录中搜索,-read 因此它只搜索可读文件和 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

来自 人发现

 -最大深度级别
          下降至命令行下方目录的最多级别(非负整数)级别
          论据。 -最大深度0
           意味着仅将测试和操作应用于命令行参数。


   -可读
          匹配可读的文件。这考虑到访问控制列表和其他per-
          -perm 测试忽略的任务工件。该测试使用 access(2) 系统
          调用,因此可能会被执行 UID 映射(或 root-squashing)的 NFS 服务器愚弄,因为许多
          系统在客户端内核中实现 access(2),因此无法使用 UID 映射
          服务器上保存的信息。

   -C型
          文件类型为 c:

          b 块(缓冲)特殊

          c 字符(无缓冲)特殊

          d目录

          p 命名管道 (FIFO)

          f 常规文件

          l 符号链接;如果 -L 选项或 -follow 选项有效,则这永远不会成立,
                 除非符号链接被破坏。如果要搜索符号链接时-L
                 生效后,使用 -xtype。

          插座

          D门(Solaris)

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

You can use find with -type f for files only, -maxdepth 1 so that find doesn't search in sub-directories, -readable so it only searches readable files and wc -w to count the words in the files:

[ 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

To show that the wc -w is correct:

[ 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

From 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文