Ubuntu 终端中是否有文件存在命令?

Sol*_*olo 19 command-line

有什么办法可以检查 Ubuntu 终端中是否存在文件或文件夹?

sud*_*dus 34

您可以使用testshell的命令bash

$ test -e ~/.bashrc && echo file exists || echo file not found
file exists
$ test -e ~/.bashrcx && echo file exists || echo file not found
file not found
Run Code Online (Sandbox Code Playgroud)

命令

help test
Run Code Online (Sandbox Code Playgroud)

打印具有不同选项的帮助文本,您可以将其与test命令一起使用。

您可能还会发现以下帮助文本很有用,以及@dessert 评论中的链接,

help [
Run Code Online (Sandbox Code Playgroud)

help [[
Run Code Online (Sandbox Code Playgroud)

find如果您不知道文件在哪里(因此您必须在多个目录中搜索它)或者您想查找文件的不同版本,您可以使用该命令。

$ sudo find / -name .bashrc
[sudo] password for sudodus: 
/etc/skel/.bashrc
/root/.bashrc
find: ”/run/user/1002/gvfs”: Permission denied
/media/multimed-2/test/test/2015-04/colour-prompt/home/guru/.bashrc
/media/multimed-2/test/test/2015-04/colour-prompt/root/.bashrc
/media/multimed-2/test/test/2015-04/colour-prompt/etc/skel/.bashrc
/media/multimed-2/rsync-bup/nio/.bashrc
/home/lfs/.bashrc
/home/myself/.bashrc
/home/nio/.bashrc
/home/sudodus/.bashrc
Run Code Online (Sandbox Code Playgroud)

  • 我建议使用 -e 代替。见 https://askubuntu.com/a/970306/295286 (2认同)
  • `-e` 测试原始存在,`-f` 和 `-d` 分别额外测试它是文件还是目录。 (2认同)