LinuxQuestions.org
Help answer threads with 0 replies.
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 05-17-2011, 02:58 PM   #1
tix
LQ Newbie
 
Registered: May 2011
Posts: 24

Rep: Reputation: 0
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.
 
Old 05-17-2011, 03:33 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
What's this all about then?? This is not a wiki / blog...
 
Old 05-17-2011, 08:51 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
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.

Last edited by MTK358; 05-17-2011 at 08:53 PM.
 
Old 05-17-2011, 09:00 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852Reputation: 4852
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.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
TIP: Reducing PDF file size from the command line with GhostScript (gs) ShellyCat Linux - Newbie 3 07-12-2012 05:11 AM
[SOLVED] Question: OpenBSD command line for checking list of library used by daemon lcxpics *BSD 3 04-28-2011 11:04 PM
command line font size poiuytrewq Linux - Newbie 8 09-21-2004 03:57 AM
Command Line Screen Size bgfay Linux - Newbie 2 06-22-2004 09:58 PM
checking memory usage from command line wrepti Linux - General 2 06-16-2004 11:24 PM

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

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