私は疑問に思っています-特定のディレクトリ内のファイルを除くディレクトリ内のすべてのファイルを移動するにはどうすればよいですか(「mv」には「--exclude」オプションがないため)?
9 に答える
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
ます。
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よりも優れていると思います)。
これはあなたが求めていたものとは正確には異なりますが、それでうまくいくかもしれません。
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
ん。
最初にファイルとフォルダの名前を取得し、必要なものを除外します。
ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...
次に、フィルタリングされmv
た名前を最初のパラメーターとしてに渡し、2番目のパラメーターが宛先になります。
mv $(ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...) destination/
これにより、。/exclude/ディレクトリにない現在のディレクトリ以下のすべてのファイルが/whereever...に移動します。
find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
#!/bin/bash
touch apple banana carrot dog cherry
mkdir fruit
F="apple banana carrot dog cherry"
mv ${F/dog/} fruit
#これにより、リストFから「dog」が削除されるため、現在のディレクトリに残り、「fruit」に移動されません。
ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
mv * exclude-dir
私にとって完璧な解決策でした
ディレクトリの名前を変更して非表示にし、ワイルドカードに表示されないようにします。
mv specific_dir .specific_dir
mv * ../other_dir