51

私は疑問に思っています-特定のディレクトリ内のファイルを除くディレクトリ内のすべてのファイルを移動するにはどうすればよいですか(「mv」には「--exclude」オプションがないため)?

4

9 に答える 9

94

dir構造が次のようになっていると仮定しましょう。

|parent
    |--child1
    |--child2
    |--grandChild1
    |--grandChild2
    |--grandChild3
    |--grandChild4
    |--grandChild5
    |--grandChild6

そして、次のように表示されるようにファイルを移動する必要があります。

|parent
    |--child1
    |   |--grandChild1
    |   |--grandChild2
    |   |--grandChild3
    |   |--grandChild4
    |   |--grandChild5
    |   |--grandChild6
    |--child2

この場合、2つのディレクトリchild1とを除外しchild2、残りのディレクトリをディレクトリに移動する必要がありますchild1

使用する、

mv !(child1|child2) child1

これにより、残りのすべてのディレクトリがディレクトリに移動しchild1ます。

于 2013-08-09T09:18:57.267 に答える
5

findには除外オプションがあるため、find + xargs+mvを使用します。

find /source/directory -name ignore-directory-name -prune -print0 | xargs -0 mv --target-directory=/target/directory

これはほとんどfindmanページからコピーされていることに注意してください(mv --target-directoryを使用する方がcpioよりも優れていると思います)。

于 2011-01-06T06:08:00.043 に答える
2

これはあなたが求めていたものとは正確には異なりますが、それでうまくいくかもしれません。

mv the-folder-you-want-to-exclude somewhere-outside-of-the-main-tree
mv the-tree where-you-want-it
mv the-excluded-folder original-location

(基本的に、移動する大きなツリーから除外されたフォルダーを移動します。)

だから、私が持っていてa/、除外したい場合a/b/c/*

mv a/b/c ../c
mv a final_destination
mkdir -p a/b
mv ../c a/b/c

またはそのようなもの。そうでなければ、あなたはあなたを助けることができるかもしれませfindん。

于 2011-01-06T05:56:14.693 に答える
2

最初にファイルとフォルダの名前を取得し、必要なものを除外します。

ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...

次に、フィルタリングされmvた名前を最初のパラメーターとしてに渡し、2番目のパラメーターが宛先になります。

mv $(ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...) destination/
于 2020-09-05T05:25:58.687 に答える
1

これにより、。/exclude/ディレクトリにない現在のディレクトリ以下のすべてのファイルが/whereever...に移動します。

find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
于 2011-01-06T06:10:31.063 に答える
0
#!/bin/bash

touch apple  banana  carrot  dog  cherry

mkdir fruit

F="apple  banana  carrot  dog cherry"

mv ${F/dog/} fruit

#これにより、リストFから「dog」が削除されるため、現在のディレクトリに残り、「fruit」に移動されません。

于 2019-04-10T23:41:08.797 に答える
0
ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
于 2019-05-20T06:10:51.170 に答える
0
mv * exclude-dir

私にとって完璧な解決策でした

于 2020-01-25T14:40:28.400 に答える
0

ディレクトリの名前を変更して非表示にし、ワイルドカードに表示されないようにします。

mv specific_dir .specific_dir 
mv * ../other_dir
于 2021-05-01T00:10:14.597 に答える