{"id":5103,"date":"2026-08-30T06:30:32","date_gmt":"2026-08-29T21:30:32","guid":{"rendered":"https:\/\/donguri3.net\/server-tech\/find-mp3-safe-move-2\/"},"modified":"2026-08-30T06:30:33","modified_gmt":"2026-08-29T21:30:33","slug":"find-mp3-safe-move","status":"publish","type":"post","link":"https:\/\/donguri3.net\/en\/server-tech\/linux-server-network\/find-mp3-safe-move\/","title":{"rendered":"How to Find and Collect All MP3 Files into a Single Directory [Supports Spaces and Japanese Filenames]"},"content":{"rendered":"<p>You might want to gather various `.mp3` files\u2014such as music or recordings\u2014into a single location, ignoring the existing directory structure. A combination of the `find` and `mv` commands is very handy for this.<\/p>\n<p>However, <span style=\"color: #ff9900;\"><strong>you need to be careful if the filenames contain spaces, Japanese characters, or symbols<\/strong><\/span>. If you process them directly with a standard `for` loop or similar approach, unintended splitting will occur and the command will fail.<\/p>\n<p>In this article, I will introduce an actual error example I encountered and show you how to safely move `.mp3` files to a single location.<\/p>\n<h2>Failure Example: A for-loop combined with find is vulnerable to spaces<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">for f in $(find . -name &quot;*.mp3&quot;); do\nmv &quot;$f&quot; .\/music\/\ndone<\/pre>\n<p>With this syntax, if the filenames contain spaces or Japanese characters, they get split up, resulting in a series of errors like the ones below:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">mv: cannot stat `Classic\/Classic&#039;: No such file or directory\nmv: cannot stat `#13&#039;: No such file or directory<\/pre>\n<h2>The Correct Approach: Use <code>-print0<\/code> and <code>read -d ''<\/code><\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">mkdir -p .\/music\n\nfind \/mnt\/main\/share\/0common\/00_swaps\/iTunes-Share\/Music\/EMI\\ Classic\/ -type f -name &quot;*.mp3&quot; -print0 | \\\\\nwhile IFS= read -r -d &#039;&#039; file; do\necho &quot;$file&quot;\nmv &quot;$file&quot; .\/music\/\ndone<\/pre>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><code>-print0<\/code>: Uses NULL (`\\\\0`) as the delimiter for filenames<\/li>\n<li><code>-d ''<\/code>: Correctly reads inputs delimited by NULL<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li><code>\"$file\"<\/code>: Encloses the filename in double quotes to prevent it from being split<\/li>\n<li><code>IFS=<\/code>: Disables extra delimiters (spaces and newlines)<\/li>\n<\/ul>\n<h2>How to Safely Handle Duplicate Filenames<\/h2>\n<p>If a file with the same name already exists in the destination, `mv` will overwrite it. To prevent this, a script that includes renaming logic is very useful.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">mkdir -p .\/music\n\nfind . -type f -iname &quot;*.mp3&quot; -print0 | while IFS= read -r -d &#039;&#039; file; do\nbase=$(basename &quot;$file&quot;)\ndest=&quot;.\/music\/$base&quot;\nif &#x5B;&#x5B; -e &quot;$dest&quot; ]]; then\ni=1\nwhile &#x5B;&#x5B; -e &quot;.\/music\/${i}_$base&quot; ]]; do\n((i++))\ndone\ndest=&quot;.\/music\/${i}_$base&quot;\nfi\nmv &quot;$file&quot; &quot;$dest&quot;\ndone<\/pre>\n<h2>Conclusion<\/h2>\n<ul>\n<li>While <code>find<\/code> + <code>mv<\/code> is extremely powerful, you must watch out for the pitfalls of <strong>spaces and special characters<\/strong>.<\/li>\n<li>The syntax combining <code>-print0<\/code> and <code>read -d ''<\/code> might look a bit niche at first glance, but it is an <strong>essential skill that achieves both safety and versatility<\/strong>.<\/li>\n<li>Once you learn the method in this article, you can easily apply it to organize other files such as images, audio, and PDFs.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You might want to gather various `.mp3` files\u2014such as music or recordings\u2014into a single location, ignoring the [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":1801,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/donguri3.net\/?p=1763","footnotes":""},"categories":[1170],"tags":[105,663,659],"class_list":["post-5103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-server-network","tag-linux","tag-663","tag-659","en-US"],"_links":{"self":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts\/5103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/comments?post=5103"}],"version-history":[{"count":1,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts\/5103\/revisions"}],"predecessor-version":[{"id":5106,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/posts\/5103\/revisions\/5106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/media\/1801"}],"wp:attachment":[{"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/media?parent=5103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/categories?post=5103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/donguri3.net\/wp-json\/wp\/v2\/tags?post=5103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}