【问题标题】:Grep: could not find fileGrep:找不到文件
【发布时间】:2014-07-31 06:05:49
【问题描述】:

在 Unix 环境中,我需要将报告写入 x_out 文件,并且在进程结束时,需要删除该文件。但是,它总是抛出以下错误。

grep: can't open /XYZ/123/Tmp/x_out

rm: /XYZ/123/Tmp/x_out non-existent

但是,我可以在相应的位置找到文件 x_out。我也可以打开并查看内容。我发现有时文件名会随着一些“~”的字符而改变。有没有办法解决这个问题?

编辑:我没有附加任何“~”。但是,我怀疑可能是附加了一些不可读的文字。

编辑:我在这里添加了实际错误

编辑:我使用的命令 grep "Report_values" ${REPORTOUT}|cut -d "|" -f 6

rm ${报告输出}

【问题讨论】:

  • 你确定“对应位置”就是你想的那样吗? (例如:您是否尝试在有问题的命令正上方插入pwd 命令,以确认当前工作目录嵌套在包含x_out 的目录的下方两层?)
  • 添加更多详细信息。发布你的整个脚本。
  • 如果将波浪号附加到文件中,那么它将是一个备份文件。只需删除波浪号。现在它将显示出来,您可以对其执行 grep。
  • 你用的是什么外壳? Bash 没有说x_out is not existent。请复制确切的输出并提供脚本的内容...
  • @beroe 更新了脚本。这就是我得到的。

标签: unix grep rm


【解决方案1】:

嗯,有两种种可能性,我能想到。毫无疑问还有更多,但我的头顶空间不是很大:-)

首先是文件存在,尽管你的断言。

第二个是它确实存在,但你在错误的地方寻找它(例如,你已更改到不同的目录)。

如果您放置类似于以下内容的行:

( pwd ; cd ../.. ; pwd ; ls )

grep/rm 之前的脚本中,它应该告诉您这两种可能性是否正确。

它将提供您当前的目录、您要查找文件的目录以及该目录中的文件。

【讨论】:

  • 哦..让我检查一下@paxdiablo
  • 感谢 teh eduts,@Yohanes,我很讨厌 incahol 的富裕 :-)
  • wlel,没有 porb,我是 srue aou yre.. =)
  • @paxdiablo 是的.. 已解决。路径有问题。谢谢
【解决方案2】:

只需检查文件名中是否有不可打印/图形字符...使用 ls 的 -Q 或 -q 标志来查看它...在下面查看它的外观...。

来自 ls 手册页的标志描述

   -q, --hide-control-chars
          print ? instead of non graphic characters

   --show-control-chars
          show non graphic characters as-is (default unless program is `ls' and output is a terminal)

   -Q, --quote-name
          enclose entry names in double quotes

   --quoting-style=WORD
          use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape

演示会议

$ ls
demo.txt       test.dat
$ ls -1
demo.txt
test.dat
$ cat demo.txt
cat: demo.txt: No such file or directory
$ rm demo.txt
rm: cannot remove `demo.txt': No such file or directory
$ ls -Q
"demo.txt     "  "test.dat"
$ ls -1Q
"demo.txt     "
"test.dat"
$ rm "demo.txt     "
$

【讨论】:

  • 问题在于位置不正确。但是,我在删除其他一些文件时遇到问题,显示不存在的错误。这样做是因为这个不可打印字符的原因。
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 2011-08-07
相关资源
最近更新 更多