1

I have a few files that I want to rename and want to accomplish this in the command line.

I need to remove these characters "¡¿" from the beginning of the file names.

From this:

¡Tú_la_llevas!-2018.mp4
¿quien_como_tu?.mp4

To be like this:

Tú_la_llevas!-2018.mp4
quien_como_tu?.mp4
2
  • You can use rename rename -n -E 's/(¿|¡)//' *.mp4. -n just prints the result without actually do anything (remove it when You are ready to rename files). Just put more characters to remove inside the brackets () and join them with pipe |. Also see man rename for more info about the command. – Michal Przybylowicz Oct 5 '19 at 22:53
  • If the files are inside /opt/server/files how will this command be applied? - it will be run on a cron job. – graveangel Oct 5 '19 at 23:39
0

First, you need to install rename:

sudo apt install rename

Then, cd to the directory containing the files and test by running the following command:

rename -n 's/^(¡|¿)(.+)\.mp4$/$2.mp4/' *

If you are satisfied with the output, rename the files by running the following command:

rename 's/^(¡|¿)(.+)\.mp4$/$2.mp4/' *

@graveangel:

To run this as a cronjob on all the files in /opt/server/ directory, pleas follow these steps to make it into a shell script that you can then run as a cronjob:

Firstly, create a file with the name myrename.sh for example. You can crate it anywhere you want.

Secondly, copy and paste the following into the file and save it:

#!/bin/bash

cd /opt/server

rename 's/^(¡|¿)(.+)\.mp4$/$2.mp4/' *

Thirdly, make the file executable:

chmod +x myrename.sh

Finally, test it and use it with bash /FULL_PATH/myrename.sh.

6
  • Forgot to mention, this command will be run as a cron job and the files are located within /opt/server/files... how can the command be applied then? – graveangel Oct 5 '19 at 23:15
  • @graveangel No problem, it will run fine. cronjobs run as root. If for any reason rename did not work, try adding the full path to it and use it instead of rename. Run which rename in the terminal to get the full path, the output would be something like /usr/bin/rename – Raffa Oct 5 '19 at 23:25
  • Meant to say the files I want to rename are inside /opt/server/files – graveangel Oct 5 '19 at 23:29
  • @graveangel I will update the answer with that. One moment please – Raffa Oct 5 '19 at 23:49
  • 1
    @inemanja Are you being sophisticated? :) sudo crontab -e will do the job it will run like root. I did not really mean the ROOT. :) – Raffa Oct 6 '19 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.