本文共 3368 字,大约阅读时间需要 11 分钟。
linux基本命令示例
In this guide, you will learn how to use the Linux/Unix mv
command. Linux mv command has 2 distinct functionalities depending on how it’s used.
在本指南中,您将学习如何使用Linux / Unix mv
命令。 Linux mv命令根据其用法有2种不同的功能。
To rename a file using mv, the 2 files (The original and renamed file) must be in the same location
The syntax for renaming a file or a directory is as follows要使用mv重命名文件,两个文件(原始文件和重命名文件)必须位于同一位置
重命名文件或目录的语法如下# mv file_name new_file_name
For example, I have a file called linux-for-beginners
and I want to rename it to unix-for-beginners
例如,我有一个名为linux-for-beginners
的文件,我想将其重命名为unix-for-beginners
# mv linux-for-beginners unix-for-beginners
You can further verify that the file has been renamed using the ls
command. The output is shown below
您可以使用ls
命令进一步验证文件是否已重命名。 输出如下所示
As observed, the original file has been renamed. The command and syntax remain the same for directories.
如图所示,原始文件已被重命名。 目录的命令和语法保持不变。
Having looked at how to rename a file /directory, let’s see how you can move a file from one directory to another.
看了如何重命名文件/目录后,让我们看看如何将文件从一个目录移动到另一个目录。
The syntax for this is as follows
语法如下
# mv [option] file /path/to/destination/directory
For example , to move the file unix-for-beginners
to the /opt
directory, run
例如,要将文件unix-for-beginners
移至/opt
目录,请运行
$ mv unix-for-beginners /opt
The syntax remains the same for moving directories. In the example below , the directory tutorials
is being moved to /opt
directory
移动目录的语法相同。 在下面的示例中,目录tutorials
被移至/opt
目录
The mv command can be accompanied by a variety of options. Let’s have a look
mv命令可以带有多种选项。 我们来看一下
This option prompts the user for confirmation before moving a file that would overwrite a pre-existing file with the same name. In this case, the user must press y to confirm or overwrite.
此选项会提示用户进行确认,然后再移动将覆盖同名现有文件的文件。 在这种情况下,用户必须按y确认或覆盖。
Sample output
样品输出
The -n
option prevents a file from being overwritten. That is , you can rename a file to match another file and still keep the contents of the pre-existing file. In this example, we have 2 files file1.txt
and file2.txt
. We are going to rename file1.txt
to file2.txt
using the -n
attribute but still retain the contents of file2.txt
-n
选项可防止文件被覆盖。 即,您可以重命名文件以匹配另一个文件,并且仍然保留先前存在的文件的内容。 在此示例中,我们有2个文件file1.txt
和file2.txt
。 我们将使用-n
属性将file1.txt
重命名为file2.txt
,但仍保留file2.txt
的内容
Sample output
样品输出
the -v
option displays the status of the activity taking place when mv
is running. The output of the activity is printed on the terminal as shown
-v
选项显示mv
运行时活动的状态。 活动的输出显示在终端上,如图所示
Sample output
样品输出
The -u
option updates the destination file only if the source file has newer content or the destination file is missing.
-u
选项仅在源文件具有较新内容或缺少目标文件时更新目标文件。
To check the version of mv
run the command
要检查mv
的版本,请运行以下命令
$ mv --version
Sample output
样品输出
For more mv command options run the command
有关更多的mv命令选项,请运行命令
$ mv --help
Sample output
样品输出
翻译自:
linux基本命令示例
转载地址:http://onlzd.baihongyu.com/