【问题标题】:Make directory structure from forward slashes从正斜杠创建目录结构
【发布时间】:2015-02-13 18:42:53
【问题描述】:

我将一个 html 文档从 Windows 机器拉到我的 linux 机器上,问题是两者之间没有维护目录结构。这就是它在linux中的样子

MyReport\partition_and_timing.files\img0.jpg
MyReport\partition_and_timing.html
MyReport\protocols_cards.html
MyReport\report_title.files\img0.jpg
MyReport\report_title.html
MyReport\scripts.html

我正在考虑使用 bash 脚本,将“\”更改为“/”,本质上是创建一个文件夹。我猜他们是这样做的标准方式,但无法弄清楚。

这就是我的工作

for file in *; do mv $file echo $file | sed 's/\\/\//g' ; done

【问题讨论】:

  • 使用cygwin,实际上可以只使用正斜杠。系统知道会为您转换它们。
  • 因为这是一个 HTML 文档,它们应该是正斜杠,即使在 Windows 中也是如此。浏览器将为您将它们转换为反斜杠。

标签: linux windows bash shell unix


【解决方案1】:

试试mv $file ${file//\\/\/}。它将${file} 中的每个反斜杠替换为正斜杠。

【讨论】:

    【解决方案2】:

    你必须捕捉

     echo $file | sed 's/\\/\//g'
    

    因为现在您要移动一个文件,一个名为“echo”的不存在的文件到文件中,并将(错误的)结果通过管道传递给无法处理它的 sed。

    【讨论】:

      【解决方案3】:

      file拥有你要修改的文件列表:

      for f in $file; do
          cp "$f" "$f".backup
          sed -i 's/\\/\//g' $f
      done
      

      这是一个for循环,首先创建一个备份文件,然后使用sed进行就地修改。

      -i of sed 表示要进行就地修改,所以修改会直接应用到文件中。

      【讨论】:

        猜你喜欢
        • 2020-11-10
        • 2021-02-18
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多