Linux filter files by name

How to use the Linux find command

Linux is an open-source operating system that was created as a free alternative to UNIX. As with UNIX, the command line is a fundamental tool to work in Linux. Here, the user enters a command on the command line which is then executed.

To find a file in Linux, you can use the Linux find command. This starts a recursive search, where a directory hierarchy is searched following certain criteria. The Linux find command is a precise tool for finding files and directories and is supported across pretty much all Linux distributions.

  1. An overview of the Linux find command
    1. Limiting your search to a specific directory
    2. Customize search results output
    1. Using the Linux file command to filter by file name
    2. Using the Linux file command to filter by file type
    3. Using the Linux file command to filter by size
    4. Using the Linux file command to filter by timestamp
    5. Using the Linux file command to filter by owner, group, and access rights
    6. Limiting the recursion depth of the Linux find command
    1. Using the Linux file command to adjust the user and groups
    2. Using the Linux file command to adjust file rights
    3. Using the Linux file command to delete empty directories and files

    Register great TLDs for less than $1 for the first year.

    Why wait? Grab your favorite domain name today!

    An overview of the Linux find command

    To use the find command in Linux, you’ll need to open the command line. Let’s have a look at the general structure of the Linux find command:

    Attention: Be careful with commands on the command line. If you carelessly execute the wrong command, you may damage your system!

    First, the command itself is written, followed by a directory path, and a variable number of search parameters. A search parameter consists of a hyphen that is immediately followed by the name of the parameter. This is followed by a space and the value of the parameter. Below, you’ll find an overview of the most commonly used search parameters:

    Search parameter

    Explanation

    Filter by owner and group

    Several search parameters can also be combined. Here, a logical AND operation is implicitly assumed. This can be written out explicitly. Furthermore, an OR link can be used or a condition can be negated:

    Search parameter

    Explanation

    Search results must meet both conditions

    Search results must meet at least one of the two conditions

    Negate subsequent condition

    Note that you must replace the with actual values in the following code examples to run the examples.

    Limiting your search to a specific directory

    Let’s see how we can limit the search to a specific directory. To search the current directory, we use the “.” item as the directory path:

    To search through your own user folder, use the tilde “~” as the directory path:

    You can also search the entire system with the Linux find command. However, due to the large number of files and the possibly deep hierarchy of directories, this can take a long time. To search the entire system, we use a forward slash “/” as the directory path:

    Attention: Be extremely careful when applying the Linux find command in combination with the “-exec” parameter to the entire system!

    Customize search results output

    The search results of the Linux find command may be extensive. That’s why it can be useful to influence how results are displayed. Here, it’s important not to use the command’s own parameters, but make use of so-called pipes. In Linux, they’re useful to use the output of one command as the input of another command.

    To output the results page by page, we pass the output of the find command to the less command:

    To count the number of results, we pass the output of the find command to the wc command with the “-l” parameter:

    To look at only the first or last n search results, we pass the output of the find command to the “head” and “tail” commands. In each case, we specify the parameter “-n” followed by the desired number of search results:

    Finding files with Linux: simple use cases

    The following examples limit the search to the current directory and any subdirectories. Use the cd command on the command line to change to any directory. To test these examples, first change to your user folder:

    Using the Linux file command to filter by file name

    To filter for file names, use the “-name” parameter. This requires an exact file name and is case sensitive:

    Here we are looking for a file with the exact file name “.gitignore”.

    To not differentiate between lowercase and uppercase letters we use the “-iname” parameter. The “I” here stands for “insensitive”, from “case-insensitive”:

    Usually, it’s more practical to search case-insensitive first and use the “-name” parameter only if the search with “-iname” returns too many results.

    If we don’t want to search for an exact file name, but want to use a pattern, we use the asterisk as a “wildcard” placeholder and write the search pattern in quotes (the asterisk is internally interpreted as “zero until additional characters have been added”). In our example, we search for files and directories whose names contain the text “git”:

    Using the Linux file command to filter by file type

    A famous aspect of UNIX philosophy is the principle that “everything is a file” – and the same applies under Linux. The term “file” refers to files in a broader sense. In other words, directories are also mapped as files under Linux. But to avoid confusion, the more precise term “file descriptor” is sometimes used.

    When we speak of the “file type” under Linux, we’re not talking about whether a document is an Excel file or a JPEG image. Instead, we distinguish between the different file descriptor types that exist under Linux. The Linux find command provides us with the search parameter “-type” to filter by file type. For example, we can distinguish between files and directories when searching. Below, we’ve put together an overview of the most commonly used file types:

    Explanation

    Источник

    How to list files without directories, and filter by name (ls options)

    I know about the -d flag, but it doesn’t get me quite what I want.

    9 Answers 9

    This sounds like a job for find .

    • Use -maxdepth to only return the current directory, not recursivly search inside subfolders
    • Use -type f to only return files and not directories or device nodes or whatever else
    • Use a combination if -not and -name to avoid the files with names you don’t want

    It might come together like this:

    find /path/to/uploads -maxdepth 1 -type f -not -name 't_*' 

    @EmmyS: You might find one more little trick useful here. @Gilles mentioned using -exec ls -lG — <> + in his answer to get the output using extra options to ls. You can also add just an -ls to this find to get a quick and dirty approximation of ls’s detailed view.

    GNU ls (i.e. the ls command on non-embedded Linux systems and Cygwin, also available on some other unices) has an option to hide some files, based on their names. There’s no way to ignore directories though.

    Another approach is to make your shell do the matching. Bash, ksh and zsh have a negation pattern !(t_*) to match all files except those matching t* ; in bash this feature needs to be turned on with shopt -s extglob , and in zsh it needs to be turned on with setopt ksh_glob . Zsh also has the equivalent syntax ^t_* which needs to be turned on with setopt extended_glob . This still doesn’t ignore directories. Zsh has an extra feature that allows to match files not only by name but also by metadata and more: glob qualifiers. Add (.) at the end of a match to restrict to regular files. The negation ^ is part of the name matching syntax, so ^t_*(.) means “all regular files not matching t_* ” and not “all files that aren’t regular files matching t_* ”.

    setopt extended_glob # put this in your ~/.zshrc ls uploads/^t_*(.) 

    If you find yourself without advanced tools, you can do this on any unix with find . It’s not the kind of thing you’d typically type on the command line, but it’s powerful and precise. Caleb has already shown how to do this with GNU find. The -maxdepth option isn’t portable; you can use -prune instead, to portably stop find from recursing.

    find uploads/* -type d -prune -o \! -type f -name 't_*' -print 

    Replace -print by -exec ls -lG — <> + to execute ls with your favorite options on the files.

    All the commands above hide dot files (i.e. files whose name begins with a . ). If you want to display them, pass -A to ls , or add the D glob qualifier in zsh ( ls uploads/^t_*(.D) ). With find , you can use a different approach of making it recurse one level only ( find doesn’t treat dot files specially). This only fully works if you run find in the current directory.

    cd uploads && find . -name . -o -type d -prune -o \! -type f -name 't_*' -print 

    Источник

    Unix Command to List files containing string but *NOT* containing another string

    How do I recursively view a list of files that has one string and specifically doesn’t have another string? Also, I mean to evaluate the text of the files, not the filenames. Conclusion: As per comments, I ended up using:

    find . -name "*.html" -exec grep -lR 'base\-maps' <> \; | xargs grep -L 'base\-maps\-bot' 

    Mikel: However, it will pipe only the file names to the other grep which in turn can only filter from those file names and not file contents.

    I’ve tried that, and agree with Alan that it only filters filenames. Thus, it doesn’t accomplish what I hope.

    @Alan Can’t you pass grep a fileset, and if so, can’t you pass in a fileset based on another grep? This is just me imagining possibilities, but if anyone knew how to do it, that’d be awesome 😀

    6 Answers 6

    Explanation: grep -lr makes grep recursively (r) output a list (l) of all files that contain . xargs loops over these files, calling grep -L on each one of them. grep -L will only output the filename when the file does not contain .

    @chrisaycock adding R and escaping hyphens, does this look right? find . -name «*.html» -exec grep -lR ‘base\-maps’ <> \; | xargs grep -L ‘base\-maps\-bot’

    The use of xargs in the answers above is not necessary; you can achieve the same thing like this:

    find . -type f -exec grep -q <> \; -not -exec grep -q <> \; -print 

    grep -q means run quietly but return an exit code indicating whether a match was found; find can then use that exit code to determine whether to keep executing the rest of its options. If -exec grep -q <> \; returns 0, then it will go on to execute -not -exec grep -q <> \; . If that also returns 0, it will go on to execute -print , which prints the name of the file.

    As another answer has noted, using find in this way has major advantages over grep -Rl where you only want to search files of a certain type. If, on the other hand, you really want to search all files, grep -Rl is probably quicker, as it uses one grep process to perform the first filter for all files, instead of a separate grep process for each file.

    Источник

    Читайте также:  Настройка тонких клиентов линукс
Оцените статью
Adblock
detector