Skip to main content
deleted 9 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

One thing to bear in mind is that in:

Which inIn csh/tcsh/zsh/bash/ksh (but not fish, see below) is the same as:

One thing to bear in mind is that in:

Which in csh/tcsh/zsh/bash/ksh (but not fish, see below) is the same as:

One thing to bear in mind is that:

In csh/tcsh/zsh/bash/ksh (but not fish, see below) is the same as:

added 1074 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

In any case, if it's just to print the matchmatching file paths, you don't need ls. In zsh, you could use its print builtin to print in columns:

If instead you want to check if there's at least one non-hidden file in any of those two directories, you can do:

# bash
if (shopt -s nullglob; set -- /some/path1/* /some/path2/*; (($#))); then
   echo yes
else
   echo no
fi

Or using a function:

has_non_hidden_files() (
  shopt -s nullglob
  set -- "$1"/*
  (($#))
)
if has_non_hidden_files /some/path1 || has_non_hidden_files /some/path2
then
  echo yes
else
  echo no
fi
# zsh
if ()(($#)) /some/path1/*(N) /some/path2/*(N); then
  echo yes
else
  echo no
fi

Or with a function:

has_non_hidden_files() ()(($#)) $1/*(NY1)
if has_non_hidden_files /some/path1 || has_non_hidden_files /some/path2
then
  echo yes
else
  echo no
fi

(Y1 as an optimisation to stop after finding the first file)

Beware those has_non_hidden_files would (silently) return false for directories that are not readable by the user (whether they have files or not). In zsh, you could detect this kind of situation with its $ERRNO special variable.

In any case, if it's just to print the match file paths, you don't need ls. In zsh, you could use its print builtin to print in columns:

In any case, if it's just to print the matching file paths, you don't need ls. In zsh, you could use its print builtin to print in columns:

If instead you want to check if there's at least one non-hidden file in any of those two directories, you can do:

# bash
if (shopt -s nullglob; set -- /some/path1/* /some/path2/*; (($#))); then
   echo yes
else
   echo no
fi

Or using a function:

has_non_hidden_files() (
  shopt -s nullglob
  set -- "$1"/*
  (($#))
)
if has_non_hidden_files /some/path1 || has_non_hidden_files /some/path2
then
  echo yes
else
  echo no
fi
# zsh
if ()(($#)) /some/path1/*(N) /some/path2/*(N); then
  echo yes
else
  echo no
fi

Or with a function:

has_non_hidden_files() ()(($#)) $1/*(NY1)
if has_non_hidden_files /some/path1 || has_non_hidden_files /some/path2
then
  echo yes
else
  echo no
fi

(Y1 as an optimisation to stop after finding the first file)

Beware those has_non_hidden_files would (silently) return false for directories that are not readable by the user (whether they have files or not). In zsh, you could detect this kind of situation with its $ERRNO special variable.

added 317 characters in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

In any case, if it's just to print the match file paths, you don't need ls. In zsh, you could use its print builtin to print in columns:

print -rC3 /some/{path1,path2}/*(N)

Would print the paths raw on 3 columns (and print nothing if there's no match with the Nullglob glob qualifier).

In any case, if it's just to print the match file paths, you don't need ls. In zsh, you could use its print builtin to print in columns:

print -rC3 /some/{path1,path2}/*(N)

Would print the paths raw on 3 columns (and print nothing if there's no match with the Nullglob glob qualifier).

Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k
Loading