-3

I have a folder with many folders in it and in those folders there are more folders and so on. somewhere in this tree there are files (text files). One of these files contain "find me". I need to find the path of that file. In bash and python (one code for each language).

Of course I don't want you to solve this challenge for me so i have some questions (all the questions are about the two languages):

  1. How do I go over all of the files and directories in a certain directory?
  2. If I got a file/directory path, how can I tell if this path is for a file or for a directory?
  3. (only for bash) How can I read a file content and check if it contains "find me"?
Alex
  • 781
  • 10
  • 23
barak
  • 13
  • 2
  • Please learn about grep command in bash. It can solve your problem, you also need to learn basics of bash if you don't know how to check file or directory. – RBH Oct 03 '14 at 12:26
  • 1) "find --help" 2) Yes 3) "cat /path/to/file" – soerium Oct 03 '14 at 12:28
  • Python: 1 & 2) `os.walk` or `os.listdir` 3) `"find me" in open(fname).read()` – matsjoyce Oct 03 '14 at 12:31
  • in 2 i meant "how can I" and in 3 how I check (via the code) if the file is contain "find me" – barak Oct 03 '14 at 13:02
  • For 2) in bash, see the section "CONDITIONAL EXPRESSIONS" in the bash man page. For Python, see the docs for the os.path module. – PM 2Ring Oct 03 '14 at 14:44

2 Answers2

2

Feel free to try grep:

grep -r "find me" .

or

grep -r "find me" /path/to/your/folder
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

I think this function will solve your question 1 and 2 for Python. (I don't know bash.) It will give you a full list of all directories and files seperately, and also the full paths which is the combination of the two. You can experiment with it and modify it to your liking. Then you need to read all the files you find and look for "find me".

import os

def find_paths(path):
    dir_names = []
    file_names = []
    path_names = []
    for dirname, dirnames, filenames in os.walk(path):
        for filename in filenames:
            # Combine dirname and filename into absolute path:
            filepath = os.path.join(dirname, filename)
            # Add info to lists:
            dir_names.append(dirname)
            file_names.append(filename)
            path_names.append(filepath)
    return dir_names, file_names, path_names

print find_paths(".") # Finds all dirs, files and paths in current folder.
PaulMag
  • 3,928
  • 3
  • 18
  • 29
  • That's right @PM2Ring. It's like this because I only wanted the full paths when I originally made this function. To remove the duplicates you can look at this question: ["Get unique values from a list in python"](http://stackoverflow.com/questions/12897374/get-unique-values-from-a-list-in-python). I do not know if this is the most elegant solution, but it is the one I could find that is supposed to work on all OS. – PaulMag Oct 04 '14 at 22:20
  • Ok. I guess it could be handy to have those 3 synchronised lists. But it seems like a waste of space, since you could just build `path_names` and extract the (path, basename) using `os.path.split()` when required. – PM 2Ring Oct 05 '14 at 01:56