{"id":97,"date":"1997-09-09T01:36:29","date_gmt":"1997-09-09T09:36:29","guid":{"rendered":"http:\/\/www.birdhouse.org\/betips\/?p=97"},"modified":"1997-09-09T01:36:29","modified_gmt":"1997-09-09T09:36:29","slug":"renaming-batches-of-files","status":"publish","type":"post","link":"https:\/\/www.betips.net\/?p=97","title":{"rendered":"Renaming batches of files"},"content":{"rendered":"<p>If you&#8217;re coming to BeOS from the DOS world, you expect Terminal to be a lot more powerful than DOS. And it is. But with power comes an occasional price &#8212; some things are more complex to accomplish in bash than in DOS. A good example of this is in renaming large batches of files. Let&#8217;s say you&#8217;ve got a directory containing 500 text files, all ending with the .txt extension, and you want to make them end in .html. In DOS, this would be as simple as typing <code>ren *.txt *.html<\/code>. Since the bash command for renaming files is <code>mv<\/code>, you might expect you can type <code>mv *.txt *.html<\/code>. But it doesn&#8217;t work that way.<br \/>\n It would take a complex explanation of exactly how the bash shell works to explain why not, but if you need to accomplish this task, here&#8217;s how: type the following into the shell, hitting Enter after each line.<\/p>\n<pre>\nfor i in *.txt\ndo\nmv \"$i\" \"$(echo $i | sed s\/.txt$\/.html\/)\"\ndone\n<\/pre>\n<p> In a nutshell, here&#8217;s what&#8217;s going on: you establish a loop to go through all the filenames in the current directory ending in .txt (* refers to all files in the current directory). &#8222;Do&#8220; means you&#8217;re going to look at the first file and do something with it. That something is the move command, <code>mv<\/code>. We move the first file from its current location of {variable} to {variable.html}. The tricky part is that we need to use sed (the stream editor) to substitute the string &#8222;txt&#8220; at the end of the line with the string &#8222;html&#8220;. Done means we&#8217;re ready to move to the next file, and also serves as a flag for the process to exit when all files have been processed.<br \/>\nIf you need to use this function frequently, save the whole block as a script prefaced with the magic cookie <code>#!\/bin\/sh<\/code> and call it &#8222;ren&#8220; or something else intuitive.<br \/>\nNote that just changing the extensions will not change the filetypes, because BeOS doesn&#8217;t use extensions as its primary means of identifying filetypes. To do that, see the tip <a href=\"\/chunga.php?ID=048\">Batch-changing filetypes<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re coming to BeOS from the DOS world, you expect Terminal to be a lot more powerful than DOS. And it is. But with power comes an occasional price &#8212; some things are more complex to accomplish in bash than in DOS. A good example of this is in renaming large batches of files. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[10],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","tag-scripting"],"_links":{"self":[{"href":"https:\/\/www.betips.net\/index.php?rest_route=\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.betips.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.betips.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.betips.net\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.betips.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":0,"href":"https:\/\/www.betips.net\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.betips.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.betips.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.betips.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}