简体   繁体   English

如何忽略 ls 命令中没有这样的文件目录的错误?

[英]How to ignore the error No such file directory in the ls command?

How to ignore the error No such file directory ?如何忽略错误No such file directory

ls /opt/data/config/run_*.config | cut -f1 -d '.' | cut -f2 -d '_' 2>/dev/null
ls: cannot access /opt/data/config/run_*.config: No such file or directory

You are doing the following:您正在执行以下操作:

ls <something> | cut <some_cut> | cut <some_other_cut> 2>/dev/null

This will do the ls , the first and the second cut , and when an error is generated at the second cut , it will be sent to the null device (which means it will be removed).这将执行ls 、第一次和第二次cut ,当第二次cut产生错误时,它将被发送到null设备(这意味着它将被删除)。

If you want to remove the error message from any command, you need to put it immediately after the corresponding command, so you get three cases:如果要从任何命令中删除错误消息,则需要将其紧跟在相应命令之后,因此会得到三种情况:

Case 1: ls <something> | cut <some_cut> | cut <some_other_cut> 2>/dev/null
Case 2: ls <something> 2>/dev/null | cut <some_cut> | cut <some_other_cut>
Case 3: ls <something> 2>/dev/null | cut <some_cut> 2>/dev/null | cut <some_other_cut> 2>/dev/null

Case 1 is the situation you're having now.案例1是你现在的情况。
Case 2 and 3 are possible solutions: case 2 only removes error messages from the ls command, while case 3 removes error messages from every command.案例 2 和 3 是可能的解决方案:案例 2 仅从ls命令中删除错误消息,而案例 3 从每个命令中删除错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM