[{"data":1,"prerenderedAt":175},["ShallowReactive",2],{"posts:\u002Fposts\u002F2021-01-06-linux-regex-batch-file-renaming":3},{"id":4,"title":5,"body":6,"categories":152,"date":154,"description":155,"extension":156,"image":155,"lang":155,"meta":157,"navigation":166,"path":167,"seo":168,"stem":169,"tags":170,"titleInPicture":173,"__hash__":174},"posts\u002Fposts\u002F2021-01-06-linux-regex-batch-file-renaming.md","Linux: regex batch file renaming",{"type":7,"value":8,"toc":144},"minimark",[9,14,18,21,25,28,32,35,52,69,72,76,84,88,103,124,133,140],[10,11,13],"h2",{"id":12},"context","Context",[15,16,17],"p",{},"Very often I find myself in the need to rename a batch of files, usually to remove, replace or to add content on a certain part of that filenames. Rewriting the contents of the files one by one, is the kind of nonesense one is supposed to get rid of when using a computer. It is, however, hard to find some free tool to do this, and often these tools are paid or have nonsense limitations.",[15,19,20],{},"I wrote this guide mostly as a code snippet for myself to come back to.",[10,22,24],{"id":23},"use-case","Use case",[15,26,27],{},"With this method you can rename files that match a description, match the sections that compose the filename and selectively remove, replace or append to each one of these sections. I also give some insight on how to make this task a bit more intuitive. One example is removing a section of the filename that makes the names unnecessarily long. Other possible use, is to homogenize certain text sections across all the files.",[10,29,31],{"id":30},"how-to-use","How to use",[15,33,34],{},"Combining Linux shell's find and rename commands, it is possible to use a regular expression to rename the files intelligently.",[15,36,37,38,45,46,51],{},"I usually create a ",[39,40,44],"a",{"href":41,"rel":42},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FRegular_expression",[43],"nofollow","regular expression"," that separates the name into sections of interest. Regular expressions can be a bit daunting at the beginning, but thanks to online tools such as ",[39,47,50],{"href":48,"rel":49},"https:\u002F\u002Fregexr.com\u002F",[43],"regexr.com",", you can try out how the expression works before touching the filenames. For security against your own mistakes, I advice you to do these changes only in folders that do not contain other folders, and keep a copy of the files without renaming safe somewhere else.",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-shell shiki shiki-themes github-dark","rename 's\u002F(regex pattern)\u002F(replace pattern)\u002F' *\n","shell","",[60,61,62],"code",{"__ignoreMap":58},[63,64,67],"span",{"class":65,"line":66},"line",1,[63,68,56],{},[15,70,71],{},"One important advice, if you don't want to mess up your files, is to use the -n flag before actually doing the renaming. The -n flag will make the command tell you the changes, but not actually change anything. In that way you can make sure that the expression works like you expected. I still recommend doing a backup anyway, just in case.",[10,73,75],{"id":74},"source","Source",[15,77,78,79,83],{},"I don't know my way around combining commands in Linux very well, but an answer to this stack overflow question offered the perfect solution: ",[39,80,81],{"href":81,"rel":82},"https:\u002F\u002Faskubuntu.com\u002Fquestions\u002F283145\u002Fpattern-based-batch-file-rename-in-terminal",[43],". Later I discovered that I can have the same results I needed by just using rename command alone.",[10,85,87],{"id":86},"example","Example",[15,89,90,91,96,97,102],{},"I downloaded ",[39,92,95],{"href":93,"rel":94},"https:\u002F\u002Ffreesound.org\u002Fpeople\u002Fpjcohen\u002Fpacks\u002F23108\u002F",[43],"this"," sound pack from pjcohen user in ",[39,98,101],{"href":99,"rel":100},"http:\u002F\u002Ffreesound.org",[43],"freesound.org",". Each file came with a preceding id number, different on each sample. I needed to remove this number.",[15,104,105,106,109,110,113,114,117,118,123],{},"I wrote the regular expression that separates the two areas of interest in the filename. The pattern \"",[60,107,108],{},"(\\d{6}__)","\" captures six digits followed by two underscores, in group 1 (a parenthesis creates a capture group in regex). Then \"",[60,111,112],{},"(.+)","\" captures all the remaining characters, in capture group 2. I know my way around regex, but if you need to try before making changes, use an online tool to simulate the replacements. Simply do an ",[60,115,116],{},"ls"," command, to get the list and use it as your test example text in there. Check ",[39,119,122],{"href":120,"rel":121},"http:\u002F\u002Fregexr.com\u002F5jnbb",[43],"this interactive example",", which is more intuitive.",[53,125,127],{"className":55,"code":126,"language":57,"meta":58,"style":58},"rename 's\u002F(\\d{6}__)(.+)\u002F$2\u002F' *.wav\n",[60,128,129],{"__ignoreMap":58},[63,130,131],{"class":65,"line":66},[63,132,126],{},[15,134,135,136,139],{},"Observe, too, that if I wanted, I could have re-ordered the sections of interest by simply changing the \"replace\" section of that regular expression with ",[60,137,138],{},"$2$1",".",[141,142,143],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":58,"searchDepth":145,"depth":145,"links":146},2,[147,148,149,150,151],{"id":12,"depth":145,"text":13},{"id":23,"depth":145,"text":24},{"id":30,"depth":145,"text":31},{"id":74,"depth":145,"text":75},{"id":86,"depth":145,"text":87},[153],"blog","2021-01-06",null,"md",{"excerpt":158},{"type":7,"value":159},[160,162,164],[10,161,13],{"id":12},[15,163,17],{},[15,165,20],{},true,"\u002Fposts\u002F2021-01-06-linux-regex-batch-file-renaming",{"title":5,"description":58},"posts\u002F2021-01-06-linux-regex-batch-file-renaming",[171,172],"gist","linux",false,"vzl-CqzF2JZUsLlb6s82J4u10JDmGB3DXvp2gA5UzAY",1792083597569]