web-dev-qa-db-ja.com

Linux Bash:複数の異なるファイルを同じディレクトリに移動します

初心者のLinuxユーザーとして、私はこれを行う方法を見つけることができないようです。 1つのディレクトリ内のすべての一意のファイルを別のディレクトリに移動しようとしています。例:

$ ls
vehicle car.txt bicycle.txt airplane.html train.docx (more files)

車内にcar.txt、bicycle.txt、planet.html、train.docxが必要です。

今私はファイルを個別に移動することでこれを行います:

$ mv car.txt vehicle
$ mv bicycle.txt vehicle
...

これを1行で行うにはどうすればよいですか?

10
Conner

できるよ

mv car.txt bicycle.txt vehicle/

/上記は不要です。単にvehicleがディレクトリであることを確認するために含めています。)

これは次のようにテストできます。

cd               #Move to home directory
mkdir temp       #Make a temporary directory
touch a b c d    #Make test (empty) files ('touch' also updates the modification date of an existing file to the current time)
ls               #Verify everything is there
mv a b c d temp/ #Move files into temp
ls               #See? They are gone.
ls temp/         #Oh, there they are!
rm -rf temp/     #DESTROY (Be very, very careful with this command)
16
Richard

Linuxのmvコマンドを使用すると、複数のファイルを別のディレクトリに移動できます。移動する各ファイルの名前をspaceで区切って書き込むだけです。

次のコマンドが役立ちます:

mv car.txt bicycle.txt airplane.html train.docx vehicle

または

mv car.txt bicycle.txt airplane.html train.docx vehicle/

どちらも機能します。

2

ワイルドカードを使用してみてください。以下のコードでは、「*」は.txtまたは.docxで終わる名前を持つすべてのファイルに一致し、それらをVehicleフォルダーに移動します。

mv *.txt *.docx vehicle/ 
2
Amit Khandelwal