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

Linux Bash: Move multiple different files into same directory

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

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

我想要 car.txt、bicycle.txt、airplane.html 和 train.docx 进入车内。

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

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

如何在一行中完成此操作?

你可以做到

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)

Shorthand命令移动所有.txt文件

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

mv *.txt *.docx vehicle/ 

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

mv car.txt bicycle.txt vehicle/

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

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

分隔

以下命令可以帮助您:

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

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

两者都可以。

您可以使用mv 命令将多个文件移动到特定目录。 在您的场景中,可以通过

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

您必须注意的一点是,最后一个条目是目的地,其余所有内容,除了 mv 是来源。

另一种情况是目的地不在我们的目录中,那么我们必须选择绝对路径来代替 vehicles/。

注意:绝对路径总是从/开始,也就是说我们是从根目录开始遍历。