Find files with 'find'

Jun 25, 12:26 AM

Quote of the Day

how to find specific files easily? there are many way to accomplish this task. you can use locate , ls with grep , tree or, simply using find

examples:

  • find files that start with error as name of file -> find . -type f -follow -print | grep error*
  • list file recursively then parse using grep for matching expression -> ls -LR | grep error*

happy searching :)

si.riesal

,

Location post: -6.328350778472211,106.67183876037597
lokasi penulisan via satelit gagal di panggil, harap kontak saya via email :)

---

drop your comments..

  1. find . -name “cookie” -print Start looking in the current directory.

    find ~sigmund -name “cookie” -print Start looking in sigmund’s home directory.

    find /etc -name “cookie” -print Start looking in the /etc directory.

    If you can’t remember the exact name of the file you’re after, but you have some sort of clue about it (for example, it has the word cow in it), you can use wildcards to search:

    find . -name “cow*” -print Look for files beginning with cow.

    find . -name “*cow” -print Look for files ending with cow.

    find . -name “cow“ -print Look for files with cow anywhere within them.

    Finding the right files is one thing, but doing something useful with the found files would be a big plus. You can use the -exec flag to apply a command to each of the found files. The following example will send all the files it finds to the printer (lpr):

    find . -name “*.txt” -exec lpr {} \;

    The set of brackets ({}) is a placeholder that represents the name of each file found, and the slash-semicolon (\;) is a bit of magic that signals the end of the command. So if you had files in the current directory named sample1.txt and sample2.txt, the find command would issue the following commands for you:

    lpr sample1.txt

    lpr sample2.txt

    You can apply any command you like to the found files.

    Using find with the -exec flag is a convenient way to copy, move, print, and even delete groups of files that may be scattered across many different directories. A word of caution, though: If you’re going to apply some potentially dangerous command (like rm) to your files, use the -ok flag instead of -exec. It works the same way, except it prompts you to confirm each command before executing it. Here’s an example:

    find . -name “*.txt” -ok mv {} junkdir \;
    mv sample1.txt junkdir ok? (y/n)
    mv sample2.txt junkdir ok? (y/n)

    — jimbo · Aug 27, 10:39 PM · #

  2. you can do with my own style too :)

    find . -name “*.c” -print | xargs grep “main(”
    grep —color=auto -iRnH ‘getChar();’ *.c -> this will print file names and line number for searched patterns.

    Highlighting searched patterns -> grep —color=auto -iR ‘getChar();’ *.c

    grep -lir “text to find” *
    grep —with-filename —line-number `find -type f`
    grep -v ‘root’ thefile | sort -u | awk ‘{ print NR,$1,$3,$6 }’
    find /path/to/dir -type f -exec grep -l yourstring {} \;

    — gauge · Aug 27, 10:51 PM · #

  3. maybe you can refer to this site for full explanation of find command and examples: http://www.linux.ie/newusers/beginners-linux-guide/find.php”

    — pablo · Aug 27, 11:28 PM · #

 
---