<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    xargs: How To Control and Use Command Line Arguments

    am trying to use xargs command using shell pipes and not able to understand how to control and use command line arguments. For example I'd like to find out all *.c file located in 100s of sub-directories and move them to another directory called ~/old.src. How do I use command line args with xargs to achieve the same?

    xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

    xargs is more safer and easy to use

    xargs functionality can be achived using the backquote feature of shell. But, it offers more options. It can deal with blanks or special characters in file names easily. It is often used with find, grep and other commands.

    xargs examples

    For example following example will print 1 2 3 4 using xargs (echo command is default)
    $ echo 1 2 3 4 | xargs echo
    OR
    $ echo 1 2 3 4 | xargs
    You can force xargs to use at most max-args arguments per command line. For example following will use first two argument per command:
    $ echo 1 2 3 4 | xargs -n 2
    Find all .bak files in or below the current directory and delete them.
    $ find . -name "*.bak" -type f -print | xargs /bin/rm -f

    {} as the argument list marker

    {} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .bak files in or below the current directory and move them to ~/.old.files directory:
    $ find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files
    You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
    $ find . -name "*.bak" -print0 | xargs -0 -I file mv file ~/old.files
    Where,

    1. -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.
    2. -I Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.

    Dealing file names with blank spaces and newline

    The following will work incorrectly if there are any filenames containing newlines or spaces (it will find out all .mp3 file located in current directory and play them using mplayer):
    $ find . -iname "*.mp3" -print | xargs mplayer
    To get rid of this problem use -0 option:
    $ find . -iname "*.mp3" -print0 | xargs -0 -I mp3file mplayer mp3file
    To find out all *.c file located in 100s of subdirectories and move them to another directory called ~/old.src, use:
    $ find /path/to/dir -iname "*.c" -print0 | xargs -0 -I file mv file ~/old.src

    Avoiding errors and resource hungry problems with xargs and find combo

    To copy all media files to another location called /bakup/iscsi, you can use cp as follows:
    $ cp -r -v -p /share/media/mp3/ /backup/iscsi/mp3
    However, cp command may fail if an error occurs such as if the number of files is too large for the cp command to handle. xargs in combination with find can handle such operation nicely. xargs is more resource efficient and will not halt with an error:

    $ find /share/media/mp3/ -type f -name "*.mp3" -print0 | xargs -0 -r -I file cp -v -p file --target-directory=/bakup/iscsi/mp3

    Please note that all of the above commands are tested with GNU/xargs version. BSD and UNIX xargs command may not have options such as -r. Please refer to your local xargs man page for further info:
    man xargs



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-06-19 02:08 Chan Chen 閱讀(281) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 亚洲AV无码一区二区三区网址| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 成人黄18免费视频| 国产视频精品免费| 久久夜色精品国产嚕嚕亚洲av| 亚洲AV永久无码精品一百度影院| 国产成人无码精品久久久久免费| 五月婷婷在线免费观看| 四虎永久免费观看| 亚洲xxxxxx| 久久一区二区免费播放| 毛片在线免费视频| 久久精品国产亚洲AV果冻传媒| 成人影片一区免费观看| 在线观看免费高清视频| 久久99国产亚洲精品观看| 久草免费手机视频| 中文字幕第13亚洲另类| 亚洲日韩中文字幕无码一区| 99久热只有精品视频免费看| 亚洲精品tv久久久久久久久久| 67194在线午夜亚洲| 8090在线观看免费观看| 亚洲AV无码第一区二区三区| 久久国产乱子免费精品| 亚洲一级毛片免费看| 日韩免费视频播放| 亚洲午夜成人精品无码色欲| 日韩免费高清大片在线| 亚洲AV无码国产精品色| 亚洲av区一区二区三| 亚洲成a人无码亚洲成www牛牛 | 国产成人综合亚洲| 女人18毛片免费观看| 国产成人精品亚洲日本在线| 精品免费国产一区二区| 亚洲看片无码在线视频| 免费看黄视频网站| 亚洲一区精品视频在线| 亚洲国产精品无码久久九九| 四虎影视在线看免费观看|