Search code examples
bashls

ls - ltr error because nothing in folder


I have a script and in the script I am running the below command.

ls -1tr ${ARCH1_DIR}/* | grep ${LOG_PREFIX} > $copied_binlog_set

if the folder is empty I get an error.

ls: cannot access /n01/mysqlarch1/*: No such file or directory

The script at this point stops processing. Is there a way so the script can carry on processing if the folder is empty.


Solution

  • You can try:

    ls -1tr ${ARCH1_DIR}/* 2>/dev/null | grep ${LOG_PREFIX} > $copied_binlog_set
    

    When you write 2> file, it redirect stderr to file, but you can use the null device and throws the input it gets. You can think about it as the black hole of your system.