SearchFiles.py

d
Nombre: SearchFiles.py
Autor: James Knowlton.
Descripción: Script que busca un patrón de archivos y al encontrarlo presenta los archivos y sus permisos UGO correspondientes. Básicamente desarrolla 3 tareas: Obtiene el patrón de búsqueda, realiza la búsqueda de archivos y presenta los resultados en pantalla
Visto en Developer Works
#!/usr/bin/python

import stat, sys, os, string, commands

#Getting search pattern from user and assigning it to a list

try:
    #run a 'find' command and assign results to a variable
    pattern = raw_input("Enter the file pattern to search for:\n")
    commandString = "find " + pattern
    commandOutput = commands.getoutput(commandString)
    findResults = string.split(commandOutput, "\n")

    #output find results, along with permissions
    print "Files:"
    print commandOutput
    print "================================"
    for file in findResults:
        mode=stat.S_IMODE(os.lstat(file)[stat.ST_MODE])
        print "\nPermissions for file ", file, ":"
        for level in "USR", "GRP", "OTH":
            for perm in "R", "W", "X":
                if mode & getattr(stat,"S_I"+perm+level):
                    print level, " has ", perm, " permission"
                else:
                    print level, " does NOT have ", perm, " permission"
except:
    print "There was a problem - check the message above"
Permisos: chmod 700 SearchFiles.py 
Ejecución: ./SearchFiles.py [ Patrón a buscar ]
Ejemplo: ./SearchFiles.py j*.py

0 comentarios: