0

Ubuntu 16.04

I tried this:

for file in $source/*.zip do echo $file done 

and it works if the directory contains zip files. It prints out all them. But if it does not, it just print the $source/*.zip. I mean if source=/home/usrname/dir which does not contain any zips then it prints

/home/usrname/dir/*.zip 

Is there a way to make it print nothing in that case?

1
  • @don_crissti yours is more appropriate! Commented Nov 9, 2016 at 13:51

2 Answers 2

4

You could use:

find "$source" -name "*.zip" 

And if needed pipe it to xargs or use:

shopt -s dotglob for file in "${source}"/*.zip do if [ -f "${file}" ]; then printf '%s\n' "$file" fi done 

To print only the zip files that are regular files or symlinks to regular files.

1

bash

shopt -s nullglob dotglob files=("$dir"/*.zip) ((${#files[@])) && printf '%s\n' "${files[@]}" shopt -u nullglob dotglob # if needed 

zsh

files=($dir/*.zip(ND)) (($#files)) && printf '%s\n' $files 

fish

set files $dir/.*.zip $dir/*.zip if count $files > /dev/null printf '%s\n' $files end 

(beware it would omit a file called .zip though (contrary to the other solutions given here)).

ksh93

FIGNORE='@(.|..)' files=(~(N)"$dir"/*.zip) ((${#files[@])) && printf '%s\n' "${files[@]}" unset FIGNORE # if needed 

yash

set -o dot-glob -o null-glop files=("$dir"/*.zip) [ "${files[#]}" -gt 0 ] && printf '%s\n' "$files" set +o got-glob +o null-glob # if needed 

POSIXly

export LC_ALL=C set -- "$dir"/[*].zip "$dir"/*.zip case ${1##*/}${2##*/} in '[*].zip*.zip') shift 2 esac set -- "$dir"/.[*].zip "$dir"/.*.zip "$@" case ${1##*/}${2##*/} in '.[*].zip.*.zip') shift 2 esac [ "$#" -gt 0 ] && printf '%s\n' "$@" 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.