use shell script to find file size - linux

I am doing a homework which ask me to find the smallest file and biggest file under the directory, I have done that. But my output is something like
"the smallest file is xxx (xxxx -'filename' bytes)
I wish I could print something without the filename part.
I am using du -b $filename to get the size.

du -b | sort -rh | head -n 1 | awk '{print "The smallest file is " $1 " bytes"}'

Related

Write a command to display text file name and its size in different lines in linux

I want to display text file name and its size in different lines
I have tried
du *.* | cut -f 1
This give me only size of the files in given directory
du *.* | cut -f 2
This gives the filenames
But i could't figure out how to format it in way where the size comes first then the file name.
example :
4
file1.txt
5
file2.txt
I just figured it out this is working as expected.
du *.txt* | tr [:space:] '\n'
You can do some awk scripting:
for file in *
do
echo "$file $(du "$file" | awk '{print $1}')"
done

Get size of image in bash

I want to get size of image. The image is in folder by name encodedImage.jpc
a="$(ls -s encodedImage.jpc | cut -d " " -f 1)"
temp="$(( $a*1024 * 8))"
echo "$a"
The output is not correct. How to get size? Thank You
Better than parsing ls output, the proper way is to use the command stat like this :
stat -c '%s' file
Check
man stat | less +/'total size, in bytes'
If by size you mean bytes or pretty bytes can you just use
ls -lh
-h When used with the -l option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less using base 2 for sizes.
I guess the more complete answer if you're just trying to tear off the file size alone (I added the file name as well you can remove ,$9 to drop it)
ls -lh | awk '{print $5,$9}'
U can use this command
du -sh your_file

Bash shell script for finding file size

Consider:
var=`ls -l | grep TestFile.txt | awk '{print $5}'`
I am able to read file size, but how does it work?
Don't parse ls
size=$( stat -c '%s' TestFile.txt )
Yes, so basically you could divide it into 4 parts:
ls -l
List the current directory content (-l for long listing format)
| grep TestFile.txt
Pipe the result and look for the file you are interested in
| awk '{print $5}
Pipe the result to awk program which cuts (by using spaces as separator) the fifth column which happens to be the file size in this case (but this can be broken by spaces in the filename, for example)
var=`...`
The backquotes (`) enclose commands. The output of the commands gets stored in the var variable.
NOTE: You can get the file size directly by using du -b TestFile.txt or stat -c %s TestFile.txt

Removing the file part of the output from du in a bash script

I'm trying to remove the output when calling du in my bash script. I'm just trying to print out the size of the current directory. So it looks like this:
DIRSIZE=$(du -hs $1)
printf "The size of the directory given is: %s\n" "$DIRSIZE"
I want the output to look like this:
The size of the directory given is: 32K
However, my command currently outputs:
The size of the directory given is: 32K /home/dir_listed/
Is there an easy way to remove the directory?
With awk:
DIRSIZE=$(du -hs $1 | awk '{print $1}')
Take only the first field from du output and save to DIRSIZE.
With sed:
DIRSIZE=$(du -hs $1 | sed 's/[[:space:]].*//')
Remove from first space to end of line and save to DIRSIZE.
With cut:
DIRSIZE=$(du -hs $1 | cut -f 1)
Take only the first field from du output which is tab seperated and save to DIRSIZE.
Try this:
DIRSIZE=$(du -hs $1 | awk '{print $1}')
printf "The size of the directory given is: %s\n" "$DIRSIZE"

Linux: Find total filesize of files path inside a file

I have a file containing some file paths like:
./file1
./dir/file2
./dir3/dir4/fil3
etc
How can i find the total filesize of all of them? I know about "du" for getting the filesize of single file but no idea how to use a file.
Thank you
you can use du to give total size of multiple files
cat file | tr "\n" "\0" | du -ch --files0-from=- | tail -n1
Use awk for getting file size
cat file | awk '{system("ls -l " $0)}' | awk '{ TOTAL += $5} END { print TOTAL}'
GNU coreutils du only suggestion
EDIT: the named option --files0-from is a GNU extension, so this suggested solution won't work with any non GNU coreutils du version. As you don't semm to have it the awk version posted by Vivek Goel is the one you should try instead.
You already answered your own question. Using du is the key. The "missing" option you're looking for might be this one found in the manual pages. (man du)
--files0-from=F
summarize disk usage of the NUL-terminated file names specified in file F; If F is - then read names from standard input
Usage would be like this:
tr "\n" "\0" <file-list | du --files0-from=-

Resources