LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   I need a tutorial/explanation for this script (https://www.linuxquestions.org/questions/linux-newbie-8/i-need-a-tutorial-explanation-for-this-script-4175452634/)

gbao256 03-04-2013 07:17 AM

I need a tutorial/explanation for this script
 
Hi, I'm learning some bash shell scripting and I found a code on the internet:

PHP Code:

find . -exec du -cbsh '{}' 

the "'{}' +" is very weird to me. I dont think i can google it because it is special character. Please help me:(

tks for all the reply below. I was understand it but I have some problem now:

Quote:

I dont want to create many topic because I'm a noob :(
I just want to get the total line so I wrote this code
Code:

find / -user u1 -type f -exec du -cbh '{}' + | tail -n 1
but it will search some thing in /proc folder and have some problem like that

Quote:

find: `/proc/2046/task/2046/fd/5': No such file or directory
find: `/proc/2046/task/2046/fdinfo/5': No such file or directory
find: `/proc/2046/fd/5': No such file or directory
find: `/proc/2046/fdinfo/5': No such file or directory
3132 total
how I can deny this. I just want to get the total line

colucix 03-04-2013 07:30 AM

It is part of the -exec syntax. It runs a single instance of the du command on the result of the search (multiple files). More details here: http://www.gnu.org/software/findutil...Multiple-Files

shivaa 03-04-2013 07:45 AM

Once check this out here.

For understanding, you can devide this cmd in two parts, as:
Code:

find . -exec du -cbsh '{}' +
The part marked in red is searching for files inside current directory and keeps it in {}, which works like a container. Then in second part (marked in green) it executes some command on what first part has searched.

So it actually executes du -cbsh on files resulted from find . part.
Code:

du -cbsh file1
du -cbsh file2
du -cbsh file3 ... and so on


druuna 03-04-2013 07:52 AM

Quote:

Originally Posted by shivaa (Post 4904325)
So it actually executes du -cbsh on files resulted from find . part.
Code:

du -cbsh file1
du -cbsh file2
du -cbsh file3 ... and so on


That is not correct!

This
Code:

find . -exec du -cbsh '{}' \;
results in
Code:

du -cbsh file1
du -cbsh file2
du -cbsh file3

And this
Code:

find . -exec du -cbsh '{}' +
results in
Code:

du -cbsh file1 file2 file3

gbao256 03-04-2013 08:45 AM

tks for all your help it is very useful

gbao256 03-04-2013 10:20 AM

Sorry to tag it unsolved, but I have some problem with this code, I dont want to create many topic because I'm a noob :(
I just want to get the total line so I wrote this code
Code:

find / -user u1 -type f -exec du -cbh '{}' + | tail -n 1
but it will search some thing in /proc folder and have some problem like that

Quote:

find: `/proc/2046/task/2046/fd/5': No such file or directory
find: `/proc/2046/task/2046/fdinfo/5': No such file or directory
find: `/proc/2046/fd/5': No such file or directory
find: `/proc/2046/fdinfo/5': No such file or directory
3132 total
how I can deny this. I just want to get the total line

shivaa 03-04-2013 10:34 AM

These are pid file inside /proc corresponding to various process, which may have completed.

Also run this find command with root privileges, not a normal user, because you're seaching in root's home directory.

Code:

~$ su - root
~$ find / -user u1 -type f -exec du -cbh '{}' + | tail -n 1

OR

~$ sudo find / -user u1 -type f -exec du -cbh '{}' + | tail -n 1


druuna 03-04-2013 10:39 AM

@gbao256: Those message are generated by the find command.

Your find command also searches the /proc directory, which can be highly dynamic. The mentioned No such file or directory are files that have gone already.

To suppress them use this:
Code:

find / -user u1 -type f -exec du -cbh '{}' + 2>/dev/null | tail -n 1
You can also run into permission problems, see shivaa's post for a solution for that.

gbao256 03-04-2013 10:53 AM

Quote:

Originally Posted by druuna (Post 4904443)
@gbao256: Those message are generated by the find command.

Your find command also searches the /proc directory, which can be highly dynamic. The mentioned No such file or directory are files that have gone already.

To suppress them use this:
Code:

find / -user u1 -type f -exec du -cbh '{}' + 2>/dev/null | tail -n 1
You can also run into permission problems, see shivaa's post for a solution for that.

tks for u and shivaa reply.
Your code work. I was think about /dev/null but my code dont work
Code:

find / -user u1 -type f -exec du -cbh '{}' + >/dev/null | tail -n 1
can u tell me what is 2 is :study:. I'm such a noob :doh:
ah, I remember something I was read is it output code ?
IS 1 is standard input 2 is standard output ?

druuna 03-04-2013 11:02 AM

1 (or "nothing") represents stdout en 2 stderr.

stdout outputs normal informations and stderr outputs errors.

Have a look here for the whole story: Standard streams

Bash related link: 3. All about redirection


All times are GMT -5. The time now is 02:29 PM.