31

How can I use the test command for an arbitrary number of files, passed in using an argument with a wildcard?

For example:

test -f /var/log/apache2/access.log.* && echo "exists one or more files"

Currently, it prints

error: bash: test: too many arguments
1

11 Answers 11

56

This solution seems to me more intuitive:

if [ `ls -1 /var/log/apache2/access.log.* 2>/dev/null | wc -l ` -gt 0 ];
then
    echo "ok"
else
    echo "ko"
fi
4
  • 2
    To me this was the most relevant answer to the question.
    – Ken Ingram
    Feb 10, 2016 at 0:19
  • This is exactly what I was looking for.
    – jimh
    Oct 1, 2018 at 21:29
  • 3
    good Idea but you can do it simpler: ˋif ls -/var/log/apache2/access.log.* 2>/dev/null 1>&2ˋ works also, test [ ], backticks and wc -l is not needed May 4, 2020 at 12:37
  • For ones who don't know, you have to redirect stderr to a null file using 2>/dev/null since if no match found, the error of ls wouldn't be printed out.
    – Khoa LT
    Jan 10 at 1:45
12

To avoid "too many arguments error", you need xargs. Unfortunately, test -f doesn't support multiple files. The following one-liner should work:

for i in /var/log/apache2/access.log.*; do test -f "$i" && echo "exists one or more files" && break; done

By the way, /var/log/apache2/access.log.* is called shell-globbing, not regexp. Please see Confusion with shell-globbing wildcards and Regex for more information.

9

First, store files in the directory as an array:

logfiles=(/var/log/apache2/access.log.*)

Then perform a test on the count of the array:

if [[ ${#logfiles[@]} -gt 0 ]]; then
  echo 'At least one file found'
fi
4
  • Good idea. But when no file exists, the array content will be ['/var/log/apache2/access.log.*'] (one element containing the '*'). Suggestion: [[ ${#logfiles[@]} -gt 1 || -e ${logfiles[0] ]] or simply [[ -e ${logfiles[0] ]] (as dmaticzka does)
    – simohe
    Oct 17, 2018 at 9:32
  • I would suggest adding shopt -s nullglob to avoid having an element in the array content if no file exists. In that case, the variable will be empty and the following test easier to make.
    – zero0cool
    Dec 1, 2020 at 8:15
  • @simohe @skupjoe what about adding test for regular file? if [[ ${#logfiles[@]} -gt 0 ]] && [[ -f ${#logfiles[@]} ]] ; then . Sorry for being late for the party, so the shopt -s nullglob will not be necessary? Jan 19, 2022 at 10:41
  • @zero0cool good catch! How could I miss that? :) And @sunta3iouxos good suggestion. The test should be: if [[ ${#logfiles[@]} -gt 0 ]] && [[ -e ${logfiles[@]} ]]; then to catch the condition for 0 files! Or use -f or -d to test for just files/directories ;).
    – skupjoe
    Feb 24, 2022 at 4:44
6

This one is suitable for use with the Unofficial Bash Strict Mode, no has non-zero exit status when no files are found.

The array logfiles=(/var/log/apache2/access.log.*) will always contain at least the unexpanded glob, so one can simply test for existence of the first element:

logfiles=(/var/log/apache2/access.log.*)

if [[ -f ${logfiles[0]} ]]
then 
  echo 'At least one file found'
else
  echo 'No file found'
fi
1
  • Great idea to test just [0] of array.
    – ajaaskel
    Oct 17, 2022 at 21:54
5

If you wanted a list of files to process as a batch, as opposed to doing a separate action for each file, you could use find, store the results in a variable, and then check if the variable was not empty. For example, I use the following to compile all the .java files in a source directory.

SRC=`find src -name "*.java"`
if [ ! -z $SRC ]; then
    javac -classpath $CLASSPATH -d obj $SRC
    # stop if compilation fails
    if [ $? != 0 ]; then exit; fi
fi
3

You just need to test if ls has something to list:

ls /var/log/apache2/access.log.* >/dev/null 2>&1 && echo "exists one or more files"
1
  • Why is this one downvoted? Redirection is ugly? Better to use explicit if statement? Shouldn't use ls for scripting?
    – kevmitch
    Oct 3, 2016 at 23:57
2

Variation on a theme:

if ls /var/log/apache2/access.log.* >/dev/null 2>&1
then 
  echo 'At least one file found'
else
  echo 'No file found'
fi
1
ls -1 /var/log/apache2/access.log.* | grep . && echo "One or more files exist."
0
1

Or using find

if [ $(find /var/log/apache2/ -type f -name "access.log.*" | wc -l) -gt 0 ]; then
  echo "ok"
else
  echo "ko"
fi
1
  • 1
    I recommend to add -maxdepth 1 before -type f to not go into subdirectories. For speedup, use this: find /var/log/apache2/ -maxdepth 1 -type f -name "access.log.*" -print -quit (quit after first match)
    – simohe
    Oct 17, 2018 at 9:31
1

This condition below doesn't produce stderr. the condition's blackhole (/dev/null) doesn't prevent the stderr in cmd.

if [[ $(ls -1 /var/log/apache2/access.log.* | wc -l ) -gt 0 ]] 2> /dev/null

therefore I suggests this code.

if [[ $(ls -1 /var/log/apache2/access.log.* | wc -l ) -gt 0 ]] 2> /dev/null 
then
    echo "exists one or more files."
fi
4
  • it work also with broken symlinks?
    – stefcud
    Nov 13, 2022 at 20:46
  • If the symlink's target changed, you could also change the broken symlinks to connect the changed target. let's say link pair { target : symlink }. I change the target name with the suffix 1. So there is no target anymore but target1. your symlink is broken. but if you "ln -s -f target1 symlink" (-f is force write on existing symlink file option), your symlink will be alive. Nov 14, 2022 at 5:06
  • Also, I changed the condition on the script above to a symlink, which works great. Nov 14, 2022 at 5:09
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 16, 2022 at 0:40
0

more simplyfied:

if ls /var/log/apache2/access.log.* 2>/dev/null 1>&2; then
   echo "ok"
else
   echo "ko"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.