我是靠谱客的博主 典雅睫毛,最近开发中收集的这篇文章主要介绍How to Finding Files : Find ,locate ,slocate,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.Finding All Your MP3 Files

Problem:

You have MP3 audio files scattered all over your filesystem.You'd like to move them all into a single location so that you can organize them and then copy them onto a music player.

Solution:

The find utility can locate all of those files and then execute a command to move them where you want .

[root@DBAMAXWELL cp8]# find . -name '*.mp3' -print -exec mv '{}' ~/songs ;

2.How to handling Filenames containing odd characters

Problem

You used a find command like  "Finding All Your MP3 Files" but the results were not what you intended because many of your filenames contain odd characters.

Solution

$ find some_directory -type f -print0 | xargs -0 chmod 0644

3. How to Speeding up operations on Found Files

Problem

You want to speed finding all files up.

Solution

$ find some_directory -type f -print0 | xargs -0 chmod 0644

4.Finding Files Across Symbolic Links

Problem

You issued a find command to find your .mp3 files but it didn't find all of them.

Solution

Use the -follow predicate.

[maxwell@DBAMAXWELL learning]$ find . -follow -name '*.mp3' -print0 | xargs -i -0 mv '{}' ~/learning
[maxwell@DBAMAXWELL learning]$ ls -ltr
total 4
-rw-r--r--  1 maxwell root  15 Mar  6 16:13 over.here
drwxrwxr-x 11 maxwell root 153 Mar 19 06:35 learning_bash_shell
-rwxrwxr-x  1 maxwell root   0 Mar 19 06:36 test1.mp3
[maxwell@DBAMAXWELL learning]$ 

5.Finding Files Irrespective of Case

Problem

Some of your MP3 files end with .MP3 rather than .mp3.How do you find those?

Solution

Use the -iname predicate to run a case-insensitive search ,rather than just -name

maxwell@DBAMAXWELL cp9]$ ls -tlr
total 0
-rwxrwxr-x 1 maxwell root 0 Mar 19 06:36 test.mp3
-rwxrwxr-x 1 maxwell root 0 Mar 19 06:52 test.MP3
[maxwell@DBAMAXWELL cp9]$ find . -follow -iname '*.mp3' -print0 | xargs -i -0 mv '{}' ~/learning
[maxwell@DBAMAXWELL cp9]$ ls -ltr
total 0
[maxwell@DBAMAXWELL cp9]$ cd ..
[maxwell@DBAMAXWELL learning_bash_shell]$ cd ..
[maxwell@DBAMAXWELL learning]$ ls -ltr
total 4
-rw-r--r--  1 maxwell root  15 Mar  6 16:13 over.here
drwxrwxr-x 11 maxwell root 153 Mar 19 06:35 learning_bash_shell
-rwxrwxr-x  1 maxwell root   0 Mar 19 06:36 test.mp3
-rwxrwxr-x  1 maxwell root   0 Mar 19 06:36 test1.mp3
-rwxrwxr-x  1 maxwell root   0 Mar 19 06:52 test.MP3
[maxwell@DBAMAXWELL learning]$ 

One of the most common places where you'll see the upper- and lowercase issue

[maxwell@DBAMAXWELL cp9]$ find . -name '*.jpg' -print
./test.jpg
[maxwell@DBAMAXWELL cp9]$ find . -name '*.[Jj][Pp][Gg]' -print
./test.jpg
./test.JpG
./test.jPg
./test.JPG
./test.jpG
./test.JPg
[maxwell@DBAMAXWELL cp9]$ 

6.Finding Files by Date

Problem

Suppose someone sent you a JPEG image file that you saved on your filesystem a few month ago.Now you don't remeber where you put it,How can you find it ?

Solution

Use a find command with the -mtime predicate, which checks the date of last modification.

[maxwell@DBAMAXWELL cp9]$ find . -iname '*.[Jj][Pp][Gg]' -mtime +90 -print
[maxwell@DBAMAXWELL cp9]$ find . -name '*.jpg' -mtime +90 -print  

The -mtime predicate takes an argument to specify the timeframe for the search.

The find utility also has logical AND ,OR ,and NOT constructs so if you know that the file was at least one week old (7 days) but not more than 14 days old , you can combine the predicate like this :

[maxwell@DBAMAXWELL cp9]$ find . -mtime +7 -a -mtime -14 -print

You can get even more complicated using OR as well as AND and even NOT to combine condition , as in :

[maxwell@DBAMAXWELL cp9]$ find . -mtime +14 -name '*.text' -o ( -mtime -14 -name '*.txt' ) -print

7.Finding Files by Type

Problem:

You are looking for a directory with the word "java" in it, When you tried

[maxwell@DBAMAXWELL cp9]$ find . -name '*java' -print

you got way too many files - including all the java source files in your part of the filesystem.

Solution:

Use the -type predicate to select only directories

[maxwell@DBAMAXWELL cp9]$ find . -type d -name '*java*' -print

8.Finding Files by Size

 Problem

How do you find your largest files?

Solution

Use the -size predicate in the find command to select files above ,below ,or exactly a certain size.

[maxwell@DBAMAXWELL cp9]$ find . -size +3000k -print

9. How to finding files by content?

Problem 

How do you find a file of some known content?

Solution

if you are in the vicinity of that file,say within the current directory , you can start with a simple grep.

[maxwell@DBAMAXWELL cp9]$ ls -ltr
total 4
-rwxrwxr-x 1 maxwell root    0 Mar 19 06:59 test.jpg
-rwxrwxr-x 1 maxwell root    0 Mar 19 07:02 test.JpG
-rwxrwxr-x 1 maxwell root    0 Mar 19 07:02 test.jPg
-rwxrwxr-x 1 maxwell root    0 Mar 19 07:02 test.JPG
-rwxrwxr-x 1 maxwell root    0 Mar 19 07:03 test.jpG
-rwxrwxr-x 1 maxwell root    0 Mar 19 07:03 test.JPg
-rwxrwxr-x 1 maxwell root 1879 Mar 19 08:31 mother.txt
[maxwell@DBAMAXWELL cp9]$ grep -i portend *.txt
had used the word “portend.”
grep -i portend *.txt
grep -i portend */*.txt
find . -name '*.txt' -exec grep -Hi portend '{}' ;
[maxwell@DBAMAXWELL cp9]$ 

With the -i option ,grep will ignore upper- and lowercase difference.

If you think the file might be in one of your many subdirectories, you can try to reach all the files that are in subdirectories of the current directory with this command.

[maxwell@DBAMAXWELL cp9]$ grep -i portend */*.txt
grep: */*.txt: No such file or directory

let's use a more complete solution: the find command.Use the -exec option on find so that if the predicates are true up to that point, it will  execute a command for each file it finds.

[maxwell@DBAMAXWELL cp9]$ find . -name '*.txt' -exec grep -Hi portend '{}' ;
./mother.txt:had used the word “portend.”
./mother.txt:grep -i portend *.txt
./mother.txt:grep -i portend */*.txt
./mother.txt:find . -name '*.txt' -exec grep -Hi portend '{}' ;
[maxwell@DBAMAXWELL cp9]$ 

10. How to finding existing files and content fast?

Problem

You'd like to be able to find files without having to wait for a long find command to complete. or you  need to find a file with some specific content.

Solution

$ locate apropos

/usr/bin/apropos

/usr/share/man/de/man1/apropos.1.gz

/usr/share/man/es/man1/apropos.1.gz

/usr/share/man/it/man1/apropos.1.gz

/usr/share/man/ja/man1/apropos.1.gz

/usr/share/man/man1/apropos.1.gz

11.How to finding a file using a list of possible locations

Problem

You need to execute ,source, or read a file,but it may be located in a number of different places in or outside of the $PATH.

Solution

$ source myfile

最后

以上就是典雅睫毛为你收集整理的How to Finding Files : Find ,locate ,slocate的全部内容,希望文章能够帮你解决How to Finding Files : Find ,locate ,slocate所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部