this is my first post, so go gently :)

I'm creating an AppleScript which runs bash commands to select and delete the most recently modified file within a specified group of folders. The file name will be different each time.

I've seen how to delete a file with special characters, when you know the actual file name, but in my case the file name is discovered by ls. Sadly when I got to delete the file using rm, it's all messed up and can't ID the file due to having question marks and spaces in it's name. Is there a way to add quotes onto the outside of a variable, so that it can be address correctly? Or is there a better way. I can't even work out how to rename the file. Also, I only want to change/delete that one file, I can't rename all of the files in the folder.

Here is an example of one of the file names. ?+61 434 570 460? on 2016-12-22 at 15.01.27.ichat

And here is the code that I'm trying to get working; rm -fr $(ls -t | head -n1)

Thanks for your input folks :)


I worked it out eventually, I had to put quotes in quotes, i.e. add quotes to the command.

This is the working command to delete the most recent file, regardless of any special characters or spaces in the name.

rm -f """$(ls -t | tail -1)"""


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.