LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 02-27-2012, 10:56 PM   #1
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Rep: Reputation: Disabled
use of ls command


using ls command i want to find the total size of all regular files
Quote:
ls -l | grep '^-' | cut -d " " -f6
when i tried with above code i got size of only a few files

how can i get the size of all files and their total
 
Old 02-27-2012, 11:39 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
ls output should not be parsed for reasons explained here.

Safer to use something like this which also copes with file names having unusual characters in their names such as backspace and linend. Not tested.
Code:
size=0
while IFS= read -r -d '' file
do
   (( size += $( stat --format=%s "$file" ) ))
done < <(find . -maxdepth 0 -type f -print0)
echo "Total size is $size bytes"
 
Old 02-28-2012, 01:43 AM   #3
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
i didn't understand the meaning of
Quote:
IFS= read -r -d '' file
and also
Quote:
$( stat --format=%s "$file" )
done < <(find . -maxdepth 0 -type f -print0)
can u tell me if u don't mind
 
Old 02-28-2012, 11:16 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Try reading the stat man page and running stat --format=%s on some file to see what that does.

Read this to see what $( ... ) does.

Read the find man page's entry on -print0 to see how that makes find's output be a list of filenames separated by the ASCII NUL character and read this for more on what the NUL character is/does.

In IFS= read -r -d '' file, the IFS= sets IFS to an empty string during execution of the following read. This seldom-used bash feature is briefly described here where it says "Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment". The effect of this on read is described later in this post.

Use help read at a command prompt (or the bash man page) for information on the read options.

The -r option prevents any "escape sequences" in the file name being processed -- so the characters in the find output are read exactly as find wrote them.

The -d option sets the delimiter that read uses to break the input. By default, it is line end so read processes each line until there are no lines left. Setting the delimiter to the empty string actually sets it to the NUL character (internally values in bash variables are terminated with a NUL character -- this is standard for strings in the C programming language), so read will return each time it gets a file name from find (remember the -print0 is making find separate file names with a NUL?).

read puts each file name into the variable "file". Normally it strips off any characters in IFS from the beginning and end of its input pieces. IFS is normally set to space, tab and line end so you don't want that in case there are file names that have any of those characters at beginning or end of their names. Setting IFS to the empty string means you get the file name exactly as find outputted it.

Last edited by catkin; 02-28-2012 at 11:22 AM. Reason: prettification and a bit more explanation
 
Old 02-29-2012, 09:32 PM   #5
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 90
Wow. I'm gonna be studying this thread for a LONG time. Thanks to the OP and catkin.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing shell command from JSP file with command line arg from URL orcusomega Programming 2 01-13-2012 04:38 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 12:21 PM
how to copy drive using dd and tee command parallely? source code of dd command mdfakkeer Linux - Software 1 02-10-2010 02:31 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 03:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 07:30 PM


All times are GMT -5. The time now is 01:22 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration