Linux Bash:将多个不同的文件移动到同一目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41277071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:33:56  来源:igfitidea点击:

Linux Bash: Move multiple different files into same directory

linuxbashmv

提问by Conner

As a rather novice Linux user, I can't seem to find how to do this. I am trying to move unique files all in one directory into another directory. Example:

作为一个相当新手的 Linux 用户,我似乎无法找到如何做到这一点。我正在尝试将一个目录中的所有文件移动到另一个目录中。例子:

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

I want car.txt, bicycle.txt, airplane.html, and train.docx inside vehicle.

我想要车内的 car.txt、bike.txt、airplane.html 和 train.docx。

Right now I do this by moving the files individually:

现在我通过单独移动文件来做到这一点:

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

How can I do this in one line?

我怎样才能在一行中做到这一点?

回答by Richard

You can do

你可以做

mv car.txt bicycle.txt vehicle/

(Note that the /above is unnecessary, I include it merely to ensure that vehicleis a directory.)

(请注意,/以上是不必要的,我包含它只是为了确保它vehicle是一个目录。)

You can test this as follows:

您可以按如下方式进行测试:

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)

回答by Opster Elasticsearch Ninja

Shorthand command to move all .txt file

移动所有 .txt 文件的速记命令

You can try using a wildcard. In the code below, *will match all the files which have any name ending with .txtor .docx, and move them to the vehicle folder.

您可以尝试使用通配符。在下面的代码中,*将匹配所有名称以.txt或结尾的文件.docx,并将它们移动到车辆文件夹。

mv *.txt *.docx vehicle/ 

If you want to move specific files to a directory

如果要将特定文件移动到目录

mv car.txt bicycle.txt vehicle/

Edit:As mentioned in a comment, If you are moving files by hand, I suggest using mv -i ...which will warn you in case the destination file already exists, giving you a choice of not overwriting it. Other 'file destroyer' commands like cp & rm too have a -ioption

编辑:如评论中所述,如果您手动移动文件,我建议使用mv -i ...which 会在目标文件已经存在的情况下警告您,让您选择不覆盖它。其他“文件销毁器”命令,如 cp & rm 也有一个-i选项

回答by Ankit Kumar Singh

mvcommand in linux allow us to move more than one file into another directory. All you have to do is write the name of each file you want to move, seperated by a space.

mvlinux 中的命令允许我们将多个文件移动到另一个目录中。您所要做的就是写下您要移动的每个文件的名称,并以space.

Following command will help you:

以下命令将帮助您:

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

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

or

或者

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

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

both of them will work.

他们都将工作。