linux - 检查文件是否存在 Linux bash

标签 linux bash file

<分区>

所以我试图检查一个文件是否存在,如果存在,脚本应该做一些事情。我遇到的问题实际上是让它认识到确实存在某些东西。

if [ -e /temp/file.txt ]; then
        echo "file found!"
        sudo cp -r temp/* extra
else
        echo "file not found! Creating new one..."
        ./create.sh
fi

下面是我正在测试的目录中的文件示例。他们显然在那里,但出于某种原因我无法让脚本看到它。我做错了什么?

nima@mkt:/docs/text$ ls -a temp
.  ..  more  file.txt  file2.txt

最佳答案

您在测试中使用了绝对路径,而您应该使用相对路径:

 if [ -e ./temp/file.txt ]; then

关于linux - 检查文件是否存在 Linux bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663575/

相关文章:

node.js - 招摇: is '😱 Could not render this component, see the console' an error message or can it be ignored

bash - 在创建脚本以更新/etc/hosts 文件中的主机名时遇到多个问题?

c - 如何将 char ** 数据类型转换为可以在 c 中打印的内容?

linux - 支持目录硬链接(hard link)的文件系统

linux - 通过 shell 自动化脚本编辑 .CSV 文件

linux - 使用 wget 时同时使用 -i 和 -O

bash - 在 heredoc 中对 shell 变量使用双引号

android - 使用 Apache FileUtils 复制文件时如何显示进度条?安卓

android - 为什么 ContentResolver 看不到其他应用添加的文件?

Linux (Fedora) 无法在终端中运行 psql 命令 - 角色不存在