Linux命令集(Linux文件管理命令--mv指令篇)

Linux文件管理命令集(mv指令篇)


如下为笔者总结出在linux中最常用的mv指令集
🌟希望能够帮助正在Linux路上奋斗的你🌟


2. mv(move)

移动或重命名文件或目录

mv filename.py /workspace/test/
#实现将文件filename.py移动至/workspace/test/路径文件夹中
mv oldfname.txt newfname.txt
#实现将oldfname旧文件名替换成newfname新文件名
短选项长选项描述
-b--在执行覆盖前,为目标文件创建一个备份
-f--force不提示用户直接强制覆盖目标文件而进行操作
-i--interactive如果目标文件已经存在,则要求用户确认是否进行覆盖操作
-n--no-clobber不覆盖任何已存在的文件,即使源文件比目标文件新
-S--为目标文件设置备份名称的拓展名
-u--update只有在源文件比目标文件新或者目标文件不存在时才进行操作
-v--verbose显示操作的详细信息
----help显示帮助信息
----version显示版本信息

下述为mv命令详细选项(--help)

Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

 ***********************************************************
 ***********************************************************
 
用法:mv [选项]... [-T] 源文件 目标文件
或 mv [选项]... 源文件... 目标目录
或 mv [选项]... -t 目标目录 源文件...

将源文件重命名为目标文件,或者将源文件移动到目标目录。

必要选项对于短选项也是必要的。

--backup[=CONTROL]         对每个现有的目标文件都进行备份
-b                         和--backup一样,但不带参数
-f, --force                在覆盖前不提示
-i, --interactive          覆盖前提示
-n, --no-clobber           不覆盖已存在的文件
如果指定多个 -i,-f,-n,则只有最后一个起作用。
--strip-trailing-slashes   从每个源文件中去除任何尾随斜杠
-S,--suffix=SUFFIX        覆盖默认备份后缀
-t,--target-directory=DIRECTORY  将所有源文件移动到目标目录
-T,--no-target-directory  把DEST视为普通文件
-u, --update               只有当源文件较新或目标文件不存在时才移动
-v, --verbose              解释正在进行的操作
-Z,--context              将目标文件的 SELinux 安全上下文设置为默认类型
--help                     显示此帮助信息并退出
--version                  输出版本信息并退出

默认情况下备份后缀为'~',除非使用 --suffix 或 SIMPLE_BACKUP_SUFFIX 设置。

可以通过 --backup 选项或 VERSION_CONTROL 环境变量来选择版本控制方法。以下是选项:

none, off       不进行备份(即使给出了--backup)
numbered, t     进行编号的备份
existing, nil   如果存在编号备份则进行编号备份,否则进行简单备份
simple, never   总是进行简单备份

GNU 核心实用程序在线帮助:http://www.gnu.org/software/coreutils/
完整的文档见:http://www.gnu.org/software/coreutils/mv
或使用 info '(coreutils) mv invocation' 在本地查看。

1. 文件移动

移动单个文件或多个文件到指定目录

mv filename_0.py filename_1.py /workspace/test/

2. 递归移动目录

包括子目录中的文件或目录全部移动到新目录

mv -r src_directory/ target_directory/

3. 文件目录重命名

更改文件或目录的名称

mv old_name.py new_name.py

4. 强制移动

文件名重合会覆盖目标文件而不提示

mv -f src_file.txt target_directory/

5. 备份覆盖的目标文件

mv -b src_file.txt target_directory/

6. 试探性移动

当文件名重合时不覆盖目标文件

mv -n src_file.txt target_directory/

7. 显示移动进度

mv -v src_file.txt target_directory/

8. 补集操作

把所有在 source 目录(src)中但不在 target 目录中的文件移动到 target 目录中

for file in `ls src`; do mv $file target/; done

9. 修改文件的权限、属主和时间戳等属性

mv --preserve=mode,ownership,timestamps src_file.txt target_directory/


  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是你呀星途

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值