<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日韩综合一区久热| 亚洲黄色网站视频| 免费国产高清毛不卡片基地 | 三年片在线观看免费西瓜视频| 国产在线播放免费| 看免费毛片天天看| 日产国产精品亚洲系列| 成人免费网站视频www| 亚洲精品亚洲人成在线观看下载| 黄页免费视频播放在线播放| 亚洲一区二区三区国产精品| 成人无码精品1区2区3区免费看| 亚洲色精品88色婷婷七月丁香| 七色永久性tv网站免费看| 亚洲色欲色欲综合网站| 国产成人免费网站| 亚洲av乱码一区二区三区按摩 | 在线精品免费视频| 日韩免费码中文在线观看| 亚洲人成影院在线无码按摩店| 免费在线中文日本| 亚洲精品国产精品国自产网站 | 毛片免费全部播放一级| 苍井空亚洲精品AA片在线播放 | 亚洲av无码av制服另类专区| 精品无码人妻一区二区免费蜜桃| 亚洲国语在线视频手机在线| 日本黄页网站免费| a级毛片免费观看网站| 7777久久亚洲中文字幕蜜桃| 最好免费观看韩国+日本| 一区二区三区免费高清视频| 久久国产亚洲高清观看| 麻豆国产人免费人成免费视频| 亚美影视免费在线观看| 亚洲成人在线免费观看| 亚洲第一页日韩专区| 18未年禁止免费观看| 黄色毛片免费在线观看| 亚洲视频手机在线| 亚洲精品无码久久久|