1

The following commands behaves different between Linux and Mac:

mkdir tmp ln -s tmp symlink rm -rf symlink/ 

On Linux, the content of tmp folder is deleted but the folder itself remains, while on Mac the folder is deleted as well, breaking the symlink.

Is there a way to have a command that behaves like Linux on Mac as well?

1

2 Answers 2

2

I can't see any way to have rm on a Mac act in the way it does on a Linux-based system, such that it is to leave the target of the symlink untouched.

This find command looks like it will achieve what you want, tested on a "Big Sur" instance of a Mac

find symlink/ -mindepth 1 -delete 

The up-side is that this command will act in the same way on both platforms (Mac and Linux-based).

1
  • 1
    "I can't see any way to have rm on a Mac act in the way it does on a Linux-based system" – I wouldn't be surprised if this difference had nothing to do with Linux or macOS but rather with the specific implementation of rm used. If that is indeed the case, the obvious solution would be to use the same implementation of rm on both. Commented Jun 21, 2024 at 21:51
0

Since you know/see that the behaviour differs - why don't you just extend your invocation by one character and get the same behaviour on both?

$ mkdir tmp $ touch tmp/test{0..9} $ ln -s tmp symlink $ ls -l total 4 lrwxrwxrwx 1 andrej andrej 3 2024-06-23 08:03 symlink -> tmp drwxrwxr-x 2 andrej andrej 4096 2024-06-23 08:03 tmp $ ls -l tmp/ total 0 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test0 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test1 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test2 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test3 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test4 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test5 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test6 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test7 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test8 -rw-rw-r-- 1 andrej andrej 0 2024-06-23 08:03 test9 $ rm -rf symlink/* $ ls -l total 4 lrwxrwxrwx 1 andrej andrej 3 2024-06-23 08:03 symlink -> tmp drwxrwxr-x 2 andrej andrej 4096 2024-06-23 08:03 tmp $ ls -l tmp/ total 0 $ 

Just adding the * after the / makes rm behave the same on both Ubuntu 20.04 and Sonoma 14.5.

4
  • P.S.: Just looking at your original command (without testing it) I would have expected the deletion of the symlink, neither Linux' nor Mac-OS' behaviour seem "intuitive" to me. Commented Jun 22, 2024 at 20:15
  • touch tmp/.dotty - such a file won't be deleted with your solution Commented Jun 22, 2024 at 21:47
  • Fair enough @ChrisDavies ... let's add a shopt -s dotglob? ;) Commented Jun 22, 2024 at 23:04
  • Would work for me :-) Commented Jun 23, 2024 at 15:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.