xargs and exec

First, think about a face questions triggered

Today's time to students, staff and they chatted and see the face questions on the table, there is a very interesting question, a total of three questions to interview three people, two people wrong, one did not answer up, I was speechless, on this level, but also out of the interview, listen to the tone of the personnel also said that very large, do not know where it came from self-confidence? I do not know they are learning yesterday?

Face questions like this:

Find the file name under /test.dir test directory contains keywords and delete them all (Note: not less than two methods)

Two interviewers are so written:

find ./ -name "*test*" -type f | rm –rf

So write seems to be yes, but certainly not the actual operation, or too tender!

Why do you not do the operation? Find out what is the output? We find by file name, find the fact the character stream file name consisting of, you put a character stream to the rm command, rm will naturally be executed as normal piped, but the character stream and file path does not matter, so delete only to see the results after the find, so it can not be deleted, then how to do the job can delete it? Just FIND find the character stream into a path on the line, the more this method, we can command converts a stream of characters comes through exec or find help to xargs real path on the line, let's try:

[root@localhost ~]# mkdir test.dir ; cd test.dir
[root@localhost test.dir]# touch test_{01..06}.txt 
[root@localhost test.dir]# ls test_01.txt  test_02.txt  test_03.txt  test_04.txt  test_05.txt  test_06.txt 
[root@localhost test.dir]# find ./ -name "*test*" -type f | xargs rm -rf 
[Root @ localhost test.dir] # ls # Empty

Another approach to try again, find this method is carrying, and the effect of the above example are the same.

[root@localhost test.dir]# ls test_01.txt  test_02.txt  test_03.txt  test_04.txt  test_05.txt  test_06.txt
[root@localhost test.dir]# find . -type f -name "*test*" -exec rm -f {} \;
[root@localhost test.dir]# ls

Again a more simple and crude way:

[root@localhost test.dir]# rm -rf `find ./ -name "*test*" -type f`
[Root @ localhost test.dir] # ls # Empty

Let us give an example to taste:

[root@localhost test.dir]# ls test_01.txt  test_02.txt  test_03.txt  test_04.txt  test_05.txt  test_06.txt
[root@localhost test.dir]# echo testtesttest > test_01.txt 
[Root @ localhost test.dir] # echo testtesttest> test_02.txt # In addition to these two files are empty
[Root @ localhost test.dir] # find -name "* .txt" | rm -rf # We already know that this is certainly not delete these files.
[root@localhost test.dir]# ls test_01.txt  test_02.txt  test_03.txt  test_04.txt  test_05.txt  test_06.txt    #果不其然
[Root @ localhost test.dir] # find -name "* .txt" | grep test # This operation is normal, the file containing the test file names which are filtered out 
./test_01.txt 
./test_02.txt
./test_03.txt
./test_04.txt
./test_05.txt
./test_06.txt
[root@localhost test.dir]# find -name "*.txt" | xargs grep test
./test_01.txt:testtesttest
./test_02.txt:testtesttest
# It is important here, plus a xargs, grep will not find in a name, but to find a file inside to find! ! !

Carefully understand it, you appreciate it? Well, I'll summarize it!

If not, then xargs, grep will only find results as a character stream, it will not be as a file, when with xargs, these character stream becomes a real path, in fact, will find that comes into xargs the exec are possible, as follows:

[Root @ localhost test.dir] # find -name "* .txt" -exec grep test {} \; # {} refers to the front of the find result, the standard output it! testtesttest testtesttest

Second, again give an example of two similar

Topic 1:

Find the file name under /test.dir test directory contains the keyword file and all moved to the / tmp directory (Note: not less than two methods)

How does it work?

The first way:

[root@localhost test.dir]# ls test_01.txt  test_02.txt  test_03.txt  test_04.txt  test_05.txt  test_06.txt
[Root @ localhost test.dir] # mv `find -name" * test * "-type f` / tmp # will be placed in the` `command them, will be able to automatically stream into meaningful path

The second way:

[root@localhost test.dir]# ls test_01.txt  test_02.txt  test_03.txt  test_04.txt  test_05.txt  test_06.txt
[Root @ localhost test.dir] # find -name "* test *" -type f -exec mv <---- I wrote here can not be written!

The syntax #mv is this: mv <file being moved> <target folder>, but will find here the final result is the complement to the <target folder> position, how do?

#exec is not used, we can, as shown below:

[root@localhost test.dir]# find -name "*test*" -type f | xargs mv -t /tmp

#xargs we have understood, that -t What does it mean? mv -t means that the <moved file> and <target folder> change places, so just find the result to completion <file was moved>, which is not perfect!

The third way:

The third way and the second way is almost, but we do not have mv -t option, while xargs comes mechanism used, as follows:

[root@localhost test.dir]# find -name "*test*" -type f | xargs -i mv {} /tmp;

# This time we did not change the syntax of the mv, except that in place of the brackets <file was moved>, do not forget to add a front xargs -i option, the only way in brackets makes sense!

Topic 2:

Find the file in the directory contains /test.dir test keywords and copy all files to the / tmp directory (Note: not less than two methods)

How does it work?

[root@localhost test.dir]# find ./ -name "*test*" -type f | xargs -i cp {} /tmp;
[root@localhost test.dir]# find ./ -name "*test*" -type f | xargs cp -t /tmp;
[root@localhost test.dir]# cp `find ./ -name "*test*" -type f` /tmp

I am typing tired, do not explain, and after the first topic to understand, this is not a topic to understand natural problems! ! !

Now, I really want to loudly say to the three interviewers:

Haha!

Documents: exec and xargs.note
link: http: //note.youdao.com/noteshare id 02401541ecae82d3fb7f3093fca2d6c2 & sub = C82F67ED974C49C0AED08685729E975F # = Ethics cloud notes version?

Guess you like

Origin www.cnblogs.com/yizhangheka/p/11701213.html