Find all writable files in the current directory

后端 未结 10 408
[愿得一人]
[愿得一人] 2020-12-25 11:47

I want to quickly identify all writable files in the directory. What is the quick way to do it?

相关标签:
10条回答
  • 2020-12-25 12:26

    If you are in shell use

    find .  -maxdepth 1 -type f -writable
    

    see man find

    You will find you get better answers for this type of question on superuser.com or serverfault.com

    If you are writing code not just using shell you may be interested in the access(2) system call.

    This question has already been asked on serverfault

    EDIT: @ghostdog74 asked if you removed write permissions for this file if this would still find the file. The answer, no this only finds files that are writable.

    dwaters@eirene ~/temp
    $ cd temp
    
    dwaters@eirene ~/temp/temp
    $ ls
    
    dwaters@eirene ~/temp/temp
    $ touch newfile
    
    dwaters@eirene ~/temp/temp
    $ ls -alph
    total 0
    drwxr-xr-x+ 2 dwaters Domain Users 0 Mar 22 13:27 ./
    drwxrwxrwx+ 3 dwaters Domain Users 0 Mar 22 13:26 ../
    -rw-r--r--  1 dwaters Domain Users 0 Mar 22 13:27 newfile
    
    dwaters@eirene ~/temp/temp
    $ find .  -maxdepth 1 -type f -writable
    ./newfile
    
    dwaters@eirene ~/temp/temp
    $ chmod 000 newfile
    
    dwaters@eirene ~/temp/temp
    $ ls -alph
    total 0
    drwxr-xr-x+ 2 dwaters Domain Users 0 Mar 22 13:27 ./
    drwxrwxrwx+ 3 dwaters Domain Users 0 Mar 22 13:26 ../
    ----------  1 dwaters Domain Users 0 Mar 22 13:27 newfile
    
    dwaters@eirene ~/temp/temp
    $ find .  -maxdepth 1 -type f -writable
    
    dwaters@eirene ~/temp/temp
    
    0 讨论(0)
  • 2020-12-25 12:28

    to find writable files regardless of owner, group or others, you can check the w flag in the file permission column of ls.

    ls -l | awk '$1 ~ /^.*w.*/'
    

    $1 is the first field, (ie the permission block of ls -l) , the regular expression just say find the letter "w" in field one. that's all.

    if you want to find owner write permission

    ls -l | awk '$1 ~ /^..w/'
    

    if you want to find group write permission

    ls -l | awk '$1 ~ /^.....w/'
    

    if you want to find others write permission

    ls -l | awk '$1 ~ /w.$/'
    
    0 讨论(0)
  • 2020-12-25 12:29

    The -writable option will find files that are writable by the current user. If you'd like to find files that are writable by anyone (or even other combinations), you can use the -perm option:

    find -maxdepth 1 -type f -perm /222
    

    This will find files that are writable by their owner (whoever that may be):

    find -maxdepth 1 -type f -perm /200
    

    Various characters can be used to control the meaning of the mode argument:

    • / - any permission bit
    • - - all bits (-222 would mean all - user, group and other)
    • no prefix - exact specification (222 would mean no permssions other than write)
    0 讨论(0)
  • 2020-12-25 12:32
    stat -c "%A->%n" *| sed -n '/^.*w.*/p'
    
    0 讨论(0)
  • 2020-12-25 12:32

    I know this a very old thread, however...

    The below command helped me: find . -type f -perm /+w

    You can use -maxdepth based on how many levels below directory you want to search. I am using Linux 2.6.18-371.4.1.el5.

    0 讨论(0)
  • 2020-12-25 12:34
    find -type f -maxdepth 1 -writable
    
    0 讨论(0)
提交回复
热议问题