linux mv重命名文件夹,mv(移动并重命名文件和目录)_语法_示例_Unix&Linux命令_uc电脑园官网...

mv myfile.txt myfiles

Move the file myfile.txt into the directory myfiles. If myfiles is a file, it will be overwritten. If the file is marked as read-only, but you own the file, you will be prompted before overwriting it.

mv myfiles myfiles2

If myfiles is a file or directory, and myfiles2 is a directory, move myfiles into myfiles2. If myfiles2 does not exist, the file or directory myfiles is renamed myfiles2.

mv myfile.txt ../

Move the file myfile.txt into the parent directory of the current directory.

mv -t myfiles myfile1 myfile2

Move the files myfile1 and myfile2 into the directory myfiles.

mv myfile1 myfile2 myfiles

Same as the previous command.

mv -n file file2

If file2 exists and is a directory, file is moved into it. If file2 does not exist, file is renamed file2. If file2 exists and is a file, nothing happens.

mv -f file file2

If file2 exists and is a file, it will be overwritten.

mv -i file file2

If file2 exists and is a file, a prompt is given:

mv: overwrite 'file2'?

Entering "y", "yes", "Yes", or "Y" will result in the file being overwritten. Any other input will skip the file.

mv -fi file file2

Same as mv -i. Prompt before overwriting. The f option is ignored.

mv -if file file2

Same as mv -f. Overwrite with no prompt. the i option is ignored.

mv My\ file.txt My\ file\ 2.txt

Rename the file "My file.txt" to "My file 2.txt". Here, the spaces in the file name are escaped, protecting them from being interpreted as part of the command.

mv "My file.txt" "My file 2.txt"

Same as the previous command.

mv "My file.txt" myfiles

The result of this command:

If myfiles a directory, My file.txt is moved into myfiles.

If myfiles a file, My file.txt is renamed myfiles, and the original myfiles is overwritten.

If myfiles does not exist, My file.txt is renamed myfiles.

mv My*.txt myfiles

Here, * is a wildcard meaning "any number, including zero, of any character."

If myfiles is a directory: all files with the extension .txt, whose name begins with My, will be moved into myfiles.

If myfiles does not exist or is not a directory, mv reports an error and does nothing.

my My\ file??.txt myfiles

Here, ? is a wildcard that means "zero or one of any character." It's used twice, so it can match a maximum of two characters.

If myfiles is a directory: any file with zero, one, or two characters between My file and .txt in their name is moved into myfiles.

If myfiles doesn't exist, or is not a directory, mv reports an error and does nothing.

Making backups

mv -b file file2

If file2 exists, it will be renamed to file2~.

mv -b --suffix=.bak file file2

If file2 exists, it will be renamed to file2.bak.

mv --backup=numbered; mv file file2

If file2 exists, it will be renamed file2.~1~. If file2.~1~ exists, it will be renamed file2.~2~, etc.

VERSION_CONTROL=numbered mv -b file file2

Same as previous command. The environment variable is defined for this command only.

export VERSION_CONTROL=numbered; mv -b file file2

By exporting the VERSION_CONTROL environment variable, all mv -b commands for the current session will use numbered backups.

export VERSION_CONTROL=numbered; mv file file2

Even though the VERSION_CONTROL variable is set, no backups are created because -b (or --backup) was not specified. If file2 exists, it is overwritten.

Renaming files using regular expressions

mv does not interpret regular expressions (regex).

If you need to rename many files, using a complex or nuanced mapping from old to new file names, you should use the rename command instead.

rename accepts perl regular expressions. For example:

rename 's/My\ file(..)/document$1/' My*

This command will rename files My file.txt and My file 2.txt to document.txt and document 2.txt.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值