Skip to content
Advertisement

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

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement