Welcome, Guest
Username Password: Remember me

Find files with the Linux find command
(1 viewing) (1) Guest
Shell Script
  • Page:
  • 1

TOPIC: Find files with the Linux find command

Find files with the Linux find command 1 year, 1 month ago #640

  • rajeshkumar
  • OFFLINE
  • Moderator
  • I love software configuration management
  • Posts: 370
  • Points: 44589
  • Karma: 4
  • Honor Medal 2009
This first Linux find example searches through the root filesystem ("/") for the file named "Chapter1". If it finds the file, it prints the location to the screen.

find / -name Chapter1 -type f -print


A nice thing to know is that on Linux systems and modern Unix system you no longer need the -print option at the end of the find command, so you can issue it like this:

find / -name Chapter1 -type f


This next find command searches through the /usr and /home directories for the file named Chapter1:

find /usr /home -name Chapter1 -type f


To search in the current directory, and all subdirectories, just use the . character to reference the current directory in your find commands, like this:

find . -name Chapter1 -type f



This next command searches through the /usr directory for all files that begin with the letters Chapter, followed by anything else. The filename can end with any other combination of characters. It will match filenames such as Chapter, Chapter1, Chapter1.bad, Chapter-in-life, etc.:

find /usr -name "Chapter*" -type f


This next command searches through the /usr/local directory for files that end with the extension .html. These file locations are then printed to the screen.

find /usr/local -name "*.html" -type f


To find all directories named build under the current directory, use this command:

find . -type d -name build


This next command shows how to find all files beneath the current directory that end with the extension .java, and contain the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a match is found, instead of printing all the matches themselves:

find . -type f -name "*.java" -exec grep -l StringBuffer {} \;
Regards,
Rajesh Kumar
Build and Release Engineer
My Blog: community.scmgalaxy.com/pg/profile/rajeshkumar

Re: Find files with the Linux find command 1 year, 1 month ago #641

  • rajeshkumar
  • OFFLINE
  • Moderator
  • I love software configuration management
  • Posts: 370
  • Points: 44589
  • Karma: 4
  • Honor Medal 2009
This next example is similar, but here I use the -i argument to the grep command, telling it to ignore the case of the characters string, so it will find files that contain string, String, STRING, etc.:

find . -type f -name "*.java" -exec grep -il string {} \;


This command searches through the /usr/local directory for files that end with the extension .html. When these files are found, their permission is changed to mode 644 (rw-r--r--).

find /usr/local -name "*.html" -type f -exec chmod 644 {} \;


This command searches through the htdocs and cgi-bin directories for files that end with the extension .cgi. When these files are found, their permission is changed to mode 755 (rwxr-xr-x). This example shows that the find command can easily search through multiple sub-directories (htdocs, cgi-bin) at one time.

find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;



From time to time I run the find command with the ls command so I can get detailed information about files the find command locates. To get started, this find command will find all the "*.pl" files (Perl files) beneath the current directory:

find . -name "*.pl"


In my current directory, the output of this command looks like this:

./news/newsbot/old/3filter.pl
./news/newsbot/tokenParser.pl
./news/robonews/makeListOfNewsURLs.pl


That's nice, but what if I want to see the last modification time of these files, or their filesize? No problem, I just add the "ls -ld" command to my find command, like this:

find . -name "*.pl" -exec ls -ld {} \;
Regards,
Rajesh Kumar
Build and Release Engineer
My Blog: community.scmgalaxy.com/pg/profile/rajeshkumar

Re: Find files with the Linux find command 1 year, 1 month ago #642

  • rajeshkumar
  • OFFLINE
  • Moderator
  • I love software configuration management
  • Posts: 370
  • Points: 44589
  • Karma: 4
  • Honor Medal 2009
To perform a case-insensitive search with the Unix/Linux find command, use the -iname option instead of -name. So, to search for all files and directories named foo, FOO, or any other combination of uppercase and lowercase characters beneath the current directory, use this command:

find . -iname foo


If you're just interested in directories, search like this:

find . -iname foo -type d


And if you're just looking for files, search like this:

find . -iname foo -type f
Regards,
Rajesh Kumar
Build and Release Engineer
My Blog: community.scmgalaxy.com/pg/profile/rajeshkumar

Re: Find files with the Linux find command 1 year, 1 month ago #643

  • rajeshkumar
  • OFFLINE
  • Moderator
  • I love software configuration management
  • Posts: 370
  • Points: 44589
  • Karma: 4
  • Honor Medal 2009
To find all files modified on a certain date, for example, '2007-06-07' the final input is:

find . -type f -newermt 2011-04-20 ! -newermt 2011-04-21


Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime
0, a file will have to have a modification in the past which is less than 24 hours ago.



find . -mtime 0
Regards,
Rajesh Kumar
Build and Release Engineer
My Blog: community.scmgalaxy.com/pg/profile/rajeshkumar
  • Page:
  • 1
Time to create page: 0.70 seconds

     
    
Home Forum