Calculate size of files in shell
Try:
find . -name "*.undo" -ls | awk '{total += $7} END {print total}'
On my system the size of the file is the seventh field in the find -ls
output. If your find … -ls
output is different, adjust.
In this version, using the existing directory information (file size) and the built-in ls feature of find should be efficient, avoiding process creations or file i/o.
bash script: calculate sum size of files
Ultimately, as other answers will point out, it's not a good idea to parse the output of ls
because it may vary between systems. But it's worth knowing why the script doesn't work.
The ambiguous redirect error is because you need quotes around your ls
command i.e.:
while IFS='' read -r line || [[ -n "$line" ]]; do
echo $line
done < "`ls -l | grep opencv | awk '{print $5}'`"
But this still doesn't do what you want. The "<" operator is expecting a filename, which is being defined here as the output of the ls
command. But you don't want to read a file, you want to read the output of ls
. For that you can use the "<<<" operator, also known as a "here string" i.e.:
while IFS='' read -r line || [[ -n "$line" ]]; do
echo $line
done <<< "`ls -l | grep opencv | awk '{print $5}'`"
This works as expected, but has some drawbacks. When using a "here string" the command must first execute in full, then store the output of said command in a temporary variable. This can be a problem if the command takes long to execute or has a large output.
IMHO the best and most standard method of iterating a commands output line by line is the following:
ls -l | grep opencv | awk '{print $5} '| while read -r line ; do
echo "line: $line"
done
linux command to get size of files and directories present in a particular folder?
Use ls
command for files and du
command for directories.
Checking File Sizes
ls -l filename #Displays Size of the specified file
ls -l * #Displays Size of All the files in the current directory
ls -al * #Displays Size of All the files including hidden files in the current directory
ls -al dir/ #Displays Size of All the files including hidden files in the 'dir' directory
ls
command will not list the actual size of directories(why?). Therefore, we use du
for this purpose.
Checking Directory sizes
du -sh directory_name #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh * #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format
Including -h
option in any of the above commands (for Ex: ls -lh *
or du -sh
) will give you size in human readable format (kb
, mb
,gb
, ...)
For more information see man ls
and man du
Get total size of a list of files in UNIX
You should simply be able to pass $file_list
to du
:
du -ch $file_list | tail -1 | cut -f 1
du
options:
-c
display a total-h
human readable (i.e. 17M)
du
will print an entry for each file, followed by the total (with -c
), so we use tail -1
to trim to only the last line and cut -f 1
to trim that line to only the first column.
total size of group of files selected with 'find'
The command du
tells you about disk usage. Example usage for your specific case:
find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -print0 | du --files0-from=- -hc | tail -n1
(Previously I wrote du -hs
, but on my machine that appears to disregard find
's input and instead summarises the size of the cwd.)
Short command to find total size of files matching a wild card
Try du
to summarize disk usage:
du -csh *.jpg
Output (for example):
8.0K sane-logo.jpg
16K sane-umax-advanced.jpg
28K sane-umax-histogram.jpg
24K sane-umax.jpg
16K sane-umax-standard.jpg
4.0K sane-umax-text2.jpg
4.0K sane-umax-text4.jpg
4.0K sane-umax-text.jpg
104K total
du
does not summarize the size of the files but summarizes the size of the used blocks in the file system. If a file has a size of 13K and the file system uses a block size of 4K, then 16K is shown for this file.
How to find the size of a file and assign it to a variable in Unix
You can use the stat
command which eliminates the need to use AWK.
For example, in Linux with Bash where myfile is your file path:
sz=$(stat -c '%s' myfile)
if [ $sz -eq 100 ]; then
echo "myfile is 100 bytes"
fi
Take note of the equality command -eq
that's the arithmetic binary operator in Bash.
Alternatively, you can use a variable for the file path:
f=my/file/path
sz=$(stat -c '%s' $f)
if [ $sz -eq 100 ]; then
echo "$f is 100 bytes"
fi
Check folder size in Bash
You can do:
du -hs your_directory
which will give you a brief output of the size of your target directory. Using a wildcard like *
can select multiple directories.
If you want a full listing of sizes for all files and sub-directories inside your target, you can do:
du -h your_directory
Tips:
Add the argument
-c
to see a Total line at the end. Example:du -hcs
ordu -hc
.Remove the argument
-h
to see the sizes in exact KiB instead of human-readable MiB or GiB formats. Example:du -s
ordu -cs
.
Related Topics
Bash - Find Files Older Than X Minutes and Move Them
Implementing an Update/Upgrade System for Embedded Linux Devices
How to Pipe the Results of 'Find' to Mv in Linux
What Exactly Is Sudo Bang Bang
How to View Log Files in Linux and Apply Custom Filters While Viewing
How to Determine If Lcd Monitor Is Turned on from Linux Command Line
How to List Specific Type of Files in Recursive Directories in Shell
Signing into Slack-Desktop Not Working on 4.23.0 64-Bit (Ubuntu)
When a Parent Process Is Killed by "Kill -9", Will Subprocess Also Be Killed
Add Double Quotes Around Fields in Awk Script Output
The Gnu Screen Is Unresponsive, Seems Blocked
How to Use Nohup to Run Process as a Background Process in Linux
How to Pass Argument in Expect Through the Command Line in a Shell Script
How to Count Number of Unique Values of a Field in a Tab-Delimited Text File
Setting Node_Env for Node.Js + Expressjs Application as a Daemon Under Ubuntu