LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   RE: Newbies - Checking File Size in Command Line (https://www.linuxquestions.org/questions/linux-newbie-8/re-newbies-checking-file-size-in-command-line-881264/)

tix 05-17-2011 01:58 PM

RE: Newbies - Checking File Size in Command Line
 
BASIC TASK 1:
Size of files in directory /musak
_______________________________________________________________
tux@system:/# cd musak
tux@system:/musak# ls -al|awk '{sum += $5}END {print "total size KB:" sum}'
_______________________________________________________________

total size KB:337339


#NOTE1: The command for file size can also be put into a script

STEP 1: CREATE FILE CALLED filesize
_______________________________________________________________
tux@system:/tmp# touch filesize
_______________________________________________________________

STEP 2: INPUT COMMAND INTO filesize

#!/bin/bash
#
echo "which directory do you want directory size"
read dirsize
ls -al $dirsize|awk '{sum += $5}END {print "total size KB:" sum}'

STEP 3: SAVE FILE AND EXIT FROM TEXT EDITOR


#STEP 4: MAKE FILE EXECUTABLE

_______________________________________________________________
tux@system:/tmp# ls -al filesize
_______________________________________________________________

-rw-r--r-- 1 tux tux 5 2011-02-20 14:14 filesize

#NOTE1: Filemask for filesize is -rw-r--r--
Filemask has three parts:
owner-group-world

owner -rwx -> rw- for filesize
group -rwx -> r-- for filesize
world -rwx -> r for filesize

Where:
"r" means read
"w" means write
"x" means execute

Also filemask can be denoted in numerical values:
r - read = 4
w - write = 2
x -execute = 1

Simple math:
rwx = 4 +2 +1 = 7
rw- = 4+2 = 6
r-- = 4

So for filesize with filemask -rw-r--r--
owner -rw = 4 + 2 = 6
group -r = 4
world -r = 4

Sof filesize has filmask:
-rw-r--r-- = 644

#NOTE2: To make a file executable in Linux, it must have filemask of 755

755 is equivalent to mask of
owner = 7 = rwx
group = 5 = r-x
world = 5 = r-x

#NOTE3: To change the filemask on a file, use command "chmod"
_______________________________________________________________
tux@system:/tmp# chmod <mask> <filename>
_______________________________________________________________


To change filemask for filesize from 644 to 755:
_______________________________________________________________
tux@system:/tmp# ls -al filesize
_______________________________________________________________

-rw-r--r-- 1 tux tux 5 2011-02-20 14:14 filesize
_______________________________________________________________
tux@system:/tmp# chmod 755 filesize
tux@system:/tmp# ls -al filesize
_______________________________________________________________

-rwxr-xr-x 1 tux tux 5 2011-02-20 14:19 filesize*

#STEP 5: EXECUTE FILE
To execute a file,
. <period>/<executable filename>
_______________________________________________________________
tux@system:/tmp# ./filesize
which directory do you want directory size
/musak
total size KB:36729296
_______________________________________________________________

#NOTE: Result of "total size KB:36729296" is 36,729,296 KB or 36.7 MB


#BASIC TASK 2: BIGGEST FILE ON HARD DISK

To determine biggest file on hard disk (^_^), re-use previous script:

#STEP 1: CREATE A FILE CALLED bigfile
_______________________________________________________________
tux@system:/tmp# touch bigfile
_______________________________________________________________

#STEP 2: EDIT FILE

#!/bin/bash
#
#comment: Maximum file size is set to 1 GB
#1 GB = 1000 MB = 1000000000 KB
ls -al /|awk '{if ($5 > 1000000000) print "\t" $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $9 }'



#STEP 3: MAKE FILE EXECUTABLE

_______________________________________________________________
tux@system:/tmp# chmod 755 bigfile
_______________________________________________________________

#STEP 4: EXECUTE FILE
#NOTE1: "ls -al /" searches through all directories in your system.
#To specify a directory, change "ls -al /" to "ls -al <directory>" where <directory> is #name of directory you want to search.
_______________________________________________________________
tux@system:/tmp# ./bigfile

-rw-r--r-- 1 root root 5293288757 2011-02-25 08:46 error_log
_______________________________________________________________

#STEP 4: LOCATE FILE
#NOTE1: "error_log" is normally located in "/var/log/httpd" directory {^_~}

_______________________________________________________________
tux@system:/tmp# cd /
tux@system:/# cd var
tux@system:/var# cd log
tux@system:/var/log# cd httpd
tux@system:/var/log/httpd# ls -alh
total 5.0G
drwxr-xr-x 2 root root 4.0K 2010-12-27 19:57 ./
drwxr-xr-x 15 root root 4.0K 2011-02-25 12:32 ../
-rw-r--r-- 1 root root 633K 2011-02-24 21:57 access_log
s-rw-r--r-- 1 root root 5.0G 2011-02-25 08:46 error_log
_______________________________________________________________

To locate a file:

#Method 1: locate
_______________________________________________________________
tux@system:/# locate error_log
_______________________________________________________________


#NOTE2: Error log has to be updated regularly using command:

_______________________________________________________________
tux@system:/# locate -u
_______________________________________________________________

#Method 2: find
_______________________________________________________________
tux@system:/# find / -name "error_log" -print
_______________________________________________________________


#NOTE3: Syntax for find is:

_______________________________________________________________
tux@system:/# find <path> -name "<filename>" -print
_______________________________________________________________

#NOTE4: Find command is more reliable than locate (^_^)
#locate depends on a database but find searches real time.

acid_kewpie 05-17-2011 02:33 PM

What's this all about then?? This is not a wiki / blog...

MTK358 05-17-2011 07:51 PM

First, you shouldn't work as root (notice the "#" prompt).

Also, there's a much better way to get the size of a file:

Code:

stat -c '%s' /path/to/file
The output of "ls" is designed for human reading, it'a a bad idea to try to parse it.

TobiSGD 05-17-2011 08:00 PM

Ever heard of du? If you want to know the size of a folder, just do
Code:

du -sh $folder
By the way, if you want to post code, use code-tags.


All times are GMT -5. The time now is 11:26 AM.