Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

ls

How to ignore the error 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
like image 481
Luis Henrique Avatar asked Dec 21 '25 02:12

Luis Henrique


1 Answers

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).

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.
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.

like image 67
Dominique Avatar answered Dec 23 '25 15:12

Dominique



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!