怎么是否存在

linux怎么检查文件是否存在

服务器技术 2021-12-03 09:12:16 31

导读

Linux检查文件是否存在 1.test命令 它经常用来测试条件表达式,用到的命令为test-e<文件>。 2.用bashshell检查 假设我们有文件目录/home/benben/go_project和文件/home/benben/go_project/test.txt,下面我们用bashshell命令来检测下这个目录和文件是否存在。 检……

Linux检查文件是否存在

1. test命令

它经常用来测试条件表达式,用到的命令为test -e <文件>

2. 用bash shell检查

假设我们有文件目录/home/benben/go_project和文件/home/benben/go_project/test.txt,下面我们用bash shell命令来检测下这个目录和文件是否存在。

  • 检查目录,执行命令[-d /home/benben/go_project ] && echo "found" || echo "not found"或者[-d ./go_project ] && echo "found" || echo "not found"
  • 检查文件,执行命令[-f /home/benben/go_project/test.txt ] && echo "found" || echo "not found"或者[-f ./test.txt ] && echo "found" || echo "not found"
1253067 TFnetwork_cn