參考: 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)