Linux mv:无法统计错误:没有此类文件或目录错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12729784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:30:34  来源:igfitidea点击:

mv: cannot stat error : No such file or directory error

linuxbashstat

提问by charlie

I need to move the files of a directory to another directory.I get stat error when I used the following program.

我需要将一个目录的文件移动到另一个目录。当我使用以下程序时出现 stat 错误。

for i in dir1/*.txt_dir; do
mv $i/*.txt  dir2/`basename $i`.txt
done

error message

错误信息

mv: cannot stat `dir1/aa7.txt_dir/*.txt': No such file or directory

回答by John Kugelman

mv $i/*.txt  dir2/`basename $i`.txt

This doesn't work when there are no text files in $i/. The shell passes the raw string "$i/*.txt"to mvwith the unexpanded *in it, which mvchokes on.

$i/. 外壳将原始字符串传递"$i/*.txt"mv其中未扩展的字符串*,它会mv窒息。

Try something like this:

尝试这样的事情:

for i in dir1/*.txt_dir; do
    find $i -name '*.txt' -exec mv {} dir2/`basename $i`.txt \;
done

回答by sushant-hiray

Normally, when a glob which does not match any filenames is expanded, it remains unchanged. Thus, you get results like this:

通常,当一个不匹配任何文件名的 glob 被扩展时,它保持不变。因此,您会得到如下结果:

$ rm .bak rm: cannot remove `.bak': No such file or directory

$ rm .bak rm: 无法删除 `.bak': 没有那个文件或目录

To avoid this we need to change the default value of nullglob variable.

为了避免这种情况,我们需要更改 nullglob 变量的默认值。

    #BASH

    shopt -s nullglob

    for i in dir1/*.txt_dir; do
       mv $i/*.txt  dir2/'basename $i'.txt
    done

Read more about it here: http://mywiki.wooledge.org/NullGlob

在此处阅读更多相关信息:http: //mywiki.wooledge.org/NullGlob

Hope this helps!

希望这可以帮助!

回答by OrkTech

Whilst it is not shown in your example - using the correct quotes is important. in BASH "*" evaluates to * and '*' evaluates to the expansion glob. so

虽然它没有在您的示例中显示 - 使用正确的引号很重要。在 BASH 中,"*" 的计算结果为 *,而 '*' 的计算结果为扩展全局。所以

`ls *`

will show all files in directory and

将显示目录中的所有文件和

`ls "*"`

will show all files named the literal *

将显示所有名为文字 * 的文件

回答by ms_guruvai

when you put directory/* alone in for iteration, it list each file with absolute path. use `ls

当您单独放置 directory/* 进行迭代时,它会列出每个文件的绝对路径。使用`ls

for i in ls dir1/*.txt_dir; do

因为我在ls dir1/*.txt_dir;做