How to suppress certain error message in 'find' command?

membersound

find /var/log/myfile.*.txt -type f

If no file is present matching that pattern, I'm getting error logs like No such file or directory.

Question: how can I prevent only this error message? I could of course add a 2>/dev/null at the end, but that would suppress any errors. How could I just ignore the one mentioned?

Because I have a cronjob that deletes some files periodically, and always logs errors if no file was present:

@daily find /var/log/myfile.*.txt -mtime +7 -delete

Kamil Maciorowski

The right thing in this case is not to trigger this message at all.

When you run this in a shell

find /var/log/myfile.*.txt -type f

/var/log/myfile.*.txt is expanded by the shell; find gets expanded object(s) or literal /var/log/myfile.*.txt if there is no match. The latter case triggers No such file or directory.

You can create a dummy file first: touch '/var/log/myfile.dummy_name.txt' (compare elephant in Cairo). This "solution" is not really elegant though.

A better solution is to make find handle the pattern:

find /var/log/ -type f -name "myfile.*.txt"

where double-quotes prevent globbing in the shell (compare this). In this case * is handled by the find itself because -name supports such patterns.

The above approach however can match myfile.*.txt in any subdirectory of /var/log/ as well. If your find supports -maxdepth, use it:

find /var/log/ -maxdepth 1 -type f -name "myfile.*.txt"

If not, see this: Limit POSIX find to specific depth?


Your OS probably already uses logrotate(8) to manage logfiles.

In my Debian logrotate runs daily because of /etc/cron.daily/logrotate; it should be similar in Ubuntu. I can create a custom config in /etc/logrotate.d/ and manage arbitrary logfiles this way. Consider this approach.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to suppress error message of a command?

From Dev

How to suppress OpenCV error message

From Dev

The command "find" output an error message

From Dev

tryCatch suppress error message

From Dev

Suppress error message in R

From Dev

Suppress error message in R

From Dev

How to suppress PowerShell error message during variable assignment

From Dev

matlab - suppress error message backtrace

From Dev

Script error message: "Could not find command in namespace"

From Dev

How to fully suppress an error when invoking powershell with a command?

From Dev

How to fully suppress an error when invoking powershell with a command?

From Dev

How to redirect the output of a command to a function but suppress the error and output to console?

From Dev

Suppress command output lines that contain a certain string

From Dev

How to find out if there is a package with certain command in AlpineLinux?

From Dev

Certain commands not working : make, wget - error message command not found

From Dev

Suppress welcome message on bash remote command execution

From Dev

How to suppress R startup message?

From Dev

How to suppress command output safely?

From Dev

How to suppress output of bash command

From Dev

Suppress supplementary error message from unit test

From Dev

JAX-RS : Suppress Error Message

From Dev

Not able to suppress the error message using /dev/null

From Dev

How to avoid Vim error message "Not an editor command"

From Dev

how to capture command error message in variable for if block

From Dev

How to avoid Vim error message "Not an editor command"

From Dev

How do you execute a certain command if previous command returned error?

From Dev

grub issues message at boot - "error - can't find command hwmatch"

From Dev

How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

From Dev

How to capture error of failed command with message using tee command

Related Related

  1. 1

    How to suppress error message of a command?

  2. 2

    How to suppress OpenCV error message

  3. 3

    The command "find" output an error message

  4. 4

    tryCatch suppress error message

  5. 5

    Suppress error message in R

  6. 6

    Suppress error message in R

  7. 7

    How to suppress PowerShell error message during variable assignment

  8. 8

    matlab - suppress error message backtrace

  9. 9

    Script error message: "Could not find command in namespace"

  10. 10

    How to fully suppress an error when invoking powershell with a command?

  11. 11

    How to fully suppress an error when invoking powershell with a command?

  12. 12

    How to redirect the output of a command to a function but suppress the error and output to console?

  13. 13

    Suppress command output lines that contain a certain string

  14. 14

    How to find out if there is a package with certain command in AlpineLinux?

  15. 15

    Certain commands not working : make, wget - error message command not found

  16. 16

    Suppress welcome message on bash remote command execution

  17. 17

    How to suppress R startup message?

  18. 18

    How to suppress command output safely?

  19. 19

    How to suppress output of bash command

  20. 20

    Suppress supplementary error message from unit test

  21. 21

    JAX-RS : Suppress Error Message

  22. 22

    Not able to suppress the error message using /dev/null

  23. 23

    How to avoid Vim error message "Not an editor command"

  24. 24

    how to capture command error message in variable for if block

  25. 25

    How to avoid Vim error message "Not an editor command"

  26. 26

    How do you execute a certain command if previous command returned error?

  27. 27

    grub issues message at boot - "error - can't find command hwmatch"

  28. 28

    How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

  29. 29

    How to capture error of failed command with message using tee command

HotTag

Archive