Linux find file that starts with

6 Examples to Find Files in Linux with Find Command

The Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria.

It can find directories and files by their name, their type, or extension, size, permissions, etc.

Find Command Syntax

The general syntax for the find command is as follows:

find [options] [path. ] [expression]

  • The options attribute controls the treatment of the symbolic links, debugging options, and optimization method.
  • The path… attribute defines the starting directory or directories where find will search the files.
  • The expression attribute is made up of options, search patterns, and actions separated by operators.

Find all the files in the /var/log directory that were modified in the last 7 days:

How to Use find Command in Linux

For example, if you want to find all of the files that have the word “file” in their name, you can run the following command:

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name.

If you want to find all of the files that have the word “file” at the beginning of their name, you can use the following command:

This command will search through the current directory and all of its subdirectories for files that have the word “file” at the beginning of their name.

Advanced options in Find command

The “find” command also allows you to use advanced search options to filter results.

  • find command with the “-type” option to search for files of a specific type.
  • find command with the “-mtime” option to search for files that have been modified in a certain amount of time.
  • find command with the “-maxdepth” option to specify how deep you want to search into directories.

Find all the files in the /var/log directory that were modified in the last 7 days:

Find Files with Name in Linux

To find files with a specific name in Linux, you can use the find command with the -name option. Here’s the basic syntax:

Where path is the directory to search, and filename is the name of the file you want to find. Here are some examples:

To find all files named index.html in the current directory and its subdirectories:

To find all files named config.json in the /etc directory and its subdirectories:

To find all files named file.txt in the /home directory and its subdirectories, but only show the filename and not the full path:

Читайте также:  Linux no arp flag

find /home -name file.txt -printf «%f\n»

To find all files in the current directory and its subdirectories with a name that starts with file and ends with .txt:

These are just a few examples of how to use the find command to find files with a specific name in Linux. The possibilities are almost endless, as you can use various options to refine your search based on different criteria.

Command Description
find . -name howtouselinux.txt Find Files Using Name in Current Directory
find /home -name howtouselinux.txt Find Files Under Home Directory
find / -type d -name howtouselinux Find Directories Using Name
find . -type f -name “*.txt” Find all txt Files in Current Directory

Find Files with Permission in Linux

To find files with a specific permission in Linux, you can use the find command with the -perm option. Here’s the basic syntax:

find [path] -perm [permission]

Where path is the directory to search, and permission is the permission code of the file you want to find. The permission code can be specified in either octal or symbolic notation.

To find all files in the current directory and its subdirectories with 777 permissions:

To find all files in the /var directory and its subdirectories that do not have 777 permissions:

find /var -type f ! -perm 0777

To find all directories in the /home directory and its subdirectories with 755 permissions:

find /home -type d -perm 0755

Command Description
find . -type f -perm 0777 -print Find Files With 777 Permissions
find / -type f ! -perm 777 Find Files Without 777 Permissions
find / -perm 2644 Find SGID Files with 644 Permissions
find / -perm 1551 Find Sticky Bit Files with 551 Permissions

Excluding Files and Directories in Find command

You can use the “find” command with the “-exclude” option to exclude certain files from your search.

For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the PDF files, you can run the following command:

find . -name ‘file*’ -exclude *.pdf

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the PDF files.

You can also use the “-exclude-dir” option to exclude certain directories from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the files in the “tmp” directory, you can run the following command:

find . -name ‘file*’ -exclude-dir tmp

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the files in the “tmp” directory.

Combine options together in Linux find command

You can use multiple options together with the find command to perform a more specific search. Here’s an example that combines several options:

Читайте также:  Linux запуск сетевого интерфейса

find /home -type f -name «*.txt» -size +10M -user john -mtime -30

This command will find all files with the .txt extension that are larger than 10 megabytes, owned by the user john, and have been modified within the last 30 days, in the /home directory and its subdirectories.

Let’s break down the options used in this command:

Option Description
-type f Specifies that we’re only interested in regular files (not directories, symbolic links, etc.)
-name “*.txt” Specifies that we’re looking for files with names that end with .txt
-size +10M Specifies that we’re looking for files that are larger than 10 megabytes
-user john Specifies that we’re only interested in files owned by the user john
-mtime -30 Specifies that we’re looking for files that have been modified within the last 30 days

By combining these options, we can perform a very specific search for files that meet our criteria. You can also use other options with the find command to further refine your search based on other criteria such as permissions, groups, and access times.

Find Files with User and Group in Linux

Command Description
find /home -user howtouselinux Find all Files Based on User
find /home -group howtouselinux Find all Files Based on Group

Find Files and Directories Based on Date and Time in Linux

Command Description
find / -mtime 50 Find Last 50 Days Modified Files
find / -atime 50 Find Last 50 Days Accessed Files
find / -mtime +50 –mtime -100 Find Last 50-100 Days Modified Files
find / -cmin -60 Find Changed Files in Last 1 Hour

Find Files and Directories Based on Size in Linux

Command Description
find / -size 50M Find 50MB Files
find / -size +50M -size -100M Find Size between 50MB – 100MB
find / -type f -size +100M Find larger than 100MB files
find / -type f -name *.mp3 -size +10M Find all .mp3 files with more than 10MB

Find Files with Not operator in Linux

Command Description
find . -type f -not -name “*.html” Find files not ending with the .html file extension
find . -type f ! -name “*.html” Find files not ending with the .html file extension

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

How to find files whose name starts with «a»?

I know I’m missing something obvious and I’m certain it’s to do with my -name part. And it is homework, but I think I’m pretty close to the correct answer. Better ways of writing are always appreciated too!

find /home/caine/thecopy -user caine -size -10240c -name ^a.* | wc 

This gives a wc of 0 0 0 , removing the name expression gives oodles of counts. I’ve tried ^a , ‘^a’ , ‘^a.*’ and all come up with 0 results. TIA folks. 🙂

2 Answers 2

You need to wrap the file name:

find /home/caine/thecopy -user caine -size -10240c -name "a.*" | wc # ^ ^ 

@fedorqui I just tested it here and with ^ it matches nothing. Without it matches anything that begins with a. . Maybe its just my bash version.

Читайте также:  Пакетный менеджер linux centos

Ops, that’s interesting @user000001! I do not have a shell here with me, so I will edit my answer to the one you suggest and tomorrow will check. Thanks for letting me know : )

The argument to -name is a shell pattern, not a regular expression. The ^ and . characters will match literal ^ and . characters, not the beginning of the name and any character, respectively, as they would if it were a regular expression.

If you want to match file names starting with ‘a’ , this should work:

find /home/caine/thecopy -user caine -size -10240c -name 'a*' | wc 

The quotation marks around a* (either single or double) are important; without them, the shell will expand the pattern before find sees it.

If you really want to match a regular expression, you can replace -name with -regex ; that’s a GNU-specific extension. But in this case, a shell pattern is probably good enough.

Note: I’m assuming that you want to match files whose names start with a ; you didn’t actually say so.

Источник

Quickly find all files that start with a given string

Because it uses -exec … + , it will execute awk as few times as possible (based on the maximum argument list size). If awk finds a line that matches the regular expression (i.e., the search string), it will print the filename. (You don’t need the backslash ( \ ) if you put the string in single quotes.) Obviously, if you also want to see the content of the line, you can say

And then, unconditionally, it advances to the next file; so it effectively looks at only the first line of each file. Note: nextfile does not seem to be defined by POSIX; it is probably only in GNU awk .

find . -type f -exec head -1v <> + | egrep -B 2 -nw -e "^\!" 

Well first, you’re going to need -type f at the very least. Unfortunately, this is extremely inefficient, as a new process is created for each file.

@StéphaneChazelas grep -B2 takes care of that (you can further postprocess to get rid of the lines that don’t contain file names).

Also building on others ideas.

Have you thought of xargs ? It can do some parallelization.

find . -type f -print0 | xargs -0 -P4 head -1 | egrep -wn "^\!" 

What it does: looks for all regular file and separate the list of files with ‘\0’ instead of white space.
We instruct xargs to understand that ‘\0’ are the separator and to run 4 concurrent processes (adjust that to your number of cores and disk speed). Xargs will run the head command on each file.
Finally grepping the results with your pattern.

Head will still be run plenty of time. But concurrently! Unless you need to do periodic search of this kind, I would not bother trying to optimize it further. The time it’s taking to optimize more will not be gained by the faster runtime, if you run it only once anyway.

Источник

Оцените статью
Adblock
detector