Linux find file by mask

command to find files by searching only part of their names?

Do you want to list matching files in only the current directory? in any directory at or below the current directory? anywhere on the system?

6 Answers 6

Finding Files with bat Anywhere

To find all files anywhere inside /path/to/folder whose names contain bat , you can use:

find /path/to/folder -name '*bat*' 

I have quoted the search pattern *bat* because, if the quotes were omitted and files match *bat* in the current directory, the shell will expand *bat* into a list of them and pass that to find . Then find wouldn’t work right. ( \*bat\* and «*bat*» also work.)

To search in the folder you’re currently in (e.g., that you’ve cd ed to), use . , as usual:

To search your whole computer, use / . To search your home directory, use ~ , or the full name of your home directory. (The shell expands ~ to your home directory’s fully qualified path.)

Broadening or Narrowing Your Search, Based on Name

If you want to search case-insensitively, so files containing BAT , bAt , and so forth are matched, use the -iname test instead of the -name test:

find /path/to/folder -iname '*bat*' 

I’ve noticed all your files end in .c . If you only want to find files like that, use:

find /path/to/folder -name '*bat*.c' 

I noticed all your filenames have bat either at the very beginning or the very end of the part preceding the .c suffix. If you want to avoid matching files like embattled.c , you could use:

find /path/to/folder -name '*bat.c' -o -name 'bat*.c' 

Matching Only Files

To find only regular files—and not folders, symbolic links, and special device nodes—you can use -type f . This is frequently recommended and sometimes quite appropriate. but often not what you really want, especially if you’re running find for the purpose of examining the output yourself. If you had a symbolic link that matched your search, wouldn’t you want to know about it?

Читайте также:  What is peta linux

If you want to find both regular files and symbolic links, you can use:

find /path/to/folder -name '*bat*' \( -type f -o -type l \) 

That uses the -o operator and also parentheses for grouping (which must be quoted so the shell does not treat them specially; otherwise you’ll get a syntax error).

But suppose you only want to see symbolic links that ultimately point to a regular file (and not symbolic links to directories, device nodes, etc.). That’s actually even simpler: use -xtype instead of -type . Provided you’re not running find with -L flag, -xtype on a symbolic link tests the type of the file the link points to.

find /path/to/folder -name '*bat*' -xtype f 

If you have a symlink to another symlink to a file, -xtype f will match it even though its direct target is another symlink rather than a regular file. This is almost always what you want.

Often people think they want -type f , but really they want -xtype f .

Getting Detailed Output

find ‘s default action if you don’t specify one is -print . All the commands given above are equivalent to themselves with -print tacked on at the end.

find is often used to run commands based on the files found—often, commands that make changes. But there are also other actions whose purpose is to display results, besides -print . Of particular interest is -ls :

find /path/to/folder -name '*bat*' -ls 

This gives detailed information on each file, in a multi-column format, similar to (though not quite the same as) what you would see by running ls file .

Further Reading

For more information on find and other ways to find files, see:

  • The find manual page, accessible online or by running man find in a terminal.
  • The GNU findutils reference manual, providing extensive documentation on the find , locate , and xargs utilities.
  • FindingFiles in the Ubuntu help wiki, which shows how to use find as well as several other methods.

Источник

linux find file by name mask

How do I search for a file with a specific name in Linux?

  1. find . — name thisfile.txt. If you need to know how to find a file in Linux called thisfile. .
  2. find /home -name *.jpg. Look for all . jpg files in the /home and directories below it.
  3. find . — type f -empty. Look for an empty file inside the current directory.
  4. find /home -user randomperson-mtime 6 -iname «.db»
Читайте также:  Подключение файловых систем linux

How do I search for files by name?

  1. Press the Windows key , then type part or all the file name you want to find. .
  2. In the search results, click the Documents, Music, Photos, or Videos section header to view a list of files that meet the search criteria.
  3. Click the file name you want to open.

How do I find a file with a specific pattern in Linux?

To find files that match a specific pattern, use the -name argument. You can use filename metacharacters (such as * ), but you should either put an escape character ( \ ) in front of each of them or enclose them in quotes. All files in the current directory starting with “pro” are listed.

How do I search for a file with a partial name?

path Type: System. String The directory to search. searchPattern Type: System. String The search string to match against the names of files in path.

How do I find a file without knowing the path in Unix?

  1. -name file-name – Search for given file-name. .
  2. -iname file-name – Like -name, but the match is case insensitive. .
  3. -user userName – The file’s owner is userName.

How do I use grep to find a file in Linux?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we’re searching for and finally the name of the file (or files) we’re searching in. The output is the three lines in the file that contain the letters ‘not’.

How do I search for a file?

  1. Introduction.
  2. 1Choose Start→Computer.
  3. 2Double-click an item to open it.
  4. 3If the file or folder that you want is stored within another folder, double-click the folder or a series of folders until you locate it.
  5. 4When you find the file you want, double-click it.

How do I search my entire computer for a file?

Search File Explorer: Open File Explorer from the taskbar or right-click on the Start menu, and choose File Explorer, then select a location from the left pane to search or browse. For example, select This PC to look in all devices and drives on your computer, or select Documents to look only for files stored there.

How do I find a file in Linux terminal?

  1. Open your favorite terminal app. .
  2. Type the following command: find /path/to/folder/ -iname *file_name_portion* .
  3. If you need to find only files or only folders, add the option -type f for files or -type d for directories.
Читайте также:  Color commands in linux

How do I grep an entire directory?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.

How do I find on Linux?

find is a command for recursively filtering objects in the file system based on a simple conditional mechanism. Use find to search for a file or directory on your file system. Using the -exec flag, files can be found and immediately processed within the same command.

POSIX Signals with C Programming

Signal

POSIX Signals with C ProgrammingStandard or Regular Signals: The header file ‘signal. . SIGHUP: This signal will hang-up the processing. . SIGINT.

Linux Mint 20.1 “Ulyssa” Review and Upgrade Guide

Linux

Is Linux Mint 20.1 Ulyssa stable?How do I update Linux Mint 20.1 to Ulyssa?Is Linux Mint 20.1 good?How do I upgrade to the latest version of Linux Min.

How To Create a New MySQL User with Grant Permissions

User

For example, to grant insert privileges to a MySQL user you would run the command: GRANT INSERT ON *. * TO ‘username’@’localhost’; You can replace the.

Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system

Источник

Get list of all files by mask in terminal

I want to find all *.h , *.cpp files in folders with defined mask, like */trunk/src* . So, I can find separately *.h and *.cpp files:

find . -path "*/trunk/src/*.h" find . -path "*/trunk/src/*.cpp" 

What is the best way to get the file-list both of types ( *.h and *.cpp )? PS I’d like to pipe the list to grep .

2 Answers 2

find . -path '*/trunk/src/*.h' -o -path '*/trunk/src/*.cpp' 
find . -path '*/trunk/src/*' \( -name '*.h' -o -name '*.cpp' \) 

If you want to run grep on these files:

find . \( -path '*/trunk/src/*.h' -o -path '*/trunk/src/*.cpp' \) -exec grep PATTERN <> + 
find . -path '*/trunk/src/*' \( -name '*.h' -o -name '*.cpp' \) -exec grep PATTERN <> + 

In bash, turn on the globstar option so that ** matches any level of subdirectories. You can do this from your ~/.bashrc . Also turn on the extglob options to activate ksh extended patterns.

grep PATTERN **/trunk/src/**/*.@(h|cpp) 

Beware that bash versions up to 4.2 follow symlinks to directories when you use ** .

Zsh makes this easier, you don’t need to set any options and can just type

grep PATTERN **/trunk/src/**/*.(h|cpp) 

If the command line is too long, and you’re on Linux or other platform with GNU grep, you can make grep recurse instead of the shell to save on the command line length.

grep -R --include='*.cpp' --include='*.h' PATTERN **/trunk/src 

Источник

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