从 shell 脚本检查目录是否包含文件 [英] Checking from shell script if a directory contains files

查看:31
本文介绍了从 shell 脚本检查目录是否包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 shell 脚本检查目录是否包含文件?

From a shell script, how do I check if a directory contains files?

类似的东西

if [ -e /some/dir/* ]; then echo "huzzah"; fi;

但是如果目录包含一个或多个文件则有效(上述仅适用于 0 或 1 个文件).

but which works if the directory contains one or several files (the above one only works with exactly 0 or 1 files).

推荐答案

目前的解决方案使用 ls.这是一个全 bash 解决方案:

The solutions so far use ls. Here's an all bash solution:

#!/bin/bash
shopt -s nullglob dotglob     # To include hidden files
files=(/some/dir/*)
if [ ${#files[@]} -gt 0 ]; then echo "huzzah"; fi

这篇关于从 shell 脚本检查目录是否包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆