bash 测试文件是否存在,bash脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12363594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:39:34  来源:igfitidea点击:

Test if file exist, bash script

bashshell

提问by Yotam

Possible Duplicate:
Checking if a directory contains files

可能的重复:
检查目录是否包含文件

I want to see if a directory is empty, so I am using [as follows:

我想看看一个目录是否为空,所以我使用[如下:

[ -f "./ini/*"]

And the return value is always 1, even though the directory is not empty. Any ideas what's wrong?

并且返回值始终为 1,即使目录不为空。任何想法有什么问题?

回答by David W.

Try this:

尝试这个:

$ echo "*"   #With quotes

vs.

对比

$ echo *    #No quotes

See the difference? When you put quotes around a string, the shell can't globto replace the asterisk with the matching files.

看到不同?当你把周围一个字符串引号,shell不能glob的与匹配的文件替换星号。

There are multitude of ways to get what you want. Simplest is to use the lscommand without the test command. The ifstatement takes the output results of the commandyou give it. The [...]simply are an alias to the testcommand. These two statements are equivalent:

有很多方法可以得到你想要的东西。最简单的是使用ls命令而不使用 test 命令。该if语句采用您给它的命令的输出结果。在[...]简单的是别名的test命令。这两个语句是等价的:

$ if test -f foo.txt

$ if [ -f foo.txt ]

Basically, all the test command does is return a 0if the test is true or a non-zero if the test is false. All the ifcommand does is execute the ifstatement if the command it's given returns a 0exit code. The reason I'm going all through this is to say let's forget the test and simply use the lscommand.

基本上,所有测试命令所做的就是0在测试为真时返回 a,如果测试为假则返回非零值。如果给定的命令返回退出代码,则该if命令所做的就是执行该if语句0。我经历这一切的原因是说让我们忘记测试并简单地使用ls命令。

Assume that you have a file foo.txt, but no file bar.txt. Try the following:

假设您有一个文件foo.txt,但没有文件bar.txt。请尝试以下操作:

$ ls foo.txt > /dev/null 2>&1
$ echo $?
0
$ ls bar.txt > /dev/null 2>&1
$ echo $?
2

The > /dev/null 2>&1is used to inhibit all output. Notice that the exit code of the lscommand when a file exists is 0while the exit code of the lscommand when the file doesn't exist isn't 0. (It's 2in this case). Now, let's use that instead of the test command:

> /dev/null 2>&1被用来抑制所有输出。请注意,ls文件存在时命令0的退出代码是ls,文件不存在时命令的退出代码不是0。(2在这种情况下)。现在,让我们用它代替 test 命令:

if ls ./ini/* > /dev/null 2>&1
then
    echo "There are files in that directory!"
else
    echo "There are no files in that directory"
fi

Notice I don't even bother with the test command (which can be the word testor the [..]).

请注意,我什至不关心 test 命令(可以是单词test[..])。

Hope this makes sense to you. It's sometimes difficult for people to realize that in BASH shell, the ifcommand and the [...]are two separate commands, and all ifis doing is running the command it's given and then acting depending upon the exit code of the command it runs.

希望这对你有意义。人们有时很难意识到,在 BASH shell 中,ifcommand 和 the[...]是两个独立的命令,if所做的只是运行它给出的命令,然后根据它运行的命令的退出代码执行操作。

回答by cdarke

Beware of filenames starting with '.', normal globbing or using ls(shudder) will not detect those There are several ways to do this, the simplest I can come up with is:

注意以“.”开头的文件名,正常的 globbing 或 using ls( shudder) 不会检测到那些有几种方法可以做到这一点,我能想出的最简单的是:

/home/user1> mkdir ini
/home/user1> cd ini
/home/user1/ini> touch .fred jim tom dick harry
/home/user1/ini> cd ..
/home/user1> declare -a names
/home/user1> names=(./ini/* ./ini/.*)
/home/user1> number_of_files=$(( ${#names[@]} -2 ))
/home/user1> echo $number_of_files
5

The -2is required to remove .and ..from the list.

-2需要删除...从列表中。

回答by aurelihein

if find ./ini/ -maxdepth 0 -empty | read;
then
echo "Not Empty"
else
echo "Empty"
fi

回答by AnBisw

If the output of the command below is 0then no files in directory (Directory empty) else it has files!

如果下面命令的输出是0目录中没有文件(目录为空),否则它有文件!

$find "/path/to/check" -type f -exec echo {} \;|wc -l

so for you -

所以对你 -

exist=`find "./ini" -type f -exec echo {} \;|wc -l`
if [ $exist -gt 0 ]
then
   echo "Directory has files"
else
   echo "Directory is empty"
fi

Or

或者

if [ `find "./ini" -type f -exec echo {} \;|wc -l` -gt 0 ]
then
   echo "Directory has files"
else
   echo "Directory is empty"
fi

回答by Andrea Gherardi

This works for me

这对我有用

[ "$(ls -A /path/to/directory)" ]

in your case it would be

在你的情况下,它会是

[ "$(ls -A ./ini/)" ]

you can test it using

你可以使用

[ "$(ls -A /tmp)" ] && echo "Not Empty" || echo "Empty"