在linux shell脚本中如何重命名文件

时间:2019-11-20 08:53:02  来源:igfitidea点击:

在UNIX/macOS(OS X)/Linux/BSD操作系统,如何在bash shell中重命名文件?

bash中使用mv重命名文件

可以使用以下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

# 创建一个文件 /tmp/foo 
touch /tmp/foo
ls -l /tmp/foo
mv /tmp/foo /tmp/bar
ls -l /tmp/bar
ls -l /tmp/foo

示例:重命名目录

mv offfer offers
## 参数 -v 显示mv的操作信息  ##
mv -v offfer offers

示例:在覆盖前进行提示

-i选项用于交互式处理文件。

在覆盖现有文件的文件之前,用户会收到一条错误消息。如果用户输入以y/Y开头,则尝试移动/重命名。

touch /tmp/test
mv -i /tmp/test /tmp/bar

输出示例:

mv: overwrite `/tmp/bar'? y

mv -u选项

仅当源文件比目标文件新或者缺少目标文件时,-u选项才会移动:

mv -u data.txt /mnt/floppy/backup.txt

mv -v选项

-v选项说明正在执行的操作:

mv -v /tmp/bar /tmp/output.txt

输出示例:

`/tmp/bar' -> `/tmp/output.txt'

示例:重命名多个文件

使用rename命令可重命名多个文件。例如,将所有* .perl文件重命名为* .pl,执行:

rename .perl .pl *.perl

有关更多详细信息,请参见如何在shell提示符下重命名多个文件。

mv命令帮助

要查看所有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, --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/>
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'