<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 閱讀(285) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 国产乱子伦精品免费女| 免费可以看黄的视频s色| 亚洲国产成人影院播放| 亚洲另类无码专区首页| 免费无码黄十八禁网站在线观看| 中文字幕亚洲综合精品一区| 精品一区二区三区免费| 亚洲熟妇丰满多毛XXXX| 久久成人免费播放网站| 亚洲人成网站影音先锋播放| 91精品手机国产免费| 亚洲三级在线视频| 最新中文字幕电影免费观看| 亚洲国产精品无码中文lv| 四虎永久成人免费| h视频免费高清在线观看| 亚洲国产精品无码久久SM| 免费国产黄网站在线观看视频 | 久草视频在线免费| 亚洲人成7777| 四虎永久免费观看| CAOPORN国产精品免费视频| 好看的电影网站亚洲一区| 91在线手机精品免费观看| 国产成人精品日本亚洲直接| 日产国产精品亚洲系列| 99久久99这里只有免费的精品 | 亚洲AV中文无码字幕色三| 永久在线观看www免费视频| 亚洲国产最大av| 亚洲A∨午夜成人片精品网站| a级毛片高清免费视频就| 亚洲午夜国产精品| 波多野结衣中文一区二区免费| 不卡视频免费在线观看| 亚洲理论片在线观看| 亚洲福利视频一区二区| 24小时日本电影免费看| 牛牛在线精品观看免费正| 亚洲日本国产乱码va在线观看| 亚洲成人一区二区|