0

Whenever I download .docx and .ppt files from my school, they always look like this.

UTF-8"File%20Name%20Example.docx

It's pretty annoying to have to rename the file every time I download one, so I want to create a bash script that will do the work for me.

I have a School folder, so I'd like to do a bash script that searches the entire directory and subdirectories for any files that start with the "UTF-8"" prefix, then.

  • Have a for loop that removes the prefix from all files starting with that prefix
  • Search the filename for occurences of "%20" and replace it with " "
  • Monitor the School folder so that any time I download another file with the prefix, it will run the script and immediately rename it.

I am pretty new to linux. I have been researching for the last two hours and I am confused on what I should do. Any help would be greatly appreciated. Thanks!

2

A basic example of how to monitor files in a folder with systemd.path (in this case /opt/download).

/etc/systemd/system/download-monitor.path:

[Unit]
Description=Download Monitor

[Path]
PathExistsGlob=/opt/download/UTF8\"*

[Install]
WantedBy=multi-user.target

Next, we need to create a .service file with the same basename as the .path file.

/etc/systemd/system/download-monitor.service:

[Unit]
Description=Download Monitor

[Service]
ExecStart=/opt/bin/filecheck.sh   

Last the Exec script file.

/opt/bin/filecheck.sh:

#!/bin/bash

shopt -s nullglob extglob

# systemd.path does not pass what changed so we
# rename all .docx and .ppt files starting with
# UTF8\" in the $a folder.
a=/opt/download
for b in $a/UTF8\"*@(.docx|.ppt); do
    c="$a/${b#$a/UTF8\"}"
    mv "$b" "$c" && deurlname "$c"
done

The deurlname command is found in the renameutils package.

Now we can enable and start our path monitor:

$ systemctl enable download-monitor.path
$ systemctl start  download-monitor.path
| improve this answer | |
1

You could use incron for this purpose. It is like a cron, but uses events as triggers.

sudo apt install incron
sudo systemctl enable incron

Add your current user to /etc/incron.allow file:

$ sudo cat /etc/incron.allow 
[sudo] password for myuser: 
myuser

After you've added your user to that file, you could edit incrontab file by that user:

incrontab -e
/home/myuser/test/utfnames      IN_CREATE       /home/myuser/test/renamefiles.sh

As you can see, the first column contains directory path, the second contains watched event (file creation), the third contains command or path to script.

The script contains the next:

for file in /home/myuser/test/utfnames/*
  do mv $file "$(echo $file | sed 's/UTF-8//' | sed 's/%20/ /g')"
done

Do not forget to chmod +x /home/myuser/test/renamefiles.sh

Testing:

myuser@ubuntu:~$ touch 'test/utfnames/UTF-8"Some%20File%20Name.docx'
myuser@ubuntu:~$ touch 'test/utfnames/UTF-8"Some%20File%20Name%202.docx'
myuser@ubuntu:~$ sudo systemctl start incron.service 
myuser@ubuntu:~$ ls test/utfnames/
'UTF-8"Some%20File%20Name%202.docx'  'UTF-8"Some%20File%20Name.docx'
myuser@ubuntu:~$ touch 'test/utfnames/UTF-8"Some%20File%20Name%203.docx'
myuser@ubuntu:~$ ls test/utfnames/
'"Some File Name 2.docx'  '"Some File Name 3.docx'  '"Some File Name.docx'
| improve this answer | |

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.