Find command in linux ------------------------------
basic 'locate document' instructions
--------------------------
locate / -call foo.Txt -type f -print # full command
locate / -call foo.Txt -kind f # -print isn't always essential
discover / -call foo.Txt # do not must specify "type==file"
discover . -call foo.Txt # seek under the modern-day dir
locate . -name "foo.*" # wildcard
locate . -call "*.Txt" # wildcard
locate /customers/al -name Cookbook -type d # search '/users/al' dir
search multiple dirs
--------------------
discover /decide /usr /var -name foo.Scala -kind f # seek more than one dirs
case-insensitive searching
--------------------------
discover . -iname foo # locate foo, Foo, FOo, FOO, and so forth.
Locate . -iname foo -type d # equal component, however most effective dirs
locate . -iname foo -type f # identical component, however best files
discover documents with exceptional extensions
------------------------------------
locate . -kind f ( -name "*.C" -o -call "*.Sh" ) # *.C and *.Sh files
discover . -type f ( -call "*cache" -o -call "*xml" -o -call "*html" ) # 3 patterns
locate documents that do not match a pattern (-now not)
--------------------------------------------
find . -type f -now not -call "*.Html" # discover all files not finishing in ".Html"
locate documents by using text inside the report (locate + grep)
--------------------------------------------
locate . -kind f -call "*.Java" -exec grep -l StringBuffer ; # find StringBuffer in all *.Java documents
discover . -type f -name "*.Java" -exec grep -il string ; # ignore case with -i alternative
discover . -type f -call "*.Gz" -exec zgrep 'GET /foo' ; # look for a string in gzip'd documents
five strains earlier than, 10 strains after grep matches
-------------------------------------------
discover . -type f -call "*.Scala" -exec grep -B5 -A10 'null' ;
(see http://alvinalexander.Com/linux-unix/discover-grep-print-lines-before-after-search-term)
discover documents and act on them (locate + exec)
----------------------------------------
find /usr/local -call "*.Html" -type f -exec chmod 644 ; # trade html files to mode 644
locate htdocs cgi-bin -call "*.Cgi" -kind f -exec chmod 755 ; # change cgi files to mode 755
locate . -name "*.Pl" -exec ls -ld ; # run ls command on files discovered
locate and copy
-------------
find . -type f -name "*.Mp3" -exec cp /tmp/MusicFiles ; # cp *.Mp3 files to /tmp/MusicFiles
copy one report to many dirs
--------------------------
find dir1 dir2 dir3 dir4 -kind d -exec cp header.Shtml ; # replica the file header.Shtml to those dirs
find and delete
---------------
locate . -kind f -call "Foo*" -exec rm ; # put off all "Foo*" documents below contemporary dir
find . -kind d -call CVS -exec rm -r ; # dispose of all subdirectories named "CVS" under cutting-edge dir
discover files by change time
-------------------------------
locate . -mtime 1 # 24 hours
locate . -mtime -7 # final 7 days
find . -mtime -7 -type f # just files
locate . -mtime -7 -kind d # just dirs
locate files by way of amendment time using a temp record
-------------------------------------------------
contact 09301330 poop # 1) create a temp report with a selected timestamp
discover . -mnewer poop # 2) returns a listing of new documents
rm poop # 3) rm the temp record
find with time: this works on mac os x
--------------------------------------
locate / -newerct '1 minute in the past' -print
find and tar
------------
discover . -kind f -name "*.Java" discover . -type f -name "*.Java" blog/submit/linux-unix/the use of-locate-xargs-tar-create-huge-archive-cygwin-linux-unix
for greater information)
discover, tar, and xargs
--------------------
discover . -call -type f '*.Mp3' -mtime -180 xargs -zero tar rvf tune.Tar
(-print0 helps take care of areas in filenames)
(see http://alvinalexander.Com/mac-os-x/mac-backup-filename-directories-spaces-find-tar-xargs)
locate and pax (in preference to xargs and tar)
---------------------------------------
discover . -type f -call "*html" discover . -kind f -name "*html" blog/put up/linux-unix/using-pax-as a substitute-of-tar)
On a related be aware, don’t overlook the locate command. It maintains a database to your Unix/Linux device to help find files very rapid:
locate command
--------------
discover tomcat.Sh # seek the whole filesystem for 'tomcat.Sh' (uses the discover database)
discover -i spring.Jar # case-insensitive seek
If you already know of any greater top find commands to proportion, please depart a observe inside the Comments phase below.
The ultimate sections on this web page describe greater completely the commands just proven.
Basic discover command examples
This first Linux locate example searches via the foundation filesystem ("/") for the report named Chapter1. If it unearths the document, it prints the area to the display.
Locate / -call Chapter1 -type f -print
On Linux structures and modern Unix device you now not want the -print alternative at the end of the find command, so you can difficulty it like this:
locate / -call Chapter1 -kind f
The -kind f choice right here tells the find command to return only documents. If you don’t use it, the locate command will returns files, directories, and other things like named pipes and tool documents that in shape the call sample you specify. If you do not care about that, just depart the -kind f option off your command.
This next find command searches via most effective the /usr and /domestic directories for any file named Chapter1.Txt:
find /usr /domestic -name Chapter1.Txt -kind f
To search within the cutting-edge listing — and all subdirectories — simply use the . Individual to reference the present day listing in your discover instructions, like this:
locate . -name Chapter1 -kind f
This subsequent example searches through the /usr directory for all documents that start with the letters Chapter, accompanied by means of whatever else. The filename can end with some other aggregate of characters. It will in shape filenames along with Chapter, Chapter1, Chapter1.Awful, Chapter-in-lifestyles, and so on.:
discover /usr -name "Chapter*" -type f
This subsequent command searches via the /usr/neighborhood directory for files that end with the extension .Html. These file locations are then published to the display:
discover /usr/neighborhood -name "*.Html" -type f
Find directories with the Unix find command
Every choice you simply noticed for finding files also can be used on directories. Just update the -f choice with a -d alternative. For example, to locate all directories named construct underneath the modern-day directory, use this command:
find . -kind d -call construct
Find documents that don't in shape a pattern
To discover all files that don't suit a filename sample, use the -not argument of the locate command, like this:
locate . -kind f -not -name "*.Html"
That generates a list of all documents under the present day directory whose filename DOES NOT result in .Html, so it suits documents like *.Txt, *.Jpg, and so forth.
Finding files that include text (discover + grep)
You can integrate the Linux discover and grep instructions to powerfully search for text strings in lots of files.
This next command shows the way to find all documents below the contemporary listing that end with the extension .Java, and comprise the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a healthy is discovered, in place of printing all of the matches themselves:
locate . -type f -name "*.Java" -exec grep -l StringBuffer ;
(Those previous couple of characters are required any time you want to exec a command at the documents which are located. I discover it helpful to consider them as a placeholder for every file that is discovered.)
This subsequent instance is similar, however here I use the -i argument to the grep command, telling it to disregard the case of the characters string, so it's going to find documents that include string, String, STRING, and so forth.:
discover . -type f -call "*.Java" -exec grep -il string ;
Acting on files you find (find + exec)
This command searches through the /usr/local listing for files that end with the extension .Html. When those files are found, their permission is changed to mode 644 (rw-r--r--).
Find /usr/local -call "*.Html" -kind f -exec chmod 644 ;
This discover command searches thru the htdocs and cgi-bin directories for files that give up with the extension .Cgi. When these files are determined, their permission is modified to mode 755 (rwxr-xr-x). This instance suggests that the find command can effortlessly seek through multiple sub-directories (htdocs, cgi-bin) at one time:
find htdocs cgi-bin -name "*.Cgi" -kind f -exec chmod 755 ;
Running the ls command on files you locate
From time to time I run the locate command with the ls command so I can get special records approximately documents the discover command locates. To get began, this find command will locate all of the *.Pl documents (Perl documents) below the cutting-edge listing:
locate . -name "*.Pl"
In my cutting-edge directory, the output of this command looks as if this:
./information/newsbot/vintage/3filter.Pl
./news/newsbot/tokenParser.Pl
./news/robonews/makeListOfNewsURLs.Pl
That's best, however what if I need to see the remaining amendment time of these files, or their filesize? No problem, I just add the ls -ld command to my locate command, like this:
discover . -name "*.Pl" -exec ls -ld ;
This results in this very specific output:
-rwxrwxr-x 1 root root 2907 Jun 15 2002 ./news/newsbot/antique/3filter.Pl
-rwxrwxr-x 1 root root 336 Jun 17 2002 ./news/newsbot/tokenParser.Pl
-rwxr-xr-x 1 root root 2371 Jun 17 2002 ./news/robonews/makeListOfNewsURLs.Pl
The "-l" flag of the ls command tells ls to give me a "long list" of every report, while the -d flag is extraordinarily beneficial in this situation; it tells ls to present me the equal output for a listing. Normally if you use the ls command on a directory, ls will listing the contents of the directory, however if you use the -d alternative, you will get one line of facts, as proven above.
Find and delete
Be very careful with these subsequent commands. If you type them in wrong, or make the incorrect assumptions approximately what you're searching for, you may delete quite a few files very speedy. Make sure you have got backups and all that, you've got been warned.
Here's how to discover all files under the present day directory that start with the letters 'Foo' and delete them.
Discover . -kind f -call "Foo*" -exec rm ;
This one is even greater risky. It finds all directories named CVS, and deletes them and their contents. Just like the previous command, be very cautious with this command, it's far risky(!), and no longer advocated for rookies, or if you do not have a backup.
Discover . -kind d -call CVS -exec rm -r ;
Find documents with exclusive report extensions
The syntax to find a couple of filename extensions with one command looks as if this:
locate . -kind f ( -name "*.C" -o -name "*.Sh" )
Just preserve adding more "-o" (or) alternatives for every filename extension. Here's a link to
Case-insensitive file looking
To carry out a case-insensitive search with the Unix/Linux find command, use the -iname option rather than -name. For instance, in case you want to look for all documents and directories named foo, FOO, or some other combination of uppercase and lowercase characters below the cutting-edge directory, use this command:
find . -iname foo
If you’re just interested by directories, search like this:
discover . -iname foo -kind d
And in case you’re simply searching out files, search like this:
locate . -iname foo -kind f
Find files through amendment time
To discover all documents and directories that have been modified inside the last seven days, use this discover command:
discover . -mtime -7
To limit the output to just documents, add the -kind f choice as proven earlier:
locate . -mtime -7 -kind f
and to show simply directories:
discover . -mtime -7 -type d
0 Comments
Post a Comment