LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-04-2013, 07:17 AM   #1
gbao256
LQ Newbie
 
Registered: Dec 2012
Posts: 10

Rep: Reputation: Disabled
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

Last edited by gbao256; 03-04-2013 at 10:21 AM.
 
Old 03-04-2013, 07:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
1 members found this post helpful.
Old 03-04-2013, 07:45 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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

Last edited by shivaa; 03-04-2013 at 07:47 AM.
 
Old 03-04-2013, 07:52 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by shivaa View Post
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
 
1 members found this post helpful.
Old 03-04-2013, 08:45 AM   #5
gbao256
LQ Newbie
 
Registered: Dec 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
tks for all your help it is very useful
 
Old 03-04-2013, 10:20 AM   #6
gbao256
LQ Newbie
 
Registered: Dec 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-04-2013, 10:34 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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

Last edited by shivaa; 03-04-2013 at 11:15 AM. Reason: Typo
 
1 members found this post helpful.
Old 03-04-2013, 10:39 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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.
 
2 members found this post helpful.
Old 03-04-2013, 10:53 AM   #9
gbao256
LQ Newbie
 
Registered: Dec 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
@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 . I'm such a noob
ah, I remember something I was read is it output code ?
IS 1 is standard input 2 is standard output ?

Last edited by gbao256; 03-04-2013 at 10:56 AM.
 
Old 03-04-2013, 11:02 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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

Last edited by druuna; 03-04-2013 at 11:03 AM. Reason: added link
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Root crontab running script, log re-directs fail. Explanation needed. mattst Linux - General 5 10-27-2011 09:25 AM
need annotated explanation of the script !! ruchika_sachdeva Linux - Newbie 4 05-19-2010 07:23 AM
/etc/init.d script tutorial mechdave Programming 1 09-22-2007 11:44 AM
Shell Script Tutorial: shahrokhnikou Programming 1 10-28-2005 07:59 AM
Explanation or Tutorial on Boot Records? kkathman Linux - General 2 10-16-2003 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration