Find all files and folders linux

How To Find Files, Folders and Directories In Linux From Command Line?

Linux provides different ways to find and locate files and folders. We can use GUI tools like GNOME and KDE file managers or other 3’rd party applications designed for finding files. In this tutorial, we will look at how to find files, folders, and directories from the command line.

Tools To Find Files and Folder In Linux

As stated previously there are a lot of tools that can be used to find files and folders. We will look in detail all of them. Here list of these tools.

Find Command

find command is very featureful command used with a lot of different options. More details about find command can be found from the following tutorial.

Find Only Files

We can search only files by providing file type as -type f . We will search files those named conf in this example. We will use the glob start and end of the search term in order to accept any prefix or postfix for the search term. So this will match conffff , myconf , myconfffff , myconfiguration.txt etc.

Alternatively, we can specify the path we want to search for the given file name. We will provide the path according to . . In this example, we will search in the /etc path.

Find Only Folders

We may need only to find the folder. We will specify the type like below a directory.

Locate Command

locate command can be used as an offline database of all files and folders. locate will search a database which is created with updatedb command. More detailed information can get from the following tutorial.

As locate database only holds file and folder names we can not search in detail. But this database provides us very fast search option then find command because it works offline.

Update Database

As stated previously locate uses a database to search files and folders. Updating this database is important before a search. The update will take very little time.

Search For File or Folders

We will use locate command and the file and folder name to search.

Grep Command

grep command mainly filters given text and files contents but we can use it to find files and folders. For more detail

We can use ls command recursively and grep the files and folder we want to find. In this example, we will search for files and folders whose names contain backup .

Which Command

which command is not an actual file and folder search. which command simply search current environment executable files. This is generally useful if we are looking for a command which is not included in PATH variable and can not use automatically.

Whereis Command

whereis command is used to list given search term related binary, source, or man page files. In this example, we will search for ls binary and related man page files.

Источник

How to Find Files in Linux Using the Command Line

When you have to find a file in Linux, it’s sometimes not as easy as finding a file in another operating system. This is especially true if you are running Linux without a graphical user interface and need to rely on the command line. This article covers the basics of how to find a file in Linux using the CLI. The find command in Linux is used to find a file (or files) by recursively filtering objects in the file system based on a simple conditional mechanism. You can use the find command to search for a file or directory on your file system. By using the -exec flag ( find -exec ), files can be found and immediately processed within the same command.

Find a File in Linux by Name or Extension

Use find from the command line to locate a specific file by name or extension. The following example searches for *.err files in the /home/username/ directory and all sub-directories:

Using Common find Commands and Syntax to Find a File in Linux

find expressions take the following form:

  • The options attribute will control the find process’s behavior and optimization method.
  • The starting/path attribute will define the top-level directory where find begins filtering.
  • The expression attribute controls the tests that search the directory hierarchy to produce output.

Consider the following example command:

This command enables the maximum optimization level (-O3) and allows find to follow symbolic links ( -L ). find searches the entire directory tree beneath /var/www/ for files that end with .html .

Читайте также:  Консольный linux под windows

Basic Examples

Command Description
find . -name testfile.txt Find a file called testfile.txt in current and sub-directories.
find /home -name *.jpg Find all .jpg files in the /home and sub-directories.
find . -type f -empty Find an empty file within the current directory.
find /home -user exampleuser -mtime -7 -iname «.db» Find all .db files (ignoring text case) modified in the last 7 days by a user named exampleuser.

Options and Optimization for find

The default configuration for find will ignore symbolic links (shortcut files). If you want find to follow and return symbolic links, you can add the -L option to the command, as shown in the example above.

find optimizes its filtering strategy to increase performance. Three user-selectable optimization levels are specified as -O1 , -O2 , and -O3 . The -O1 optimization is the default and forces find to filter based on filename before running all other tests.

Optimization at the -O2 level prioritizes file name filters, as in -O1 , and then runs all file-type filtering before proceeding with other more resource-intensive conditions. Level -O3 optimization allows find to perform the most severe optimization and reorders all tests based on their relative expense and the likelihood of their success.

Command Description
-O1 (Default) filter based on file name first.
-O2 File name first, then file type.
-O3 Allow find to automatically re-order the search based on efficient use of resources and likelihood of success.
-maxdepth X Search current directory as well as all sub-directories X levels deep.
-iname Search without regard for text case.
-not Return only results that do not match the test case.
-type f Search for files.
-type d Search for directories.

Find a File in Linux by Modification Time

The find command contains the ability to filter a directory hierarchy based on when the file was last modified:

The first command returns a list of all files in the entire file system that end with the characters conf and modified in the last seven days. The second command filters exampleuser user’s home directory for files with names that end with the characters conf and modified in the previous three days.

Use grep to Find a File in Linux Based on Content

The find command can only filter the directory hierarchy based on a file’s name and metadata. If you need to search based on the file’s content, use a tool like grep . Consider the following example:

This searches every object in the current directory hierarchy ( . ) that is a file ( -type f ) and then runs the command grep «example» for every file that satisfies the conditions. The files that match are printed on the screen ( -print ). The curly braces ( <> ) are a placeholder for the find match results. The <> are enclosed in single quotes ( ‘ ) to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon ( ; ), which should be escaped ( \; ) to avoid interpretation by the shell.

How to Find and Process a File in Linux

The -exec option runs commands against every object that matches the find expression. Consider the following example:

This filters every object in the current hierarchy ( . ) for files named rc.conf and runs the chmod o+r command to modify the find results’ file permissions.

The commands run with the -exec are executed in the find process’s root directory. Use -execdir to perform the specified command in the directory where the match resides. This may alleviate security concerns and produce a more desirable performance for some operations.

The -exec or -execdir options run without further prompts. If you prefer to be prompted before action is taken, replace -exec with -ok or -execdir with -okdir .

How to Find and Delete a File in Linux

To delete the files that end up matching your search, you can add -delete at the end of the expression. Do this only when you are positive the results will only match the files you wish to delete.

In the following example, find locates all files in the hierarchy starting at the current directory and fully recursing into the directory tree. In this example, find will delete all files that end with the characters .err :

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on Monday, October 25, 2010.

Источник

How to Find Files and Folders on Linux

Unless you’re a perfectionist whose files are all diligently labeled and organized, chances are you’ve had to search for a file at least once in your life. If you’re a new Linux user, you might be wondering how to find files on Linux. The good news is there are several ways to do it, which means that everyone can choose the method that suits them best.

Generally speaking, there are two types of apps that help you find files and folders on Linux. The first are those that search the live filesystem every time. The second type are apps that build an index of files, then perform searches on the index. In this article you’ll find a balanced mix of both, and you can combine them depending on your needs.

How to Find Files in the Terminal

Yes, I know…you’re not a fan of the command-line interface. That’s cool – keep reading and you’ll discover apps that are more to your liking. However, don’t think you can escape the commands. Most of the apps on this list are just graphical interfaces for find and/or locate, so you’ll still be using them, only not directly.

find

Let’s start with the most important command. Find is one of the essential Linux utilities. It looks for a string in the directories you’ve set according to parameters (“switches”) that you’ve included. This example:

find /home/username/Documents -iname “writ*” -type f

means that you’re performing a case-insensitive ( -iname ) search for files ( -type f ) in the Documents folder, and their filenames begin with “writ”. As you can see, find supports wildcards, and you can also use them to find files by their extension (for example, “*.pdf” to find all PDF files in a folder).

You can search for empty files with the -empty option, or find files by size and modification time. Find supports regular expressions, and if you want to search file contents, you can combine it with grep. To learn more, check the official documentation (or just type man find in the terminal).

locate

Locate uses a different approach. It relies on the updatedb utility which creates a database of your files and periodically updates it via cron scheduling. This lets locate know which files are currently present on your filesystem. You can also update the database manually whenever you want.

Locate can search for files by name, and you can use wildcards and regular expressions in your query. For instance:

locate -ei grub.cfg

will list the paths to all existing (-e) files called “grub.cfg”. The -i option stands for “case-insensitive”. If you don’t know the full name of the file you’re looking for, just type a part of it, and locate will display all files with the word in their name.

whereis

This command has a very specific purpose, so you probably won’t use it every day. Whereis shows you the location of the source, binaries, and user manuals for a given application. This means you won’t run whereis when you want to find a random text file. You will, however, use it when you need to check where GIMP or Firefox keep their configuration and executable files.

You can run whereis without any options to get a list of all files, or add switches for their respective functions (-b for binaries, -s for source, and -m for manuals).

How to Use a File Manager to Find Files

Most file managers for Linux can filter files by name or perform basic searches. If you don’t need any advanced parameters, this is a quick method that does the job.

Nautilus

Access the search function (highlighted in green on the screenshot) by pressing Ctrl+F or by clicking the magnifying glass icon in the toolbar. The search is case-insensitive, so you don’t have to worry about capitalizing your queries. You can filter files by type and location, although the latter is somewhat limited in terms of what you can adjust.

Dolphin

Dolphin’s search responds to the same keyboard shortcut (Ctrl+F), or you can open it from the Edit menu. It lets you filter files by name, content and location (current folder or the whole filesystem). If you have enabled file indexing with Baloo, Dolphin will be able to find files by type and modification date.

Krusader

Krusader is popular among KDE users as a Dolphin alternative thanks to its abundance of advanced options. Krusader’s file search functionality is two-fold: it works as a GUI for both find and locate commands.

The former lets you tweak many details, such as file type, included or excluded directories, size, ownership, and file permissions. Krusader can search for keywords within files and even archives (like ZIP and TAR), and you can use regular expressions to customize your query. If you’ve never tried Krusader, now is the time to give it a chance.

Thunar

Thunar integrates with the file search utility called Catfish to provide fast yet detailed results. You can filter files by type and modification date, and search file contents as well as their names. Catfish supports fuzzy (incomplete) filename matching, so you don’t have to know the exact name of the file you’re looking for.

How to Search for Files with Launchers

Launchers are usually used for, well, launching apps. However, you can also use them to find files by enabling various plugins. They’re quick and practical – you just start typing and the results pop right up. There are many launchers for Linux; we’ll focus on just a few examples.

Kupfer

Kupfer is a simple launcher available in the repositories of Debian, Ubuntu, Fedora, and Arch Linux. It comes with a bunch of plugins that let you find files with the locate command, and it can create its own catalog of indexed folders.

Kupfer is an action-based launcher. After typing in your search keyword, Kupfer will list actions that you can perform on/with the results. These depend on the plugins you’ve enabled, and you can activate them by selecting them in the drop-down menu.

KRunner

KRunner is the default KDE launcher that you can configure in the System Settings – Plasma Search dialogue.

Like Kupfer, it supports numerous plugins that help you not only find files, but also interact with other Linux applications and parts of the Plasma desktop environment. KRunner can search YouTube and Wikipedia, show your recent documents, find files by type, and much more.

Albert

Albert is inspired by the Alfred launcher for OS X. Although it looks simple, Albert has plenty of options to play with. It also has – you guessed it – plugins, with “Files” being the most important here.

This plugin lets you create an index of directories that Albert will monitor and rely on. You can enable fuzzy (incomplete) matching and choose which types of files should be indexed. To find files, simply run Albert by pressing the designated keyboard shortcut and start typing your query.

Mutate

Another Alfred-inspired launcher for Linux, Mutate doesn’t have as many options as Albert. Still, it features multiple search types, including file search. You can look for files by name and by file extension. The Preferences dialogue is somewhat unusual, because it shows which scripts Mutate is using, but doesn’t let you configure much apart from keywords and keyboard shortcuts.

Finding Files with Specialized Linux Apps

So far we’ve covered mostly simple file search solutions. They’re great for everyday lookups, but not so useful when it comes to complex queries and file contents search. If you need something more powerful, consider the following suggestions.

GNOME Search for Files

In case GNOME Search not installed on your distribution, look for the gnome-search-tool package in the repository. GNOME Search is powered by locate, find, and grep commands, and supports wildcards as well as partial filename matching. You can combine multiple search options by choosing them from the drop-down menu and clicking “Add”.

KFind

KFind is the KDE equivalent of GNOME Search with a few extra options. It can search for filenames or file contents, and if you’ve enabled file indexing on your KDE system, it can search the index to speed up the process. The options are divided into tabs, and the last tab (“Properties”) lets you find files by size, modification date, and ownership. Apart from regular expressions and wildcards, KFind supports the question mark as a stand-in for a single character in your query. For example, searching for “no?es” will find files named “noses”, “notes”, “nodes”, and so on.

Unity Dash

Ubuntu users faithful to the Unity desktop will be familiar with the Dash. Unity Dash is capable of finding your files and folders according to several parameters (filename, modification date, file type, size). To extend its functionality, you can install various Unity Scopes and Lenses. They integrate external services into Dash, enabling it to search for your browser bookmarks, Google Docs files, web history, and more.

SearchMonkey

SearchMonkey is a relatively old, but still completely functional desktop search app. Regular expressions are its main focus, and it has a “Test Regular Expression” tool that helps you build them. SearchMonkey supports all the essential search parameters (filenames, modification date, size, and file contents), plus the option to restrict the recursive search depth to a selected number of folders.

You can also save search results as a CSV file and limit the amount of results for every query.

DocFetcher

DocFetcher is a desktop search engine for people who often need to search for file contents instead of just filenames. Think researchers, students, and other users who work with large collections of text-based files. DocFetcher first builds a database of files and folders that you choose. This database is automatically updated whenever DocFetcher detects that you’ve modified the files.

When searching for files, you can filter them by type and size, or use regular expressions for fine-grained queries. DocFetcher can search within PDF, EPUB, HTML, RTF, and Office files, as well as within archive files (ZIP, TAR, 7z…) and even Outlook emails.

One great thing about DocFetcher is that it has a portable version, so you can carry your database and the app on a USB stick and use it anywhere.

Recoll

Recoll is probably the most powerful desktop search engine for Linux. It’s similar to DocFetcher: you use it to search through file contents. On first run, it will prompt you to create an index of files. You can select which directories and file types will be indexed, and limit files by size. Recoll will then set up an update schedule so that the index is always synchronized with the actual files. If you want, you can create multiple file indexes and search for files only in one, or in all of them.

You can look up files by name or search for keywords within files. Recoll lets you filter results by several criteria. It can also show related or similar files, search for phrases within files, and recognize word forms thanks to support for stemming. This means that you can search for “work”, and the results will include files that contain “working”, “worked”, “workers”…

Supported file formats include regular text files, logs, man pages, HTML, PDF, CHM, RTF, DJVU, and EPUB files, Libre and Microsoft Office files (including Excel and Powerpoint documents), TAR, RAR, 7z and ZIP archives. Note that external libraries or helper utilities might be required for some of them.

Since most file search tools support the same options, choosing one is largely a matter of convenience, or deciding what works best for your typical workflow. This list might seem long, but there are more file search utilities for Linux. We’ll mention ANGRYsearch, a new project that strives to be the fastest search tool. And what about you? Do you know any other apps for finding files on Linux? What do you use – and can you share some tips? Let us know in the comments.

Источник

Читайте также:  Simple djvu windows 10 крякнутый
Оцените статью