【问题标题】:removing files which are in one folder from another folder linux shell从另一个文件夹 linux shell 中删除一个文件夹中的文件
【发布时间】:2014-02-15 17:41:42
【问题描述】:

我有两个目录。 DirA 包含DirB 中的所有文件。 我想从DirA 中删除DirB 中的所有文件。 怎么能在 linux 命令行中做到这一点?

我正在使用 ubuntu。

谢谢

【问题讨论】:

    标签: linux shell ubuntu command-line


    【解决方案1】:
    cd DirB
    for i in *
    do
        rm DirA/"$i"
    done
    

    编辑:$i 周围使用双引号来处理包含空格的文件名。

    【讨论】:

      【解决方案2】:

      你可以这样做 i for 循环:

      for f i DirB/*; do
          fn="${f##*/}"
          [[ -f "$fn" ]] && rm -f "DirA/$fn"
      done
      

      【讨论】:

        【解决方案3】:

        这里有一个带有执行输出的示例,如果文件名包含空格,它也可以工作(顺便说一下,我不建议这样做不舒服):

        root@folgore:/tmp/test# tree
        .
        ├── DirA
        │   ├── a
        │   ├── b
        │   ├── c
        │   └── d
        └── DirB
            ├── a
            ├── b
            ├── e
            ├── g
            ├── h
            └── r
        
        2 directories, 10 files
        root@folgore:/tmp/test# for f in `ls DirB/* | sed 's/ /_SPC_/g'`;do fa=`echo $f | sed 's/_SPC_/ /g;s/^DirB\//DirA\//'`;echo "removing $fa if exists";rm -f "$fa";done
        removing DirA/a if exists
        removing DirA/b if exists
        removing DirA/e if exists
        removing DirA/g if exists
        removing DirA/h if exists
        removing DirA/r if exists
        root@folgore:/tmp/test# tree
        .
        ├── DirA
        │   ├── c
        │   └── d
        └── DirB
            ├── a
            ├── b
            ├── e
            ├── g
            ├── h
            └── r
        
        2 directories, 8 files
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-25
          • 2013-12-08
          • 1970-01-01
          • 2013-03-18
          • 2019-06-23
          • 2015-04-27
          • 2023-01-14
          • 1970-01-01
          相关资源
          最近更新 更多