<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

    主站蜘蛛池模板: 性做久久久久久免费观看| 青青操视频在线免费观看| 巨波霸乳在线永久免费视频| 亚洲色偷拍另类无码专区| 无码免费又爽又高潮喷水的视频| 午夜网站免费版在线观看| 国产精品高清视亚洲一区二区| 亚洲级αV无码毛片久久精品| 一进一出60分钟免费视频| 亚洲国产精品不卡毛片a在线| 免费无码又爽又黄又刺激网站| 免费a在线观看播放| kk4kk免费视频毛片| 亚洲AV综合色一区二区三区| 日韩免费无码视频一区二区三区 | 91福利免费视频| 精品日韩亚洲AV无码一区二区三区| 色猫咪免费人成网站在线观看 | 亚洲卡一卡2卡三卡4麻豆| 99精品国产免费久久久久久下载| 亚洲深深色噜噜狠狠网站| 国产高清视频在线免费观看| 成人a毛片免费视频观看| 亚洲精品高清国产一线久久| 91精品视频在线免费观看| 色老板亚洲视频免在线观| 免费在线观看a级毛片| 热久久这里是精品6免费观看| 精品无码一区二区三区亚洲桃色 | 亚洲国产精品无码av| 在线视频精品免费| 噜噜噜亚洲色成人网站| 亚洲人成精品久久久久| 精品无码免费专区毛片| 国产AV无码专区亚洲AV蜜芽| 亚洲精品成人片在线观看精品字幕 | 久久亚洲高清观看| 免费看国产精品3a黄的视频 | 国产AV无码专区亚洲AV蜜芽| 国产l精品国产亚洲区在线观看| 无码精品A∨在线观看免费|