Remove all spaces or unwanted characters from the file name
Often, there are multiple spaces or unwanted characters in the file name, and it becomes annoying to access these files from the terminal. We can remove spaces from file name manually, but it becomes a tedious task when there are a large number of files involved. Fortunately, GNU/Linux has a command that solves this problem. This is the “rename” command, Which renames the filenames supplied according to the rule specified as the first argument. Let us understand this with a simple example.
Shown below are the files whose names contain spaces:
vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ ls -lash total 5.3M 4.0K drwxrwxr-x 2 vijayan vijayan 4.0K Jan 10 23:46 . 4.0K drwxr-xr-x 8 vijayan vijayan 4.0K Jan 10 23:42 .. 648K -rw-rw-r-- 1 vijayan vijayan 643K Dec 31 22:53 Aval Azhakai.mp3 564K -rw-rw-r-- 1 vijayan vijayan 557K Dec 31 22:54 Azhagiya Theeye.mp3 480K -rw-rw-r-- 1 vijayan vijayan 475K Dec 31 22:54 Azhagiya Theeye Whistle.mp3 680K -rw-rw-r-- 1 vijayan vijayan 675K Dec 31 22:55 Iru Vizhi unathu.mp3 624K -rw-rw-r-- 1 vijayan vijayan 617K Dec 31 22:55 Nenjai Poopoal.mp3 584K -rw-rw-r-- 1 vijayan vijayan 580K Dec 31 22:55 Pooppol Pooppol.mp3 540K -rw-rw-r-- 1 vijayan vijayan 536K Dec 31 22:55 Vaseegara Theme.mp3 620K -rw-rw-r-- 1 vijayan vijayan 613K Dec 31 22:55 Venmathi love fail.mp3 596K -rw-rw-r-- 1 vijayan vijayan 590K Dec 31 22:56 Verenna Verenna Male.mp3
Now, let us remove these spaces using the “rename” command as follows:
vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ rename 's/ //g' *.mp3
It’s that simple, We are done. Now, Let us verify the results:
vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ rename 's/ //g' *.mp3 vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ ls -lash total 5.3M 4.0K drwxrwxr-x 2 vijayan vijayan 4.0K Jan 10 23:45 . 4.0K drwxr-xr-x 8 vijayan vijayan 4.0K Jan 10 23:42 .. 648K -rw-rw-r-- 1 vijayan vijayan 643K Dec 31 22:53 AvalAzhakai.mp3 564K -rw-rw-r-- 1 vijayan vijayan 557K Dec 31 22:54 AzhagiyaTheeye.mp3 480K -rw-rw-r-- 1 vijayan vijayan 475K Dec 31 22:54 AzhagiyaTheeyeWhistle.mp3 680K -rw-rw-r-- 1 vijayan vijayan 675K Dec 31 22:55 IruVizhiunathu.mp3 624K -rw-rw-r-- 1 vijayan vijayan 617K Dec 31 22:55 NenjaiPoopoal.mp3 584K -rw-rw-r-- 1 vijayan vijayan 580K Dec 31 22:55 PooppolPooppol.mp3 540K -rw-rw-r-- 1 vijayan vijayan 536K Dec 31 22:55 VaseegaraTheme.mp3 620K -rw-rw-r-- 1 vijayan vijayan 613K Dec 31 22:55 Venmathilovefail.mp3 596K -rw-rw-r-- 1 vijayan vijayan 590K Dec 31 22:56 VerennaVerennaMale.mp3 vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$
Indeed, it has worked. In the above example, the “rename” command specified the rule to replace the space with nothing, i.e., it just removes the space. In the above command, the “g” option implies “global”, i.e., “remove all spaces”. If you want to replace spaces with any other character - for instance, an underscore _ then use the following rule with the “rename” command:
vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ rename 's/ /-/g' *.mp3
It’s that simple, We are done. Now,
vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ rename 's/ /-/g' *.mp3 vijayan@vijayan-VPCCA35FA:~/Downloads/techpulsetoday$ ls -lash total 5.3M 4.0K drwxrwxr-x 2 vijayan vijayan 4.0K Jan 10 23:46 . 4.0K drwxr-xr-x 8 vijayan vijayan 4.0K Jan 10 23:42 .. 648K -rw-rw-r-- 1 vijayan vijayan 643K Dec 31 22:53 Aval-Azhakai.mp3 564K -rw-rw-r-- 1 vijayan vijayan 557K Dec 31 22:54 Azhagiya-Theeye.mp3 480K -rw-rw-r-- 1 vijayan vijayan 475K Dec 31 22:54 Azhagiya-Theeye-Whistle.mp3 680K -rw-rw-r-- 1 vijayan vijayan 675K Dec 31 22:55 Iru-Vizhi-unathu.mp3 624K -rw-rw-r-- 1 vijayan vijayan 617K Dec 31 22:55 Nenjai-Poopoal.mp3 584K -rw-rw-r-- 1 vijayan vijayan 580K Dec 31 22:55 Pooppol-Pooppol.mp3 540K -rw-rw-r-- 1 vijayan vijayan 536K Dec 31 22:55 Vaseegara-Theme.mp3 620K -rw-rw-r-- 1 vijayan vijayan 613K Dec 31 22:55 Venmathi-love-fail.mp3 596K -rw-rw-r-- 1 vijayan vijayan 590K Dec 31 22:56 Verenna-Verenna-Male.mp3