• xargs使用技巧 —— 筑梦之路


    Unix命令都带有参数,有些命令可以接受”标准输入(stdin)”作为参数。而管道命令(|)的作用,是将左侧命令的标准输出转换为标准输入,提供给右侧命令作为参数使用。虽然,在 Unix 系统中大多数命令都不接受标准输入作为参数,只能直接在命令行输入参数,这导致无法用管道命令传递参数。比如,我们日常使用的 echo 命令就不接受管道传参。而 xargs 命令的作用,就是将标准输入转为命令行参数。

    注意:xargs 后面的默认跟的是 echo 命令,所以它可以单独使用

    1. 帮助信息
    2. xargs --help
    3. Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
    4. Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.
    5. Mandatory arguments to long options are mandatory for short options too.
    6. Non-mandatory arguments are indicated by [square brackets]
    7. -0, --null Items are separated by a null, not whitespace.
    8. Disables quote and backslash processing
    9. -a, --arg-file=FILE Read arguments from FILE, not standard input
    10. -d, --delimiter=CHARACTER Input items are separated by CHARACTER, not by
    11. blank space. Disables quote and backslash
    12. processing
    13. -E END If END occurs as a line of input, the rest of
    14. the input is ignored.
    15. -e [END], --eof[=END] Equivalent to -E END if END is specified.
    16. Otherwise, there is no end-of-file string
    17. --help Print a summary of the options to xargs.
    18. -I R same as --replace=R (R must be specified)
    19. -i,--replace=[R] Replace R in initial arguments with names
    20. read from standard input. If R is
    21. unspecified, assume {}
    22. -L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per
    23. command line
    24. -l Use at most one nonblank input line per
    25. command line
    26. -n, --max-args=MAX-ARGS Use at most MAX-ARGS arguments per command
    27. line
    28. -P, --max-procs=MAX-PROCS Run up to max-procs processes at a time
    29. -p, --interactive Prompt before running commands
    30. --process-slot-var=VAR Set environment variable VAR in child
    31. processes
    32. -r, --no-run-if-empty If there are no arguments, run no command.
    33. If this option is not given, COMMAND will be
    34. run at least once.
    35. -s, --max-chars=MAX-CHARS Limit commands to MAX-CHARS at most
    36. --show-limits Show limits on command-line length.
    37. -t, --verbose Print commands before executing them
    38. --version Print the version number
    39. -x, --exit Exit if the size (see -s) is exceeded
    1. 示例:
    2. 基本使用:
    3. # 将标准输入转为命令行参数
    4. echo "hello rumenz" | xargs echo
    5. # grep接受管道传参
    6. cat /etc/passwd | grep root
    7. # echo 命令不接受管道传参
    8. echo "hello rumenz" | echo
    9. 参数:
    10. -d 指定分隔符,默认使用空格分割
    11. # 空格作为分隔符
    12. echo "one two three" | xargs mkdir
    13. # 指定制表符为分隔符
    14. echo -e "a\tb\tc" | xargs -d "\t" echo
    15. -p 打印出要执行的命令并询问用户是否要执行
    16. echo 'one two three' | xargs -p touch
    17. -0 表示用 null 当作分隔符
    18. find命令有一个特别的参数-print0,用来指定输出的文件列表以null作为分隔符
    19. find /path -type f -print0 | xargs -0 rm
    20. # 指定多少行作为一个命令行参数
    21. xargs -L 1 find -name "*.txt"
    22. -n 指定每次将多少项作为命令行参数
    23. echo {0..9} | xargs -n 2 echo
    24. # 指定每一项命令行参数的替代字符串
    25. # 将命令行参数传给多个命令
    26. cat foo.txt
    27. one
    28. two
    29. three
    30. cat foo.txt | xargs -I {} sh -c 'echo {}; mkdir {}'
    31. one
    32. two
    33. three
    34. ls
    35. one two three
    36. # 将多行输入转换成单行输入
    37. > cat rumenz.txt
    38. 1 2 3 4
    39. 5 6
    40. 7 8 9
    41. > cat rumenz.txt | xargs
    42. 1 2 3 4 5 6 7 8 9
    43. # 将单行文本转换成多行
    44. > cat rumenz.txt
    45. 1 2 3 4 5 6 7 8 9
    46. > cat rumenz.txt | xargs -n 3
    47. 1 2 3
    48. 4 5 6
    49. 7 8 9
    50. # 指定分隔符进行行转换
    51. > echo "rumenz:123:rumenz:456:rumenz:789" | xargs -d : -n 2
    52. rumenz 123
    53. rumenz 456
    54. rumenz 789
    55. # xargs和find结合
    56. find . -type f -name "*.txt" -print | xargs rm -f
    57. # 批量下载文件
    58. cat rumenz.txt | xargs wget -c

  • 相关阅读:
    大数据批量处理神器 - 自定义周期批量消费队列的实现
    Qt5开发从入门到精通——第四篇十三节(程序启动画面 )
    后端跨域支持
    设计师必收藏的5个配色网站
    加拿大人工智能数据搜索平台【Secoda】完成1400万美元A轮融资
    5.27 picker组件
    tcpdump使用大全
    痞子衡嵌入式:简析i.MXRT1170 MECC64功能特点及其保护片内OCRAM1,2之道
    docker安装卸载及基础命令
    【车载开发系列】诊断故障码中的扩展数据
  • 原文地址:https://blog.csdn.net/qq_34777982/article/details/125262113