linux - Finding not empty directories without subdirectories and specific sorting

07
2014-07
  • Sebastian Potasiak

    I have a problem with my "homework" on studies.

    I have to list all not empty directories from /var and /usr, which do not have subdirectories and their owner is not root user. Also, for each directory I have to show depth in directory tree, i-node number, size, permissions in human-readable and octal formats and absolute path to this directory, and sort it descending by i-node number.

    Here is what I've currently done:

    find /{us,va}r -type d \! -user root \! -empty -printf "%d %i %k %M %m %u %h/%f\n" | sort -rn
    

    Now I just have to eliminate directories with subdirectories and sort it by i-node number.

    So, here comes the questions:

    1. How can I eliminate directories with subdirectories from this list?
    2. How can I sort this list by i-node, which is in the second column?

    Thanks for help.

  • Answers
  • tink

    OK, I think I found a python-based solution to your problem.

    Save this snippet as e.g. x.py, and chmod +x x.py

    #!/usr/bin/python
    import sys
    x=[]
    for line in sys.stdin:
      x.append(line.rstrip())
    
    y=x[:]
    for i in x:
      mark=x.index(i)
      for j in y:
        if i.split()[6]  in j.split()[6]  and i != j:
          if i in y: y.remove(i)
    
    for j in y:
      print j
    

    Then pipe your find command (w/o the links bit) through it, and sort

    find /{us,va}r -type d \! -user root \! -empty -printf "%d %i %k %M %m %u %h/%f\n" | x.py | sort -k2,2n
    
  • Sebastian Potasiak

    So, I was right. All I had to do was to add -links 2 argument to find, so it will output directories with only 2 "hard links" (which are not hard links - it's subdirectory counter and every directory has at least 2 subdirs - '.' and '..') and -k 2 to sort, so it will sort by second column.

    Whole command looks like this:

    find /{us,va}r -links 2 -type d \! -user root \! -empty -printf "%d %i %k %M %m %u %h/%f\n" | sort -rnk 2
    

  • Related Question

    command line - How to find the executable files under a certain directory in linux?
  • HaiYuan Zhang

    How to find the executable files under a certain directory in linux?


  • Related Answers
  • knittl

    use the -executable option:

    find <dir> -executable
    

    if you want to find only executable files and not searchable directories, combine with -type f:

    find <dir> -executable -type f
    

    EDIT:

    checking with the comments i see there’s no type x. i’m sorry, this was my mistake. checking for executable files can be done with -perm (not recommended) or -executable (recommended, as it takes ACL into account).

  • innaM

    Use the find's -perm option. This will find files in the current directory that are either executable by their owner, by group members or by others:

    find . -perm /u=x,g=x,o=x
    

    Edit:

    I just found another option that is present at least in GNU find 4.4.0:

    find . -executable
    

    This should work even better because ACLs are also considered.

  • AjayKumarBasuthkar

    A file marked executable need not be a executable or loadable file or object.

    Here is what I use:

    find ./ -type f -name "*" -not -name "*.o" -exec sh -c '
        case "$(head -n 1 "$1")" in
          ?ELF*) exit 0;;
          MZ*) exit 0;;
          #!*/ocamlrun*)exit0;;
        esac
    exit 1
    ' sh {} \; -print