如何重命名Bash中的文件
时间:2020-01-09 10:37:24 来源:igfitidea点击:
如何在UNIX/macOS(OS X)/Linux/BSD操作系统下的bash中重命名文件?
您需要使用mv命令或重命名命令来重命名bash shell中的文件。
使用mv重命名bash中的文件
我们需要使用以下mv命令语法将SOURCE文件分配给DESTINATION文件:
mv oldname newname mv SOURCE DEST mv olddir newdir mv old-file new-file
将SOURCE重命名为DEST,或将SOURCE移至DIRECTORY
将名为/tmp/foo的文件重命名为/tmp/bar
键入以下命令(打开终端并发出以下命令):
# create /tmp/foo touch /tmp/foo ls -l /tmp/foo mv /tmp/foo /tmp/bar ls -l /tmp/bar ls -l /tmp/foo
ls命令列出了当前工作目录或Linux或类Unix系统中给定目录中的文件。
重命名提供的目录
键入以下命令:
mv offfer offers ## or tell us what mv is doing by passing the -v option ## mv -v offfer offers
覆盖前提示
-i选项是交互式文件处理选项。
在移动将覆盖现有文件的文件之前,您会收到一条错误消息。
如果来自用户的响应以字符y或Y开头,则尝试移动/重命名。
touch /tmp/test mv -i /tmp/test /tmp/bar
输出示例:
mv: overwrite `/tmp/bar'? y
-u选项
仅当SOURCE文件比目标文件新或缺少目标文件时,-u选项才会移动:
mv -u data.txt /mnt/floppy/backup.txt
-v选项
-v选项说明正在执行的操作:
mv -v /tmp/bar /tmp/output.txt
输出示例:
`/tmp/bar' -> `/tmp/output.txt'
重命名多个文件
使用重命名命令可重命名多个文件。
例如,将所有* .perl文件重命名为* .pl,输入:
rename .perl .pl *.perl
有关更多详细信息,请参见如何在shell提示符下重命名多个文件。
所有mv命令选项的摘要
要在bash中重命名文件,我们使用mv命令:
- ``-v`:详细选项。换句话说,显示文件在bash shell中被移动或重命名时的进度
- ``-i`:在覆盖文件之前提示
- ``-u`:仅当SOURCE文件比目标文件新或bash shell中缺少目标文件时移动
- ``-f`:在覆盖文件之前不提示
要查看所有mv命令选项的列表,请键入:
man mv 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, --interact我有 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 g我有n)
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/>
Report mv translation bugs to <http://translationproject.org/team/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

