I am glad I found this workaround, but maybe someone can explain this to me. I became so frustrated with it that I even explicitly spelled out the pathname each time.
I cannot remove files with a command run from a shell script which works fine if run directly in the CLI … For … security reasons? … Maybe? I don't know. There's no error message either – it seems like a success. But nothing happens.
#!/bin/bash
sudo chown -R $USER:$USER "/home/user/project/uploads"
sudo chmod -R 777 "/home/user/project/uploads"
sudo rm -rf "/home/user/project/uploads/media/*" # ‹———
But if I do the same with moving it works fine ¿!?¡
#!/bin/bash
DIR="/home/user/project/tmp/media"
if [ ! -d "$DIR" ]; then
mkdir "$DIR"
fi
sudo mv "/home/user/project/uploads/media" "/home/user/project/uploads/delete-me-manually"
sudo mv "/home/user/project/tmp/media" "/home/user/project/uploads/media"
Where's the sense in that? Why is remove not working in the first place? The file permissions are correct [1] and the paths don't contain spaces or other characters. What else could be the issue?
Would be nice if my script wouldn't leave behind trash that I have to manually delete.
File permissions and ownership of script are:
-rwxr--r-- 1 user user 26K Nov 19 16:23 my-script.sh
Shell matches file shebang:
echo $0
bash
Script is called such (not prefaced with bash or sh):
$ ./my-script.sh
File permissions and ownerships of dir are:
drwxr-xr-x 8 user user 4,0K Jul 22 21:02 /home/user/project/
drwx------ 8 user user 4,0K Nov 19 10:44 /home/user/project/tmp/
# At start of script
drwxr-xr-x 9 www-data www-data 36K Nov 19 16:35 /home/user/project/uploads/
drwxr-xr-x 65 www-data www-data 4,0K Nov 19 11:11 /home/user/project/uploads/media/
# After changing it.
drwxrwxrwx 9 user user 36K Nov 19 16:35 /home/user/project/uploads/
drwxrwxrwx 65 user user 4,0K Nov 19 11:11 /home/user/project/uploads/media/
CLI user and script user are identical (as tested with whoami).
*is not expanded when in quotes. I'd treat this as a typo. Also, what's the sense in all thesudocalls?useruser can manipulatewww-datafiles. And, because I got more and more frustrated.