-1

I want to use the find command and only output the files in the console with their path that the find command actually found. Cause if I now use the command it will also display all the files it checked.

The command I used was:

find idle /* > find_idle.txt

It seems to me it just outputs everything it searched through. It doesn't give me any hits on files with "idle" in it. I am a beginner with Linux.

Jan
  • 11
  • 4
    No. find only prints the files that it found. What is your actual problem? Copy-paste the command that you tried, its actual output, and what you expected instead. – Gilles 'SO- stop being evil' Aug 22 '20 at 17:50
  • The comment I used was: find idle /* > find_idle.txt As for the output: https://file.io/F94hiAAnmA1u It's just completely filled up with what seems like everything it searched through. I don't see the results.

    I am a beginner with Linux so this might just be a dumb question

    – Jan Aug 23 '20 at 11:16

2 Answers2

2

Try running:

grep -r idle /

That's valid, but you'll probably notice a problem. You don't want to search files in the virtual filesystem /proc, for example. It might get stuck, take a long time, or just include results from special types of files that you're not interested in.

If you need to search under /, you want something like:

find / -xdev -type f -exec grep -H idle '{}' +
  • /* would skip "hidden" files and directories that are at the top level. If you don't have a very specific aim like that, don't put the * on the end.

  • find doesn't search strings in text files. It has various options. find / -name idle will find files named idle and print their full path. find path-a path-b lists all files under path-a and path-b, which are expected to exist.

    $ find path-a path-b
    find: ‘path-a’: No such file or directory
    find: ‘path-b’: No such file or directory
    
  • find -xdev avoids crossing into a different filesystem, e.g. /proc. If /home is a different filesystem, you might want find / /home -xdev ... instead.

  • find -type f filters out directories and some other special files.

  • find -exec allows running another program with the file names. Using + specifically passes a batch of file names at once, which is more efficient.

sourcejedi
  • 50,249
1

The Windows command line and the Unix command line are completely different. Knowledge of Windows command line utilities won't help you on Unix and vice versa. On the other hand, there are a lot of commonalities between Unix variants such as Linux and macOS, and knowledge on one will help you with the others.

By coincidence, both Windows and Linux have a utility called find. But they do completely different things. Your question was originally poorly received here because nobody imagined that you expected a command called find to search the contents of a file. The Unix find utility searches for files based on their metadata: name, location, modification time, etc. It doesn't look at the file contents.

The Unix command to search file contents is grep. Its main focus is to search inside files, but modern versions of grep can be instructed to automatically traverse directories and search inside all the files in them. To look for files containing the string idle in the current directory:

grep idle *

The * character is a wildcard pattern which is expanded by the shell to the list of files in the current directory. This is one of a very few things that works mostly (but not exactly) the same in Windows. Lose the habit of writing *.*: that excludes files whose name contains a . (unlike Windows where there's special magic for this pattern for historical reasons). * does exclude dot files but that's a story for another time. This command doesn't search files in subdirectories; to do that:

grep -r idle .

Here we don't rely on the shell to list files, since grep will do it on its own.

If you want to look for files on your whole system, you can use

grep -r idle /

but this will search through many system files, so you probably don't want this. See sourcejedi's very good answer on this topic.

If you want to search only for the word “idle”, and not when it's a substring of a word such as “idled” or “sidle”, pass the -w option to grep:

grep -w idle *

If you want to search for “idle” in any case (Idle, IDLE, iDle, …), pass -i:

grep -i idle *

In general, if you're looking for a command to do something, a web search is your best friend. There are ways to search the documentation of a command on your system, but they're typically less effective: you already have to have some idea of what you're looking for. Once you do know the command name, you can look up its documentation with the man command, for example man grep or man find. I recommend reading How do I use man pages to learn how to use commands?. Man pages are often dry summaries that assume that you broadly know what you're doing but have forgotten the details, so don't hesitate to consult other resources.