2

I have done lots of searching about this, but I cannot seem to find an answer that satisfies my needs. I want a simple way to search for two file types or more (e.g. *.mp3, *.aac, *.pdf, etcetera.) I understand that the Linux command find does this via terminal searches, but I want a program with a GUI for this particular need. I have tried Nemo File Manager's built in search, the same goes for Caja, PCManFM, and others. I have also experimented with catfish and searchmonkey. I just can't seem to get any productive results. Any help is highly appreciated. Thanks in advance.

  • Well, I am not exactly advanced in programming. I am, however, teaching myself C at the moment. Could you go into further detail, please? – crypto Aug 15 '16 at 1:00
  • I started typing here, but the answer got too long. I've put it in a proper Answer. – jcoppens Aug 15 '16 at 4:20
  • How do you define "productive results"? – Satō Katsura Aug 15 '16 at 4:37
  • For instance, if I try to search for two file types using various methods of separation for listing two file extensions, I will only get either results for the first file type in order of the query, or I won't get any at all. By "productive", I mean successfully getting the desired results. – crypto Aug 15 '16 at 10:39
  • Have you tried stuff like gnome-search-tool and the likes ? – don_crissti Aug 15 '16 at 18:57
0

Unless you really need the speed of C, I'd recommend a script language (Ruby, Python or similar). You'll be able to do sophisticated tasks without the tediousness of C (no flames please).

Here's what I'd do:

  • Use the Python-GTK interface to make the GUI: You only need 4 widgets: A Gtk.Window, a Gtk.VBox (to store the next two items), A Gtk.Entry to enter the file types, and a Gtk.TreeView to show the results

  • From Python you can easily combine the list from the Gtk.Entry and the command find + options + file types, to get the list. (You can execute the find command from subprocess.checkcall).

Checkcall returns the list, which you can then show in the Gtk.TreeView.

If you'd like to learn Python, there are plenty excellent tutorials on the 'net. This is the official tutorial on Python's site. The learnPython site is very interesting, as you can even try out your programs. This tutorial should give you all you need to use the GUI stuff in Python with plenty of examples.

A script language like Python is ideal to solve problems like the one you describe.

EDIT I had some time, and thought that it wouldn't be bad to have a compact GUI finder. Below is a working code. Yes, it's longer than 20 lines, mainly because I constructed the entire GUI in code. If I used Glade (a GUI designer), the code would be halved. Also, I added a dialog to select the root of the search.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  test_finder.py
#  
#  Copyright 2016 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  


from gi.repository import Gtk
import os, fnmatch

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())
        self.set_size_request(600, 400)

        vbox = Gtk.VBox()
        # Pattern entry
        self.entry = Gtk.Entry()
        self.entry.connect("activate", self.can't open file 'pyfind.py'on_entry_activated)

        # Initial path selection
        self.path = Gtk.FileChooserButton(title = "Select start path",
                                          action = Gtk.FileChooserAction.SELECT_FOLDER)
        self.path.set_current_folder(".")

        # The file list + a scroll window
        self.list = self.make_file_list()
        scrw = Gtk.ScrolledWindow()
        scrw.add(self.list)

        vbox.pack_start(self.entry, False, False, 0)
        vbox.pack_start(self.path, False, False, 0)
        vbox.pack_start(scrw, True, True, 0)

        self.add(vbox)
        self.show_all()

    def make_file_list(self):
        self.store = Gtk.ListStore(str, str)
        filelist = Gtk.TreeView(model = self.store, enable_grid_lines = True)

        renderer = Gtk.CellRendererText()
        col = Gtk.TreeViewColumn("Files:", renderer, text = 0, sizing = Gtk.TreeViewColumnSizing.AUTOSIZE)
        filelist.append_column(col)
        col = Gtk.TreeViewColumn("Path:", renderer, text = 1)
        filelist.append_column(col)

        return filelist

    def on_entry_activated(self, entry):
        self.store.clear()
        patterns = entry.get_text().split()
        path = self.path.get_filename()

        for root, dirs, files in os.walk(path):
            for name in files:
                for pat in patterns:
                    if fnmatch.fnmatch(name, pat):
                        self.store.append((name, root))

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

View of the GUI finder

Manual:

  1. Check if you have Python3 installed (python -V). If not, install it.
  2. Save the above program as, say pyfind.py
  3. Make it executable chmod 755 pyfind.py
  4. Execute it with ./pyfind.py
  5. Type in the filter, press Enter.
  6. If you change the path, repeat 1 (can avoid that by using a signal from the FileChooserButton, if wanted)

EDIT2

I'm not sure what's happening in you case, but the log says python3 is not finding the pyfind.py file. You can make the file directly executable:

Change the first line of the code to:

#!/usr/local/bin/python3

To make the above file executable, do (in a terminal window, in the directory where pyfind.py is):

chmod 755 pyfind.py

You should then be able to execute the program simply like this (again, in the terminal):

./pyfind.py
  • I apologize if this is a very elementary question, but I am not knowledgeable when it comes to Python or replicating much of anything you described above. I went off of what I thought I knew. I copied your script, saved it to a file named "find.py" to my desktop, ran chmod 755 find.py, then got the following results: "find.py:26: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. from gi.repository import Gtk" – crypto Aug 16 '16 at 2:34
  • I don't know what to do from there. Could you help me out? Thanks. – crypto Aug 16 '16 at 2:37
  • Edited the answer. – jcoppens Aug 16 '16 at 3:14
  • I am currently compiling Python3. I will comment once I have results. Thanks! – crypto Aug 16 '16 at 3:34
  • Okay, I got the following after running "./pyfind.py": Traceback (most recent call last): File "./pyfind.py", line 26, in <module> from gi.repository import Gtk ImportError: No module named 'gi' – crypto Aug 16 '16 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.