【问题标题】:Move command moves the whole directory instead of a specific file in a shell script移动命令移动整个目录而不是 shell 脚本中的特定文件
【发布时间】:2015-02-20 20:02:05
【问题描述】:
/bin/mv /myhome/templocation/images/*.iso /storage/data/repository/ >/dev/null 2>&1

我希望这个命令将 /myhome/templocation/images 中的 iso 移动到 /storage/data/respository。

但是,所见有所不同。在脚本结束时,整个图像文件夹被移动到 /storage/data/repository/.

例如:/storage/data/repository/images/*.iso ...但我希望脚本将 .iso 复制到 /storage/data/respository/*.iso/

我哪里错了..??由于此 shell 脚本在系统启动期间运行,我无法调试。 任何建议。

这种行为出现在 CentOs6 上。

【问题讨论】:

  • 你展示了一行代码,但你谈论的是一个脚本。你还有什么需要给我们看的吗?
  • 尝试使用该命令;没有脚本的其余部分。
  • 也;您希望副本位于 /storage/data/repository/ 或子文件夹中?
  • 感谢您的回复。我想将它复制到 /storage/data/repository/ 中。我不想要任何子文件夹。如果我在 shell 上尝试该命令,它工作正常。

标签: linux shell centos6 mv


【解决方案1】:

使用“*”的外壳扩展可能会导致随机错误和安全漏洞。 使用起来更安全:

find ... -exec mv {} dst; 

喜欢:

find -maxdepth 0 \            <-- do not enter subdirectories
 -type f \                    <-- just files (not links, directories)
 -name "*.iso"  \             <-- just *iso names
 /myhome/templocation/images/ <-- base dir 
 -exec mv {} /storage/data/repository/ \;   <-- {} will be replaced by each file found

【讨论】:

    【解决方案2】:
    foreach file in(path/with/wildcards*)
    do
      if [ $file -f ];                # if file
      then
        mv $file 'new/location$file'
      else
        continue                      # if subdirectory
      fi
    done 
    
    echo finished.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 2010-10-26
      • 1970-01-01
      相关资源
      最近更新 更多