Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux search file based on file name pattern [closed]

Tags:

linux

unix

I want to search a series of files based on the files name.

Here is my directory :

enter image description here

For example I had the file on above.

I only want to search out the file which is without _bak.

like image 619
crchin Avatar asked Jun 15 '12 02:06

crchin


People also ask

How do I find a file with a specific pattern 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 use grep to find a file name?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do I filter a file name in Linux?

Filter files by name in a directory using grep This is the easiest way to find files (and folders) on Linux systems. grep is a program that is shipped with all Linux and FreeBSD systems (and even macOS). grep is used for filtering data using regular expressions.

How do I find a file using ls in Linux?

The easiest way to list files by name is simply to list them using the ls command. Listing files by name (alphanumeric order) is, after all, the default. You can choose the ls (no details) or ls -l (lots of details) to determine your view.


1 Answers

If you're wanting to limit your search to the current directory, use:

find . -maxdepth 1 -mindepth 1 ! -name '*_bak' 

If you want to recursively find in all directories remove the -maxdepth 1.


Edit in response to OP's comment

To get files that begin with ei and do not end with _bak use:

find . -type f -name 'ei*' -a ! -name '*_bak' 
like image 153
Tim Pote Avatar answered Sep 28 '22 04:09

Tim Pote