Find files in linux that contain string

On Linux, how can I find all files that contain a string and delete them?

Please clarify: do you want to delete files whose name contains foo (e.g. myfoo.jpg ), or files that contain the byte sequence foo (which may include binary files which just so happen to contain that sequence of bytes)?

3 Answers 3

grep -lrIZ foo . | xargs -0 rm -f -- 
  • -l prints file names of files matching the search pattern.
  • -r performs a recursive search for the pattern foo in the given directory . . If this doesn’t work, try -R .
  • -I (capital i ) causes binary files like PDFs to be skipped.
  • -Z ensures that file names are zero- (i.e., nul-)terminated so that a name containing white space does not get interpreted in the wrong way (i.e., as multiple names instead of one).
  • xargs -0 feeds the file names from grep to rm -f , separating words by zero (nul) bytes (remember the -Z option from grep ).
  • — is often forgotten but it is very important to mark the end of options and allow for removal of files whose names begin with — .

If you would like to see which files are about to be deleted, simply remove the | xargs -0 rm -f — part, and leave off the Z option to grep .

Another user suggested something like the following, which you should not run because it is unsafe:

files=`grep foo * | cut -d: -f1` rm -f $files # unsafe, do not run it! 

If I have files ImportantStuff that I do not want to delete and obsolete ImportantStuff containing foo , then I lose ImportantStuff (and not obsolete ImportantStuff !) when I run this command, because $files gets broken apart at spaces when it is interpreted. It is dangerous to put a list of filenames into a scalar shell variable in this way.

Источник

How to Find Files Containing Specific Text String in Linux

Almost all file managers for Linux like Nemo and Thunar by default provide an option to search for files. But if you want to search for a string inside a file’s content using file manager, the majority of them do not let you do so.

In this article, I’ll discuss different ways that you can use on any Linux distribution to find all files containing specific text strings or words by recursively digging through all sub-directories.

On this page

Search Specific Text in Linux Using Catfish GUI Tool

I’ll start with the easiest way that can work for all including beginners to advance Linux users. Catfish is a simple and lightweight GUI-based file search tool for Linux desktops. Along with searching for files on your system, you can also use it to find all files that contain a particular word.

Читайте также:  Аналог терминальный сервер linux

Install Catfish Search Tool on Linux

The Catfish package is already available on the primary Debian and Ubuntu repositories. Hence, If you’re using Debian or Ubuntu-based distributions, you can simply install it by running the command:

$ sudo apt install catfish [On Debian/Ubuntu & Mint]

On other Linux distributions, you can install it from the default repositories using your package manager.

$ sudo yum install catfish [On CentOS/RHEL 7] $ sudo dnf install catfish [On CentOS/RHEL 8 & Fedora] $ sudo pacman -S catfish [On Arch Linux] $ sudo pkg_add -v catfish [On FreeBSD]

If the package is not available, you can download the latest release file, extract the downloaded tar.bz2 file, and run the following command:

$ sudo python3 setup.py install

Search File Contents Using Catfish

Once installed, you only need to do is enable “Search file contents”, select directories from the top-left dropdown option, and type the text you want to search for. It will list down all files containing text along with file size and location.

Search Specific Text in Linux Using Catfish Tool

You can also use file type and modified date filter from the left panel to reduce the search scope. You can even add a directory path where you don’t want to search.

Search Text Based on File Types

Search Specific Text in Linux Using Grep Command

Beside the GUI way, grep is one of the popular command line tools that can be used to search inside file content. Since almost all unix-like operating systems ship grep utility by default, you don’t need to install it.

Search for Particular Text in Files

Now to search and find all files for a given text string in a Linux terminal, you can run the following command. Here, the ‘-r’ or ‘-R’ flag recursively searches through the all subdirectories inside the specified directory.

$ grep -r “linuxshelltips” /home/sarvottam/

Search Specific Text in Files

Find File Names That Contains a Given String

If you want to print only file names and hide the text from the output, you can use the ‘-l’ flag.

$ grep -rl “linuxshelltips” /home/sarvottam/

List File Names Contains Specific Text in Linux

Furthermore, you can also tweak the output using the following options available for grep:

$ sudo grep -rnwi “linuxshelltips” /home/sarvottam/

Search Text and Print File Names and Line Numbers

It is also worth mentioning that if you search through directories that require root permissions, you need to use the sudo command.

Search for Specific Text in Specific File Types

To further reduce the search scope, you can also specifically mention the type of file and directories to only look for while searching. “—include” , “—exclude” , and “—exclude-dir” are the options available to add file type and directory filter.

$ grep --include=\*.txt --exclude=\*. --exclude-dir=\bin -rnwi "linuxshelltips" /home/sarvottam/

Search for Specific Text in Specific File Types

Search Specific Text In Linux Using MC (Midnight Commander)

If you live in a terminal and still want to search text using GUI way, you can also try Terminal User Interface (TUI) based file manager tool, mc (midnight commander) – is a visual file manager that is used to search for files.

Читайте также:  Управление яркостью linux mint

Midnight Commander

Install Midnight Commander on Linux

The Midnight Commander is available to install from the default repositories in the most of the Linux distributions.

$ sudo apt install mc [On Debian/Ubuntu & Mint] $ sudo yum install mc [On CentOS/RHEL 7] $ sudo dnf install mc [On CentOS/RHEL 8 & Fedora] $ sudo pacman -S mc [On Arch Linux] $ sudo pkg_add -v mc [On FreeBSD]

Search File Contents Using Midnigh Commander

Inside the terminal, mc provides a visual representation of the filesystem in which you can navigate either through keyboard or mouse. To search file content, you can open a search dialog using ALT+SHIFT+? and enter the text in the “Content:” section.

Search File Contents Using Midnight Commander

As you can see in the above picture, you can also use filters like ignore case, whole words, and regular expression by just checking and unchecking it.

Search File Contents Using Filters

Conclusion

All the above-mentioned applications are beginner’s friendly, free-to-use and open source to add your own enhancements. Alternatively, you can also try other free tools like Ack, The Silver Searcher, Ripgrep, and find command.

Источник

Find all files with name containing string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I have been searching for a command that will return files from the current directory which contain a string in the filename. I have seen locate and find commands that can find files beginning with something first_word* or ending with something *.jpg . How can I return a list of files which contain a string in the filename? For example, if 2012-06-04-touch-multiple-files-in-linux.markdown was a file in the current directory. How could I return this file and others containing the string touch ? Using a command such as find ‘/touch/’

8 Answers 8

find . -maxdepth 1 -name «*string*» -print

It will find all files in the current directory (delete maxdepth 1 if you want it recursive) containing «string» and will print it on the screen.

If you want to avoid file containing ‘:’, you can type:

find . -maxdepth 1 -name «*string*» ! -name «*:*» -print

If you want to use grep (but I think it’s not necessary as far as you don’t want to check file content) you can use:

But, I repeat, find is a better and cleaner solution for your task.

@Dru, if you want it ‘shorter’ you can avoid -print as this is the default behaviour and . as this is the default folder where it checks.

Awesome. I see myself using this a lot. I will take your -print and . removal suggestions, make it a command, and try to pass *string* in as a command line argument.

find . -name «*string*» Works great too. Removing . throws an error on my end. Thanks again @Zagorax.

Just an observation, the above command complained about the position of -maxdepth argument better to move it before -name as @Sunil Dias mentioned

Читайте также:  Linux 32bit on 64bit

I have find *.jpg -name «*from*» -print which works for a given directory. How can I make search recursively? I’ve tried -maxdepth .

-R means recurse. If you would rather not go into the subdirectories, then skip it.

-i means «ignore case». You might find this worth a try as well.

Great. I noticed that some file contents follow a : . Is there anyway to withhold that? Using an option perhaps?

That seems to only produce the contents of the files. You essentially answered my question though, I can try to do some digging for withholding the contents.

Ah. you only need the file names? Run : grep -R «touch» . | cut -d «:» -f 1 (sorry must have misread you).

Thanks @carlspring this is interesting. grep either returns files with contents and filenames containing touch or contents containing touch , I’m not sure which is the case, yet. Of the list of files returned, half contain touch in the title and the other half conatains touch in the body, not the title. Just realized this.

The -maxdepth option should be before the -name option, like below.,

find . -maxdepth 1 -name "string" -print 
find $HOME -name "hello.c" -print 

This will search the whole $HOME (i.e. /home/username/ ) system for any files named “hello.c” and display their pathnames:

/Users/user/Downloads/hello.c /Users/user/hello.c 

However, it will not match HELLO.C or HellO.C . To match is case insensitive pass the -iname option as follows:

find $HOME -iname "hello.c" -print 
/Users/user/Downloads/hello.c /Users/user/Downloads/Y/Hello.C /Users/user/Downloads/Z/HELLO.c /Users/user/hello.c 

Pass the -type f option to only search for files:

find /dir/to/search -type f -iname "fooBar.conf.sample" -print find $HOME -type f -iname "fooBar.conf.sample" -print 

The -iname works either on GNU or BSD (including OS X) version find command. If your version of find command does not supports -iname , try the following syntax using grep command:

find $HOME | grep -i "hello.c" find $HOME -name "*" -print | grep -i "hello.c" 
find $HOME -name '[hH][eE][lL][lL][oO].[cC]' -print 
/Users/user/Downloads/Z/HELLO.C /Users/user/Downloads/Z/HEllO.c /Users/user/Downloads/hello.c /Users/user/hello.c 

If the string is at the beginning of the name, you can do this

$ compgen -f .bash .bashrc .bash_profile .bash_prompt 

compgen is not an appropriate hammer for this nail. This little-used tool is designed to list available commands, and as such, it lists files in the current directory (which could be scripts) and it can neither recurse nor look past the beginning of a file name nor search file contents, making it mostly useless.

An alternative to the many solutions already provided is making use of the glob ** . When you use bash with the option globstar ( shopt -s globstar ) or you make use of zsh , you can just use the glob ** for this.

does a recursive directory search for files named bar (potentially including the file bar in the current directory). Remark that this cannot be combined with other forms of globbing within the same path segment; in that case, the * operators revert to their usual effect.

Note that there is a subtle difference between zsh and bash here. While bash will traverse soft-links to directories, zsh will not. For this you have to use the glob ***/ in zsh .

Источник

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