Solved

Interview Question

Posted on 2009-07-01
18
732 Views
Last Modified: 2013-12-26
Hi all,

This is one of the interview question. Please see the attached output of ls -lrt | grep "^d"

The command shows the directories of the current folder.

In the output what is mean by 512, 1024, 45056

How it differs (in what condition )

thanks
kamaraj.s
drwxrwxrwx   9 ks296a   frame        512 Feb 24 09:44 usrpr9.4

drwxrwxrwx   2 ks296a   frame        512 Feb 25 07:06 UAT

drwxrwxrwx   2 ks296a   frame        512 Mar  6 02:34 awk

drwxrwxrwx   2 ks296a   frame        512 Mar  9 10:07 log

drwxrwxrwx   2 ks296a   frame       1024 Mar 13 13:06 PD148

drwxrwxrwx   2 ks296a   frame        512 Apr 10 04:27 7201

drwxr-xr-x   2 ks296a   frame        512 Jun  5 10:41 prod_cadm_issue

drwxrwxrwx   4 ks296a   frame        512 Jun  6 06:19 june_proj

drwxr-xr-x   9 ks296a   staff        512 Jun 11 03:28 usrpr9.10

drwxr-xr-x   2 ks296a   frame        512 Jun 18 10:25 chg_pvc

drwxrwxrwx   2 ks296a   frame      45056 Jun 30 07:12 public_html

drwxr-xr-x   2 ks296a   frame        512 Jul  1 03:33 oct_proj

Open in new window

0
Comment
Question by:Kamaraj Subramanian
  • 6
  • 6
  • 5
  • +1
18 Comments
 
LVL 9

Expert Comment

by:DCMBS
Comment Utility
The figures refere to the size of the directories in KB
0
 
LVL 23

Author Comment

by:Kamaraj Subramanian
Comment Utility
I dont think your answer is correct. Sorry
0
 
LVL 23

Author Comment

by:Kamaraj Subramanian
Comment Utility
I just want the answer for the below quetion

How it differs (in what condition )
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Size is in BYTES.
The entries differ in regard to permissions (rwx...), number of links ( . , .. and subdirectories), group ownership, size (number of inodes*inode-size), date/time and name.
0
 
LVL 23

Author Comment

by:Kamaraj Subramanian
Comment Utility
>>>>The entries differ in regard to permissions (rwx...), number of links ( . , .. and subdirectories), group ownership, size (number of inodes*inode-size), date/time and name.


Can i have a example ?

I have created a simple directory in /tmp folder of my AIX machine

and i checked the size it is 512 after that i copied some files to my directory. then now it comes to 2048

It seems it increases like 512, 1024.......

At what basics it exactly changes ?

please suggest.

thanks
kamaraj.s

$ cd /tmp

$ mkdir kamaraj

$ ls -lrt | grep "^d"

drwxr-xr-x   3 training staff        512 Jun  4 12:57 test

drwxr-xr-x   2 IBMUser9 billjoy      512 Jun  9 13:53 munna

drwxr-xr-x   2 training staff        512 Jul  1 12:53 kamaraj
 

$ ls -lrt

-rw-r--r--   1 training staff         49 Jun 18 07:13 immi.sh

-rw-------   1 training staff      41765 Jun 19 06:56 man51246

-rwxrwxrwx   1 training staff         15 Jun 20 10:51 mycmds.sh

-rw-r--r--   1 training staff        129 Jun 20 11:14 script1.sh

-rwxrwxrwx   1 training staff        129 Jun 20 11:16 yscript1.sh

-rw-r--r--   1 training staff        869 Jun 20 11:29 who1

-rw-r--r--   1 training staff        869 Jun 20 11:29 who2

-rwxrwxrwx   1 training staff        158 Jun 20 11:42 yscript2.sh

-rwxrwxrwx   1 training staff         53 Jun 20 11:56 yscript3.sh

-rw-------   1 training staff      12000 Jun 23 08:35 man17294

-rw-------   1 training staff      12000 Jun 23 13:56 man19544

-rw-------   1 training staff       5100 Jun 25 05:18 man50958

-rw-r--r--   1 training staff        190 Jun 25 14:10 df.work

-rw-r--r--   1 training staff         65 Jun 25 14:10 df.outfile

-rw-------   1 training staff       2287 Jun 29 09:12 man41864

-rw-------   1 training staff       5166 Jul  1 05:55 man36400

-rw-------   1 training staff      12994 Jul  1 10:23 man51258

-rw-------   1 training staff      41765 Jul  1 10:25 man50990

drwxr-xr-x   2 training staff        512 Jul  1 12:53 kamaraj

$ cp man50990 kamaraj/

$ ls -lrt | grep "^d"

drwxr-xr-x   3 training staff        512 Jun  4 12:57 test

drwxr-xr-x   2 IBMUser9 billjoy      512 Jun  9 13:53 munna

drwxr-xr-x   2 training staff        512 Jul  1 12:54 kamaraj

$ cp * kamaraj/

cp: kamaraj is a directory (not copied).

cp: man19022: Permission denied

cp: man19052: Permission denied

cp: man19160: Permission denied

cp: man23046: Permission denied

cp: man30688: Permission denied

cp: man45304: Permission denied

cp: man49308: Permission denied

cp: munna is a directory (not copied).

cp: test is a directory (not copied).

$ ls -lrt | grep "^d"

drwxr-xr-x   3 training staff        512 Jun  4 12:57 test

drwxr-xr-x   2 IBMUser9 billjoy      512 Jun  9 13:53 munna

drwxr-xr-x   2 training staff       2048 Jul  1 12:54 kamaraj

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
You're right, directory sizes for AIX's jfs/jfs2 (not NFS) are always evenly divisible by 512.
I think, as directory size grows, the increment grows with it, but I don't know the exact algorithm.
 
0
 
LVL 9

Expert Comment

by:DCMBS
Comment Utility
Apologies for typo, LS outputs size in bytes not KB.

The ls size is not telling you the total size of all the files and subdirectories inside the directory. It is telling you the the amount of bytes it needs to define the files/subdirectories that it contains. To see the total size of all the files and subdirectories of a directory, you should use use "du -s directory".
0
 
LVL 9

Expert Comment

by:DCMBS
Comment Utility
Just to follow up on my post.

When the ls command lists a size for a directory it allocates size in blocks.  The size of the block varies with distribution and in your distribution it seems to allocate sapce in 512 byte blocks.  Thus a new dirextory will have a size of 512 butes and when this is full it will jump to 1024 byes the 2048 and so on.
0
 
LVL 23

Author Comment

by:Kamaraj Subramanian
Comment Utility
Hi,

just see the output of the command which i tried now

I have a file called sw.pdf ( more than 2MB) which is present inside the directory called kamaraj.

But eventhough it is showing as 512.

My solution is :  the size grows like 512, 1024.... ( It only grows when the no. of files goes increase (not matter about the size of the file))

thanks
kamaraj.s
$ ls -lrt | grep "^d"

drwxr-xr-x   3 training staff        512 Jun  4 12:57 test

drwxr-xr-x   2 IBMUser9 billjoy      512 Jun  9 13:53 munna

drwxr-xr-x   3 training staff        512 Jul  1 13:34 kamaraj

$ cd kamaraj

$ ls -lrt

total 4952

drwxr-xr-x   2 training staff        512 Jul  1 13:32 inside_dir

-rw-r-----   1 training staff    2528590 Jul  1 13:35 sw.pdf

$ pwd

/tmp/kamaraj

$

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Yes, that's absolutely correct. The directory only carries a reference to the file, whose size is independent of the actual filesize.
0
 
LVL 23

Author Comment

by:Kamaraj Subramanian
Comment Utility
but, is that file references are not completely delete once we remove the files in the directory ?

I am confused about this conversion. Now i am using solaris. The solaris machine output gives below

check for the public_html dir
drwxr-xr-x   2 ks296a   frame        512 Jun 18 10:25 chg_pvc

drwxrwxrwx   2 ks296a   frame      45056 Jun 30 07:12 public_html

drwxr-xr-x   2 ks296a   frame        512 Jul  1 03:33 oct_proj

ks296a@/home/moose1/ks296a:>du -s public_html

88      public_html

ks296a@/home/moose1/ks296a:>cd public_html

ks296a@/home/moose1/ks296a/public_html:>ls -lrt

total 0

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
Yes they are, but a directory never shrinks. You'd have to delete and recreate it to reduce its size once it is empty.
And Solaris and AIX seem consistent, don't they?
0
 
LVL 9

Expert Comment

by:DCMBS
Comment Utility
Please see my post.  The figure is the amount of space in bytes needed to define the files/subdirectories that are contained in the directory. This space is allocated when the directory is created and increased as neccesary as files are added.  Therfore an empty directory will show as 512 bytes.  As files are added this space will fill up and more sapce will be added in 512 byte blocks
0
 
LVL 23

Author Comment

by:Kamaraj Subramanian
Comment Utility
>>>>>>>As files are added this space will fill up and more sapce will be added in 512 byte blocks

In what basics it will increase ?



0
 
LVL 9

Expert Comment

by:DCMBS
Comment Utility
The space is the amount of space used by the operating system to hold the directory information about the files contained in the directory so that the operating system can locate those files.  When a folder is created this space is allocated as 512 bytes.  As the directory fills up it will require more space to hold the directory information so the operating system will allocate more space in 512 byte blocks for this purpose. So the space will grow from 512 to 1024 to 1536 to 2048 and so on as more files are added to the directory.  i.e the space will be always increase by adding 512 bytes to the previous value.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 167 total points
Comment Utility

From my experience -
512 - 1024 - 2048 - 4096 - 8192 - ..... ( increment stays at 4K)
This is for AIX jfs!
jfs2 is more flexible. An empty directory can start at 256.
NFS is quite different.
0
 
LVL 9

Assisted Solution

by:DCMBS
DCMBS earned 167 total points
Comment Utility
Yes you are probably right. I am not totally sure about the size of the increment.  I tried to deduce it from the information above but I have seen the 4K value mentioned before so it could well be 4K
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 166 total points
Comment Utility
In addition to what other experts already explained and commented, in unix, a directory is nothing but a file that contains index for files and directories in it. The content of this file is a name and a pointer (inode number). The dir file will increase in size like any file when its content is increasing and will not shrink in size since there is a potential increase in  the files and dirs under it.

To reduce the size of this file , you simply move contents to other dir , delete the empty dir, and recreate it, then move back files and dirs.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countEvens challenge 2 55
isEverywhere  challenge 19 61
either24  challenge 19 83
Issue with cURL on Windows 7 65
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now