Why is “rm -r” unable to delete this folder?












9















I have a folder with -wx permissions called folder1 and another folder inside it called folder2 with rwx permissions.



I tried to delete folder1 using this command:



rm -r folder1


But I got the following error:



rm: cannot remove 'folder1': Permission denied


The reason I think I got this error is because the rm program needs to first get the content of folder1 (get the names of the files and folders inside folder1 that is) in order to be able to delete that content (because you can't delete a file or folder without knowing its name I think), and then the rm program can delete folder1 itself.



But since folder1 doesn't have the read permission, then the rm program can't get its content, and hence it can't delete its content, and since it can't delete its content, then it can't delete it.



Am I correct?










share|improve this question

























  • Do "ls -l" and tell us what the permissions of the DIRECTORY are.

    – jamesqf
    3 mins ago
















9















I have a folder with -wx permissions called folder1 and another folder inside it called folder2 with rwx permissions.



I tried to delete folder1 using this command:



rm -r folder1


But I got the following error:



rm: cannot remove 'folder1': Permission denied


The reason I think I got this error is because the rm program needs to first get the content of folder1 (get the names of the files and folders inside folder1 that is) in order to be able to delete that content (because you can't delete a file or folder without knowing its name I think), and then the rm program can delete folder1 itself.



But since folder1 doesn't have the read permission, then the rm program can't get its content, and hence it can't delete its content, and since it can't delete its content, then it can't delete it.



Am I correct?










share|improve this question

























  • Do "ls -l" and tell us what the permissions of the DIRECTORY are.

    – jamesqf
    3 mins ago














9












9








9








I have a folder with -wx permissions called folder1 and another folder inside it called folder2 with rwx permissions.



I tried to delete folder1 using this command:



rm -r folder1


But I got the following error:



rm: cannot remove 'folder1': Permission denied


The reason I think I got this error is because the rm program needs to first get the content of folder1 (get the names of the files and folders inside folder1 that is) in order to be able to delete that content (because you can't delete a file or folder without knowing its name I think), and then the rm program can delete folder1 itself.



But since folder1 doesn't have the read permission, then the rm program can't get its content, and hence it can't delete its content, and since it can't delete its content, then it can't delete it.



Am I correct?










share|improve this question
















I have a folder with -wx permissions called folder1 and another folder inside it called folder2 with rwx permissions.



I tried to delete folder1 using this command:



rm -r folder1


But I got the following error:



rm: cannot remove 'folder1': Permission denied


The reason I think I got this error is because the rm program needs to first get the content of folder1 (get the names of the files and folders inside folder1 that is) in order to be able to delete that content (because you can't delete a file or folder without knowing its name I think), and then the rm program can delete folder1 itself.



But since folder1 doesn't have the read permission, then the rm program can't get its content, and hence it can't delete its content, and since it can't delete its content, then it can't delete it.



Am I correct?







linux permissions rm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









psmears

44328




44328










asked 13 hours ago









JohnJohn

1886




1886













  • Do "ls -l" and tell us what the permissions of the DIRECTORY are.

    – jamesqf
    3 mins ago



















  • Do "ls -l" and tell us what the permissions of the DIRECTORY are.

    – jamesqf
    3 mins ago

















Do "ls -l" and tell us what the permissions of the DIRECTORY are.

– jamesqf
3 mins ago





Do "ls -l" and tell us what the permissions of the DIRECTORY are.

– jamesqf
3 mins ago










2 Answers
2






active

oldest

votes


















12














I think your analysis is correct: you cannot delete the directory since its non-empty, and you cannot empty it since you cannot see its contents.



EDIT: I just gave it a try:



$ mkdir -p folder1/folder2
$ chmod -r folder1
$ rm -rf folder1
rm: cannot remove 'folder1': Permission denied
$ rmdir folder1/folder2
$ rm -rf folder1
$


EDIT 2: When I wrote “you”, I meant any program you may run. Your rm -r command first sees that folder1 is a directory, so it tries to discover its contents to empty it, but fails for missing read permission, then it tries to delete it but fails because it’s non-empty. The “Permission denied” is misleading; I think “Directory not empty” (like rmdir reports) would be more appropriate.)






share|improve this answer





















  • 3





    It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

    – Kusalananda
    13 hours ago













  • @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

    – user2233709
    12 hours ago











  • Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

    – Kusalananda
    12 hours ago











  • @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

    – user2233709
    11 hours ago













  • Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

    – Kusalananda
    11 hours ago





















4














For deletion to occur the system must be able to read the contents and identify what has to be deleted.



I've tried simulating what you are attempting :



[vagrant@desktop1 ~]$ sudo rm -rf folder1/ && mkdir -pv folder1/folder2 && sudo chmod 333 -v folder1/ && sudo chmod 777 -v folder1/folder2
mkdir: created directory 'folder1'
mkdir: created directory 'folder1/folder2'
mode of 'folder1/' changed from 0775 (rwxrwxr-x) to 0333 (-wx-wx-wx)
mode of 'folder1/folder2' changed from 0775 (rwxrwxr-x) to 0777 (rwxrwxrwx)
[vagrant@desktop1 ~]$ ls -lh
total 0
d-wx-wx-wx. 3 vagrant vagrant 21 Feb 24 10:40 folder1
[vagrant@desktop1 ~]$


If we try deleting without read permissions it fails:



[vagrant@desktop1 ~]$ rm -r folder1/
rm: cannot remove 'folder1/': Permission denied
[vagrant@desktop1 ~]$ sudo chmod +r folder1/
[vagrant@desktop1 ~]$ rm -r folder1/
[vagrant@desktop1 ~]$


In an strace for the two attempts the difference is that the directory contents cannot be read (getdents):



newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
geteuid() = 1000
newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
faccessat(AT_FDCWD, "folder1/", W_OK) = 0
openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0


With read permissions:



newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0777, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 3
fstat(3, {st_mode=S_IFDIR|0777, st_size=21, ...}) = 0
fcntl(3, F_GETFL) = 0x38800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW)
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
getdents(3, /* 3 entries */, 32768) = 80
close(3) = 0
geteuid() = 1000


To conclude even if you own a directory and it has the executable bit, you still need read permissions so that you may see its contents and delete the folder. It's not the same for a file though.






share|improve this answer










New contributor




ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502659%2fwhy-is-rm-r-unable-to-delete-this-folder%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    I think your analysis is correct: you cannot delete the directory since its non-empty, and you cannot empty it since you cannot see its contents.



    EDIT: I just gave it a try:



    $ mkdir -p folder1/folder2
    $ chmod -r folder1
    $ rm -rf folder1
    rm: cannot remove 'folder1': Permission denied
    $ rmdir folder1/folder2
    $ rm -rf folder1
    $


    EDIT 2: When I wrote “you”, I meant any program you may run. Your rm -r command first sees that folder1 is a directory, so it tries to discover its contents to empty it, but fails for missing read permission, then it tries to delete it but fails because it’s non-empty. The “Permission denied” is misleading; I think “Directory not empty” (like rmdir reports) would be more appropriate.)






    share|improve this answer





















    • 3





      It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

      – Kusalananda
      13 hours ago













    • @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

      – user2233709
      12 hours ago











    • Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

      – Kusalananda
      12 hours ago











    • @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

      – user2233709
      11 hours ago













    • Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

      – Kusalananda
      11 hours ago


















    12














    I think your analysis is correct: you cannot delete the directory since its non-empty, and you cannot empty it since you cannot see its contents.



    EDIT: I just gave it a try:



    $ mkdir -p folder1/folder2
    $ chmod -r folder1
    $ rm -rf folder1
    rm: cannot remove 'folder1': Permission denied
    $ rmdir folder1/folder2
    $ rm -rf folder1
    $


    EDIT 2: When I wrote “you”, I meant any program you may run. Your rm -r command first sees that folder1 is a directory, so it tries to discover its contents to empty it, but fails for missing read permission, then it tries to delete it but fails because it’s non-empty. The “Permission denied” is misleading; I think “Directory not empty” (like rmdir reports) would be more appropriate.)






    share|improve this answer





















    • 3





      It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

      – Kusalananda
      13 hours ago













    • @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

      – user2233709
      12 hours ago











    • Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

      – Kusalananda
      12 hours ago











    • @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

      – user2233709
      11 hours ago













    • Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

      – Kusalananda
      11 hours ago
















    12












    12








    12







    I think your analysis is correct: you cannot delete the directory since its non-empty, and you cannot empty it since you cannot see its contents.



    EDIT: I just gave it a try:



    $ mkdir -p folder1/folder2
    $ chmod -r folder1
    $ rm -rf folder1
    rm: cannot remove 'folder1': Permission denied
    $ rmdir folder1/folder2
    $ rm -rf folder1
    $


    EDIT 2: When I wrote “you”, I meant any program you may run. Your rm -r command first sees that folder1 is a directory, so it tries to discover its contents to empty it, but fails for missing read permission, then it tries to delete it but fails because it’s non-empty. The “Permission denied” is misleading; I think “Directory not empty” (like rmdir reports) would be more appropriate.)






    share|improve this answer















    I think your analysis is correct: you cannot delete the directory since its non-empty, and you cannot empty it since you cannot see its contents.



    EDIT: I just gave it a try:



    $ mkdir -p folder1/folder2
    $ chmod -r folder1
    $ rm -rf folder1
    rm: cannot remove 'folder1': Permission denied
    $ rmdir folder1/folder2
    $ rm -rf folder1
    $


    EDIT 2: When I wrote “you”, I meant any program you may run. Your rm -r command first sees that folder1 is a directory, so it tries to discover its contents to empty it, but fails for missing read permission, then it tries to delete it but fails because it’s non-empty. The “Permission denied” is misleading; I think “Directory not empty” (like rmdir reports) would be more appropriate.)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 13 hours ago

























    answered 13 hours ago









    user2233709user2233709

    993312




    993312








    • 3





      It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

      – Kusalananda
      13 hours ago













    • @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

      – user2233709
      12 hours ago











    • Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

      – Kusalananda
      12 hours ago











    • @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

      – user2233709
      11 hours ago













    • Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

      – Kusalananda
      11 hours ago
















    • 3





      It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

      – Kusalananda
      13 hours ago













    • @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

      – user2233709
      12 hours ago











    • Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

      – Kusalananda
      12 hours ago











    • @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

      – user2233709
      11 hours ago













    • Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

      – Kusalananda
      11 hours ago










    3




    3





    It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

    – Kusalananda
    13 hours ago







    It can't report Directory not empty in this case since it would not know it was empty or not. You would still get the same error when trying to delete an empty directory that you don't have read permissions on. (Also, please disregard my previous comment, I didn't have my thinking cap on).

    – Kusalananda
    13 hours ago















    @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

    – user2233709
    12 hours ago





    @Kusalananda That sounds sane, but rmdir is able to report “Directory not empty”. And if you read my test, you’ll see that it accepts to remove the folder1 directory, with no read permission, once I have emptied it.

    – user2233709
    12 hours ago













    Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

    – Kusalananda
    12 hours ago





    Your test shows an interesting difference between our systems. I get a Permission denied when trying to rm -r folder1 when it's empty. I'm on OpenBSD, not Linux.

    – Kusalananda
    12 hours ago













    @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

    – user2233709
    11 hours ago







    @Kusalananda That’s interesting. I would have thought that this behavior was specified by the Single Unix Specification, so that Linux and {Free,Net,Open}BSD would behave identically. (For the record, I am using Debian Stretch 9.8 with a linux 4.9.144-3 x86_64 kernel.)

    – user2233709
    11 hours ago















    Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

    – Kusalananda
    11 hours ago







    Hmm... The only thing that POSIX says is that if the operand is a directory and -r is used, each directory entry (except for . and ..) should be removed as if they were a file operand of rm -r. It appears as if GNU rm simply does a rmdir() on the directory if it's not readable, because it will have no way to get the contents of it.

    – Kusalananda
    11 hours ago















    4














    For deletion to occur the system must be able to read the contents and identify what has to be deleted.



    I've tried simulating what you are attempting :



    [vagrant@desktop1 ~]$ sudo rm -rf folder1/ && mkdir -pv folder1/folder2 && sudo chmod 333 -v folder1/ && sudo chmod 777 -v folder1/folder2
    mkdir: created directory 'folder1'
    mkdir: created directory 'folder1/folder2'
    mode of 'folder1/' changed from 0775 (rwxrwxr-x) to 0333 (-wx-wx-wx)
    mode of 'folder1/folder2' changed from 0775 (rwxrwxr-x) to 0777 (rwxrwxrwx)
    [vagrant@desktop1 ~]$ ls -lh
    total 0
    d-wx-wx-wx. 3 vagrant vagrant 21 Feb 24 10:40 folder1
    [vagrant@desktop1 ~]$


    If we try deleting without read permissions it fails:



    [vagrant@desktop1 ~]$ rm -r folder1/
    rm: cannot remove 'folder1/': Permission denied
    [vagrant@desktop1 ~]$ sudo chmod +r folder1/
    [vagrant@desktop1 ~]$ rm -r folder1/
    [vagrant@desktop1 ~]$


    In an strace for the two attempts the difference is that the directory contents cannot be read (getdents):



    newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
    openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
    geteuid() = 1000
    newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
    faccessat(AT_FDCWD, "folder1/", W_OK) = 0
    openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
    newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0


    With read permissions:



    newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0777, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
    openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 3
    fstat(3, {st_mode=S_IFDIR|0777, st_size=21, ...}) = 0
    fcntl(3, F_GETFL) = 0x38800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW)
    fcntl(3, F_SETFD, FD_CLOEXEC) = 0
    getdents(3, /* 3 entries */, 32768) = 80
    close(3) = 0
    geteuid() = 1000


    To conclude even if you own a directory and it has the executable bit, you still need read permissions so that you may see its contents and delete the folder. It's not the same for a file though.






    share|improve this answer










    New contributor




    ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      4














      For deletion to occur the system must be able to read the contents and identify what has to be deleted.



      I've tried simulating what you are attempting :



      [vagrant@desktop1 ~]$ sudo rm -rf folder1/ && mkdir -pv folder1/folder2 && sudo chmod 333 -v folder1/ && sudo chmod 777 -v folder1/folder2
      mkdir: created directory 'folder1'
      mkdir: created directory 'folder1/folder2'
      mode of 'folder1/' changed from 0775 (rwxrwxr-x) to 0333 (-wx-wx-wx)
      mode of 'folder1/folder2' changed from 0775 (rwxrwxr-x) to 0777 (rwxrwxrwx)
      [vagrant@desktop1 ~]$ ls -lh
      total 0
      d-wx-wx-wx. 3 vagrant vagrant 21 Feb 24 10:40 folder1
      [vagrant@desktop1 ~]$


      If we try deleting without read permissions it fails:



      [vagrant@desktop1 ~]$ rm -r folder1/
      rm: cannot remove 'folder1/': Permission denied
      [vagrant@desktop1 ~]$ sudo chmod +r folder1/
      [vagrant@desktop1 ~]$ rm -r folder1/
      [vagrant@desktop1 ~]$


      In an strace for the two attempts the difference is that the directory contents cannot be read (getdents):



      newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
      openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
      geteuid() = 1000
      newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
      faccessat(AT_FDCWD, "folder1/", W_OK) = 0
      openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
      newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0


      With read permissions:



      newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0777, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
      openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 3
      fstat(3, {st_mode=S_IFDIR|0777, st_size=21, ...}) = 0
      fcntl(3, F_GETFL) = 0x38800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW)
      fcntl(3, F_SETFD, FD_CLOEXEC) = 0
      getdents(3, /* 3 entries */, 32768) = 80
      close(3) = 0
      geteuid() = 1000


      To conclude even if you own a directory and it has the executable bit, you still need read permissions so that you may see its contents and delete the folder. It's not the same for a file though.






      share|improve this answer










      New contributor




      ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        4












        4








        4







        For deletion to occur the system must be able to read the contents and identify what has to be deleted.



        I've tried simulating what you are attempting :



        [vagrant@desktop1 ~]$ sudo rm -rf folder1/ && mkdir -pv folder1/folder2 && sudo chmod 333 -v folder1/ && sudo chmod 777 -v folder1/folder2
        mkdir: created directory 'folder1'
        mkdir: created directory 'folder1/folder2'
        mode of 'folder1/' changed from 0775 (rwxrwxr-x) to 0333 (-wx-wx-wx)
        mode of 'folder1/folder2' changed from 0775 (rwxrwxr-x) to 0777 (rwxrwxrwx)
        [vagrant@desktop1 ~]$ ls -lh
        total 0
        d-wx-wx-wx. 3 vagrant vagrant 21 Feb 24 10:40 folder1
        [vagrant@desktop1 ~]$


        If we try deleting without read permissions it fails:



        [vagrant@desktop1 ~]$ rm -r folder1/
        rm: cannot remove 'folder1/': Permission denied
        [vagrant@desktop1 ~]$ sudo chmod +r folder1/
        [vagrant@desktop1 ~]$ rm -r folder1/
        [vagrant@desktop1 ~]$


        In an strace for the two attempts the difference is that the directory contents cannot be read (getdents):



        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
        openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
        geteuid() = 1000
        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
        faccessat(AT_FDCWD, "folder1/", W_OK) = 0
        openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0


        With read permissions:



        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0777, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
        openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 3
        fstat(3, {st_mode=S_IFDIR|0777, st_size=21, ...}) = 0
        fcntl(3, F_GETFL) = 0x38800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW)
        fcntl(3, F_SETFD, FD_CLOEXEC) = 0
        getdents(3, /* 3 entries */, 32768) = 80
        close(3) = 0
        geteuid() = 1000


        To conclude even if you own a directory and it has the executable bit, you still need read permissions so that you may see its contents and delete the folder. It's not the same for a file though.






        share|improve this answer










        New contributor




        ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        For deletion to occur the system must be able to read the contents and identify what has to be deleted.



        I've tried simulating what you are attempting :



        [vagrant@desktop1 ~]$ sudo rm -rf folder1/ && mkdir -pv folder1/folder2 && sudo chmod 333 -v folder1/ && sudo chmod 777 -v folder1/folder2
        mkdir: created directory 'folder1'
        mkdir: created directory 'folder1/folder2'
        mode of 'folder1/' changed from 0775 (rwxrwxr-x) to 0333 (-wx-wx-wx)
        mode of 'folder1/folder2' changed from 0775 (rwxrwxr-x) to 0777 (rwxrwxrwx)
        [vagrant@desktop1 ~]$ ls -lh
        total 0
        d-wx-wx-wx. 3 vagrant vagrant 21 Feb 24 10:40 folder1
        [vagrant@desktop1 ~]$


        If we try deleting without read permissions it fails:



        [vagrant@desktop1 ~]$ rm -r folder1/
        rm: cannot remove 'folder1/': Permission denied
        [vagrant@desktop1 ~]$ sudo chmod +r folder1/
        [vagrant@desktop1 ~]$ rm -r folder1/
        [vagrant@desktop1 ~]$


        In an strace for the two attempts the difference is that the directory contents cannot be read (getdents):



        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
        openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
        geteuid() = 1000
        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
        faccessat(AT_FDCWD, "folder1/", W_OK) = 0
        openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = -1 EACCES (Permission denied)
        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0333, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0


        With read permissions:



        newfstatat(AT_FDCWD, "folder1/", {st_mode=S_IFDIR|0777, st_size=21, ...}, AT_SYMLINK_NOFOLLOW) = 0
        openat(AT_FDCWD, "folder1/", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_DIRECTORY|O_NOFOLLOW) = 3
        fstat(3, {st_mode=S_IFDIR|0777, st_size=21, ...}) = 0
        fcntl(3, F_GETFL) = 0x38800 (flags O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW)
        fcntl(3, F_SETFD, FD_CLOEXEC) = 0
        getdents(3, /* 3 entries */, 32768) = 80
        close(3) = 0
        geteuid() = 1000


        To conclude even if you own a directory and it has the executable bit, you still need read permissions so that you may see its contents and delete the folder. It's not the same for a file though.







        share|improve this answer










        New contributor




        ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited 2 hours ago









        George

        1104




        1104






        New contributor




        ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 13 hours ago









        ttaran7ttaran7

        413




        413




        New contributor




        ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        ttaran7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502659%2fwhy-is-rm-r-unable-to-delete-this-folder%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wolfgang Unzicker

            Unua mondmilito

            Schloss Hohenburg (Lenggries)