{"id":158,"date":"2014-06-26T03:25:28","date_gmt":"2014-06-26T03:25:28","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=158"},"modified":"2014-06-26T03:25:28","modified_gmt":"2014-06-26T03:25:28","slug":"%e4%b8%80%e8%a1%8c%e5%91%bd%e4%bb%a4%e8%a7%a3%e5%86%b3%e6%9f%a5%e6%89%be%e7%b3%bb%e7%bb%9f%e4%b8%ad%e6%89%80%e6%9c%89%e7%9a%84mp3%e6%96%87%e4%bb%b6%e5%b9%b6%e5%b0%86%e5%85%b6%e7%a7%bb%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/158.html","title":{"rendered":"\u4e00\u884c\u547d\u4ee4\u89e3\u51b3:\u67e5\u627e\u7cfb\u7edf\u4e2d\u6240\u6709\u7684MP3\u6587\u4ef6\u5e76\u5c06\u5176\u79fb\u52a8\u81f3\u67d0\u4e00\u65b0\u7684\u76ee\u5f55\u4e2d"},"content":{"rendered":"<div>\n<h1 class=\"entry-title\" style=\"color: #111111;\">Finding All .mp3 Files And Move To a New Directory From A Shell Prompt<\/h1>\n<\/div>\n<div>\n<p style=\"color: #111111;\">have mp3 music files all over my file system. I would like to move them into specific directory called \/mnt\/mp3. So how do you find and move all mp3 files to \/mnt\/mp3 directory on Linux or Unix-like system?<br \/>\n<span id=\"more-1170\"><\/span><br \/>\nSimply use the find command. It locates all files and then executes a command to move each mp3 file to \/mnt\/mp3 directory.<\/p>\n<p style=\"color: #111111;\"><span style=\"color: #ff0000;\"><strong>Warning: Backup all your data before you type the following commands.\u3010\u64cd\u4f5c\u4e4b\u524d\u5148\u5907\u4efd\uff01\u3011<\/strong><\/span><\/p>\n<h2 style=\"color: #111111;\">Step # 1: Finding all your .mp3 files<\/h2>\n<p style=\"color: #111111;\">The following command will just find all your .mp3 files using the find command:<br \/>\n<code># find \/ -iname \"*.mp3\" -print<\/code><br \/>\nWhere,<\/p>\n<ol style=\"color: #111111;\">\n<li><kbd><strong>\/<\/strong><\/kbd>\u00a0&#8211; Search \/ root directory<\/li>\n<li><kbd><strong>-iname<\/strong><\/kbd>\u00a0&#8211; File name search pattern (case insensitive)<\/li>\n<li><kbd><strong>-print<\/strong><\/kbd>\u00a0&#8211; Display name of files on screen<\/li>\n<\/ol>\n<h2 style=\"color: #111111;\">Step # 2: Finding and moving all your .mp3 files in one single pass<\/h2>\n<p style=\"color: #111111;\">Type the following command to find and move all files using\u00a0<a style=\"color: #2361a1;\" title=\"See Linux\/Unix mv command examples for more info\" href=\"http:\/\/www.cyberciti.biz\/faq\/unix-mv-command-examples\/\">mv command<\/a>\u00a0to \/mnt\/mp3 directory:<br \/>\n<code># find \/ -iname \"*.mp3\" -exec mv {} \/mnt\/mp3 ;<\/code><br \/>\nWhere,<\/p>\n<ul style=\"color: #111111;\">\n<li><kbd><strong>-exec mv {} \/mnt\/mp3 ;<\/strong>\u00a0<\/kbd>: Execute mv command.\uff08<span style=\"color: #ff0000; font-size: medium;\">\u5b57\u7b26\u4e32&#8221;{}&#8221;\u6700\u540e\u4f1a\u88ab\u5339\u914d\u51fa\u7684\u6587\u4ef6\u540d\u66ff\u6362\uff1b\u800c\u5b57\u7b26\u4e32&#8221;;&#8221;\u5219\u8868\u793amv\u547d\u4ee4\u7ed3\u675f<\/span>\uff09<\/li>\n<li><span style=\"color: #ff0000; font-size: large;\">The string &#8216;{}&#8217; is replaced by the file name<\/span><\/li>\n<li><span style=\"color: #ff0000; font-size: large;\">; ends \/bin\/mv command<\/span><\/li>\n<\/ul>\n<p style=\"color: #111111;\">To just move .mp3 files and not directories, use:<br \/>\n<code># find \/ -iname \"*.mp3\"\u00a0<span style=\"color: #ff0000;\">-type f<\/span>\u00a0-exec \/bin\/mv {} \/mnt\/mp3 ;<\/code><\/p>\n<p style=\"color: #111111;\">Find all directories having name mp3 and move:<br \/>\n<code># find \/ -iname \"*.mp3\"\u00a0<span style=\"color: #ff0000;\">-type d<\/span>\u00a0-exec \/bin\/mv {} \/mnt\/mp3 ;<\/code><\/p>\n<p style=\"color: #111111;\">For performance you may need to consider using xargs command\uff08<span style=\"color: #ff0000; font-size: medium;\">\u8fd9\u91cc\u6dfb\u52a0\u4e86xargs\u7684 &#8220;-I&#8221; \u9009\u9879\uff0c\u9632\u6b62\u5f53\u5339\u914d\u51fa\u7684\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u7a7a\u683c\u65f6\u540e\u9762\u7684\u547d\u4ee4\u6267\u884c\u51fa\u73b0\u9519\u8bef\uff0c\u4ee5\u6362\u884c\u7b26\u4f5c\u4e3a\u4f20\u5165\u53c2\u6570\u7684\u7ed3\u675f\u6807\u5fd7<\/span>\uff09:<br \/>\n<code>find \/ -iname \"*.mp3\" -type f | xargs -I '{}' mv {} \/mnt\/mp3<\/code><\/p>\n<p style=\"color: #111111;\">Sp to moves all .mp3 files with special characters in its name such as white spaces try\uff08<span style=\"color: #ff0000; font-size: medium;\">xargs\u547d\u4ee4\u7684-0\u53c2\u6570\u662f\u4e3a\u4e86\u9632\u6b62\u5339\u914d\u51fa\u7684\u6587\u4ef6\u540d\u4e2d\u542b\u6709\u7279\u6b8a\u5b57\u7b26\u800c\u51fa\u73b0\u9519\u8bef\uff0c\u8fd9\u4e2a\u9009\u9879\u4e00\u822c\u548cfind\u547d\u4ee4\u7684-print0\u9009\u9879\u4e00\u8d77\u4f7f\u7528<\/span>\uff09:<br \/>\n<code>find \/ -iname \"*.mp3\" -type f -print0 | xargs -0 -I '{}' \/bin\/mv \"{}\" \/mnt\/mp3\/<\/code><\/p>\n<h2 style=\"color: #111111;\">Final solution\uff08\u4f7f\u7528rsync\u66ff\u6362\u7b80\u5355\u7684mv\u547d\u4ee4\uff09<\/h2>\n<p style=\"color: #111111;\">Above commands will not maintain sub-directory structure. <span style=\"color: #ff0000;\">Try replacing mv with rsync to use the same directory structure on the target directory with the\u00a0<kbd>-R<\/kbd>\u00a0option<\/span>:<br \/>\n<code>find \/ -iname \"*.mp3\" -type f -print0 | xargs -0 -I '{}' \/usr\/bin\/rsync -avR \"{}\" \/mnt\/mp3\/<\/code><\/p>\n<p style=\"color: #111111;\">You can also write a script that moves files along with directories. This is also useful to\u00a0<em>move all files to mp3 player<\/em>that has been mounted on \/mnt\/mp3 directory.<\/p>\n<ul style=\"color: #111111;\">\n<li><a style=\"color: #2361a1;\" href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-bsd-xargs-construct-argument-lists-utility\/\">xargs: How To Control and Use Command Line Arguments<\/a><\/li>\n<li>Man pages &#8211;\u00a0<a style=\"color: #2361a1;\" title=\"See find(1) linux man page for more information and examples\" href=\"http:\/\/www.manpager.com\/linux\/man1\/find.1.html\">find(1)<\/a>,\u00a0<a style=\"color: #2361a1;\" title=\"See xargs(1) linux man page for more information and examples\" href=\"http:\/\/www.manpager.com\/linux\/man1\/xargs.1.html\">xargs(1)<\/a>,\u00a0<a style=\"color: #2361a1;\" title=\"See mv(1) linux man page for more information and examples\" href=\"http:\/\/www.manpager.com\/linux\/man1\/mv.1.html\">mv(1)<\/a><\/li>\n<\/ul>\n<\/div>\n<div><span style=\"color: #000000; font-size: medium;\">Finding All .mp3 Files And Move To a New Directory From A Shell Prompt &#8211; nixCraft <a href=\"http:\/\/www.cyberciti.biz\/tips\/howto-linux-unix-find-move-all-mp3-file.html\">http:\/\/www.cyberciti.biz\/tips\/howto-linux-unix-find-move-all-mp3-file.html<\/a><\/span><\/div>\n<div><span style=\"color: #000000; font-size: medium;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/span><\/div>\n<div>\n<h1 class=\"entry-title\" style=\"color: #111111;\">xargs: How To Control and Use Command Line Arguments<\/h1>\n<\/div>\n<div>\n<p style=\"color: #111111;\">I 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&#8217;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?<br \/>\n<span id=\"more-3309\"><\/span><br \/>\nxargs 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.<\/p>\n<h2 style=\"color: #111111;\">xargs is more\u00a0<span style=\"color: #6666cc;\">safer and easy<\/span>\u00a0to use<\/h2>\n<p style=\"color: #111111;\">xargs functionality can be achived using the backquote feature of shell. But, it offers more options. It can deal with <strong><span style=\"color: #ff0000;\">blanks or special characters<\/span><\/strong> in file names easily. It is often used with find, grep and other commands.<\/p>\n<h2 style=\"color: #111111;\">xargs examples<\/h2>\n<p style=\"color: #111111;\">For example following example will print 1 2 3 4 using xargs (echo command is default)<br \/>\n<code>$ echo 1 2 3 4 | xargs echo<\/code><br \/>\nOR<br \/>\n<code>$ echo 1 2 3 4 | xargs<\/code><br \/>\nYou can force xargs to use at most max-args arguments per command line. For example following will use first two argument per command:<br \/>\n<code>$ echo 1 2 3 4 | xargs -n 2<\/code><br \/>\nFind all .bak files in or below the current directory and delete them.<br \/>\n<code>$ find . -name \"*.bak\" -type f -print | xargs \/bin\/rm -f<\/code><\/p>\n<h2 style=\"color: #111111;\"><span style=\"color: #993399;\">{} as the argument list marker<\/span><\/h2>\n<p style=\"color: #111111;\"><span style=\"color: #ff0000; font-size: large;\">{} is the default argument list marker.\uff08{}\u662f\u9ed8\u8ba4\u7684\u53c2\u6570\u5217\u8868\u6807\u5fd7\uff09<\/span> 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:<br \/>\n<code>$ find . -name \"*.bak\" -print0 | xargs -0 -I {} mv {} ~\/old.files<\/code><br \/>\n<span style=\"color: #ff0000; font-size: large;\">You can rename {} to something else.\uff08\u6211\u4eec\u53ef\u4ee5\u5bf9{}\u8fdb\u884c\u91cd\u547d\u540d\uff09<\/span> In the following example {} is renamed as file. This is more readable as compare to previous example:<br \/>\n<code>$ find . -name \"*.bak\" -print0 | xargs -0 -I file mv file ~\/old.files<\/code><br \/>\nWhere,<\/p>\n<ol style=\"color: #111111;\">\n<li><strong>-0<\/strong>\u00a0If there are blank spaces or characters (including newlines) many commands will not work. <span style=\"color: #ff0000; font-size: medium;\"><strong>This option take cares of file names with blank space.<\/strong><\/span><\/li>\n<li><strong>-I\u00a0<\/strong>Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, <strong><span style=\"color: #ff0000;\">unquoted blanks<\/span><\/strong> do not terminate input items; instead the separator is the newline character.\uff08<span style=\"color: #ff0000; font-size: medium;\">-I\u9009\u9879\u8bbe\u7f6e\u540e\uff0c\u53c2\u6570\u540d\u4e2d\u6700\u672b\u5c3e\u7684\u7a7a\u767d\u7b26\u5c06\u4e0d\u518d\u7528\u4e8e\u7ec8\u6b62\u8f93\u5165\uff0c\u53c2\u6570\u5217\u8868\u5206\u9694\u7b26\u88ab\u66ff\u6362\u6210\u4e86\u6362\u884c\u7b26<\/span>\uff09<\/li>\n<\/ol>\n<h3 style=\"color: #111111;\">Dealing\u00a0<span style=\"color: #009900;\">file names with blank spaces and newline<\/span><\/h3>\n<p style=\"color: #111111;\">The following will work incorrectly if there are any filenames containing newlines or spaces \uff08<strong><span style=\"color: #ff0000;\">\u5f53\u6587\u4ef6\u540d\u4e2d\u5305\u542b\u6362\u884c\u7b26\u548c\u7a7a\u683c\u65f6\u8be5\u547d\u4ee4\u6267\u884c\u5c06\u4f1a\u51fa\u9519<\/span><\/strong>\uff09(it will find out all .mp3 file located in current directory and play them using mplayer):<br \/>\n<code>$ find . -iname \"*.mp3\" -print | xargs mplayer<\/code><br \/>\nTo get rid of this problem use -0 option\uff08\u53ef\u4ee5\u548cfind\u547d\u4ee4\u4e2d\u7684-print0\u9009\u9879\u8054\u5408\u4f7f\u7528\u4ee5\u907f\u514d\u9519\u8bef\uff09:<br \/>\n<code>$ find . -iname \"*.mp3\" -print0 | xargs -0 -I mp3file mplayer mp3file<\/code><br \/>\nTo find out all *.c file located in 100s of subdirectories and move them to another directory called ~\/old.src, use:<br \/>\n<code>$ find \/path\/to\/dir -iname \"*.c\" -print0 | xargs -0 -I file mv file ~\/old.src<\/code><\/p>\n<h2 style=\"color: #111111;\">Avoiding\u00a0<span style=\"color: #ff0000;\">errors and resource hungry problems<\/span>\u00a0with xargs and find combo<\/h2>\n<p style=\"color: #111111;\">To copy all media files to another location called \/bakup\/iscsi, you can use cp as follows:<br \/>\n<code>$ cp -r -v -p \/share\/media\/mp3\/ \/backup\/iscsi\/mp3<\/code><br \/>\nHowever, 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\uff08\u5f53cp\u547d\u4ee4\u7684\u53c2\u6570\u8fc7\u591a\u65f6\u6267\u884c\u8d77\u6765\u5c31\u4f1a\u51fa\u9519\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u4f7f\u7528find\u548cxargs\u547d\u4ee4\u7684\u7ec4\u5408\u6765\u5904\u7406\u8fd9\u4e00\u95ee\u9898\uff09:<\/p>\n<pre class=\"bash\" style=\"color: #111111;\">$ <span style=\"font-weight: bold; color: #c20cb9;\">find<\/span> \/share\/media\/mp3\/ -<span style=\"font-weight: bold; color: #7a0874;\">type<\/span> f -name <span style=\"color: #ff0000;\">\"*.mp3\"<\/span> -print0 | <span style=\"font-weight: bold; color: #c20cb9;\">xargs<\/span> <span style=\"color: #000000;\">-0<\/span> -r -I <span style=\"font-weight: bold; color: #c20cb9;\">file<\/span> <span style=\"font-weight: bold; color: #c20cb9;\">cp<\/span> -v -p <span style=\"font-weight: bold; color: #c20cb9;\">file<\/span> --target-<span style=\"color: #007800;\">directory=<\/span>\/bakup\/iscsi\/mp3<\/pre>\n<p style=\"color: #111111;\">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:<br \/>\n<code>man xargs<\/code><\/p>\n<\/div>\n<div><span style=\"color: #000000; font-size: medium;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/span><\/div>\n<div><span style=\"color: #000000; font-size: medium;\">for i in `sort \/root\/1.txt | awk &#8216;!a[$0]++&#8217;`;do<br \/>\nmkdir $DIR2\/$i;<br \/>\nfind $DIR2 -type f -newermt $i -print0 | xargs -0 mv -t $DIR2\/$i<br \/>\ndone<\/span><\/div>\n<div><span style=\"color: #000000; font-size: medium;\">\u6216\u8005<\/span><\/div>\n<div>for i in `sort \/root\/1.txt | awk &#8216;!a[$0]++&#8217;`;do<br \/>\nmkdir $DIR2\/$i;<br \/>\nfind $DIR2 -type f -newermt $i -print0 | xargs -0 mv\u00a0{} $DIR2\/$i<br \/>\ndone<\/div>\n<div><span style=\"color: #000000; font-size: medium;\">&#8212;&#8212;&#8211;<\/span><\/div>\n<div><span style=\"color: #000000; font-size: medium;\">mv\u547d\u4ee4\u7684\u4f7f\u7528\u6982\u8981\uff1a<\/span><br \/>\nmv [OPTION]&#8230; [-T] SOURCE DEST<br \/>\nmv [OPTION]&#8230; SOURCE&#8230; DIRECTORY<br \/>\nmv [OPTION]&#8230; -t DIRECTORY SOURCE&#8230;<\/div>\n<div><span style=\"color: #000000; font-size: medium;\">\u6240\u4ee5\uff0c\u4f60\u5fc5\u987b\u7ed9mv\u547d\u4ee4\u63d0\u4f9b\u6e90\u6587\u4ef6\/\u76ee\u5f55\uff01<\/span><\/div>\n<div><\/div>\n<div><span style=\"color: #000000; font-size: medium;\">\u53c2\u8003\u94fe\u63a5\uff1a<\/span><\/div>\n<div>Linux xargs\u547d\u4ee4 &#8211; \u7a0b\u5e8f\u5458\u5929\u4e0b &#8211; \u535a\u5ba2\u56ed <a href=\"http:\/\/www.cnblogs.com\/wdpp\/archive\/2012\/02\/28\/2386683.html\">http:\/\/www.cnblogs.com\/wdpp\/archive\/2012\/02\/28\/2386683.html<\/a><\/div>\n<div><strong><span style=\"color: #ff0000;\">Finding All .mp3 Files And Move To a New Directory From A Shell Prompt<\/span><\/strong> &#8211; nixCraft <a href=\"http:\/\/www.cyberciti.biz\/tips\/howto-linux-unix-find-move-all-mp3-file.html\">http:\/\/www.cyberciti.biz\/tips\/howto-linux-unix-find-move-all-mp3-file.html<\/a><\/div>\n<div><strong><span style=\"color: #ff0000;\">xargs: How To Control and Use Command Line Arguments<\/span><\/strong> <a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-bsd-xargs-construct-argument-lists-utility\/\">http:\/\/www.cyberciti.biz\/faq\/linux-unix-bsd-xargs-construct-argument-lists-utility\/<\/a><\/div>\n<div>Linux\u6309\u6587\u4ef6\u5185\u5bb9\u67e5\u627e\u6587\u4ef6 &#8211; bbirdsky &#8211; \u535a\u5ba2\u9891\u9053 &#8211; CSDN.NET <a href=\"http:\/\/blog.csdn.net\/bbirdsky\/article\/details\/22421107\">http:\/\/blog.csdn.net\/bbirdsky\/article\/details\/22421107<\/a><\/div>\n<div>Linux Shell\u5e38\u7528\u6280\u5de7(\u4e03) find xargs &#8211; David_Tang &#8211; \u535a\u5ba2\u56ed <a href=\"http:\/\/www.cnblogs.com\/mchina\/archive\/2012\/07\/02\/2573313.html\">http:\/\/www.cnblogs.com\/mchina\/archive\/2012\/07\/02\/2573313.html<\/a><\/div>\n<div><span style=\"color: #000000; font-size: medium;\">Linux Shell &#8211; Stephen_Liu &#8211; \u535a\u5ba2\u56ed <a href=\"http:\/\/www.cnblogs.com\/stephen-liu74\/category\/326653.html\">http:\/\/www.cnblogs.com\/stephen-liu74\/category\/326653.html<\/a><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Finding All .mp3 Files And Move To a New Directory From [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[89,90],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-find","tag-xargs"],"views":3575,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}