Linux下批量修改文件名
参考: http://korishev.com/blog/2012/01/19/parallel-processing-with-xargs/
参考: https://www.cnblogs.com/nkwy2012/p/6362207.html
参考: https://blog.csdn.net/longxibendi/article/details/5889055
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
在Linux下, 对单个文件重命名
例如:把文件名称由: xxx.bin 修改为 xxx.asc
通常我们采用move进行重命名
mv xxx.bin xxx.asc
而对许多文件进行重命名时,我们一般就会要使用批量命令来做:
一种是使用rename命令,另外一种是使用xargs组合命令
例如:把*.bin文件名包含".bin"的部分修改为".asc"
首先明确替换的范围,所有以.bin为结尾的文件
然后替换原则,文件名中的 .bin -> .asc
1. rename实现
替换命令:
rename .bin .asc *.bin
参数:expresion,明确替换前的元素
参数:replacement,明确替换后的内容
参数 file…:要替换的文件有哪些
(注:以上为使用Centos下的验证结果,如果是ubuntu的话,可能是有差别的)
rename 的命令格式
Usage:
rename [options] expression replacement file...
Options:
-v, --verbose explain what is being done
-s, --symlink act on symlink target
-h, --help display this help and exit
-V, --version output version information and exit
2. xargs命令组合实现
xargs命令可以把许多输入提取出来一个一个执行,符合我们希望的把同样的操作对于所有符合要求的文件执行一遍。
使用xargs组合命令实现:可以有多种方式来实现功能,也可以实现一些其它的功能,例如压缩,重命名,文件夹下的递归遍历。
对这个例子而言,命令使用 与 awk 或 sed组合均可
ls *.bin | sed 's/\.bin//' | xargs -I {} mv {}.bin {}.asc
或
ls *.bin | awk -F '.' '{print $1}' | xargs -I {} mv {}.bin {}.asc
另外xargs支持多进程执行,启动多个进程去做,形如
ls *.bin | sed 's/\.bin//' | xargs -n 100 -P 4 -I {} mv {}.bin {}.asc
xargs的命令格式:
Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.
Mandatory arguments to long options are mandatory for short options too.
Non-mandatory arguments are indicated by [square brackets]
-0, --null Items are separated by a null, not whitespace.
Disables quote and backslash processing
-a, --arg-file=FILE Read arguments from FILE, not standard input
-d, --delimiter=CHARACTER Input items are separated by CHARACTER, not by
blank space. Disables quote and backslash
processing
-E END If END occurs as a line of input, the rest of
the input is ignored.
-e [END], --eof[=END] Equivalent to -E END if END is specified.
Otherwise, there is no end-of-file string
--help Print a summary of the options to xargs.
-I R same as --replace=R (R must be specified)
-i,--replace=[R] Replace R in initial arguments with names
read from standard input. If R is
unspecified, assume {}
-L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per
command line
-l Use at most one nonblank input line per
command line
-n, --max-args=MAX-ARGS Use at most MAX-ARGS arguments per command
line
-P, --max-procs=MAX-PROCS Run up to max-procs processes at a time
-p, --interactive Prompt before running commands
--process-slot-var=VAR Set environment variable VAR in child
processes
-r, --no-run-if-empty If there are no arguments, run no command.
If this option is not given, COMMAND will be
run at least once.
-s, --max-chars=MAX-CHARS Limit commands to MAX-CHARS at most
--show-limits Show limits on command-line length.
-t, --verbose Print commands before executing them
--version Print the version number
-x, --exit Exit if the size (see -s) is exceeded
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)