Wednesday, November 7, 2012

ls command howto


Output from ls -la Command

picard@spnode15$ ls -la

total 1600
drwx---s-x  11 picard   STAFF       1536 Jun 26 14:49 .
dr-xr-sr-x1300 bin      bin        20480 Jun 26 12:06 ..
-rw-------   1 picard   STAFF        948 Jun 06 09:46 .addressbook
-rw-------   1 picard   STAFF       3368 Jun 06 09:46 .addressbook.lu
-rw-------   1 picard   STAFF        193 Apr 02 10:06 .article
-rw-------   1 picard   STAFF       1035 May 20 12:30 .bash_history
drwx---S--   2 picard   STAFF        512 Jun 23 13:56 .mailpgp
-rw-------   1 picard   STAFF     128654 Jun 10 19:19 .newsrc
drwx------   4 picard   STAFF        512 May 29 07:01 .pgp
-rw-------   1 picard   STAFF      10196 Jun 26 14:33 .pinerc
-rwxr-xr-x   1 picard   STAFF       1047 May 27 14:15 .plan
-rw-------   1 picard   STAFF         35 Jun 17 09:23 .profile
-rw-------   1 picard   STAFF        371 Sep 08 1995  .signature
-rw-------   1 picard   STAFF      81691 Jun 20 10:34 3dtree.jpg
-rw-------   1 picard   STAFF      31156 Jan 03 10:19 HTMLBgnrGuide.txt
drwx---s-x   2 picard   STAFF        512 Apr 01 13:26 News
-rw-------   1 picard   STAFF      11760 Jul 23 1995  TUTORIAL
-rw-------   1 picard   STAFF        234 Feb 02 08:18 baen.txt
drwx---s-x   2 picard   STAFF        512 Mar 12 06:57 bin
-rw-------   1 picard   STAFF         71 Jul 31 1995  calendar
-rw-------   1 picard   STAFF     338912 May 02 1995  command.memos
-rw-------   1 picard   STAFF        747 Jun 24 13:12 dead.letter
-rw-------   1 picard   STAFF      10506 Jun 01 12:42 info.listserv
-rw-------   1 picard   STAFF     698675 Nov 01 1995  jim.kirk.letters
-rw-------   1 picard   STAFF        122 Jun 24 13:28 junk
drwx------   2 picard   STAFF       1536 Jun 25 12:40 mail
-rw-r-----   1 picard   STAFF       1397 May 28 12:50 mj.ultra
drwx---s-x   2 picard   STAFF        512 May 26 21:09 pine
-rw-------   1 picard   STAFF       1716 Jul 23 1995  print.txt
drwxr-sr-x   6 picard   STAFF       1024 Mar 27 10:54 public_html
drwx---s-x   3 picard   STAFF        512 Mar 31 07:24 rexx
drwx---s-x   2 picard   STAFF        512 Aug 08 1995  temp
-rwx--x--x   1 picard   STAFF        368 May 28 14:10 unpgp

picard@spnode15$

Let's look at this listing, and see what it tells us. At first glance, the left-most column looks like utter nonsense; drwxr--blah-blah-blah. What's going on here?

File Type. The first position/character in this column describes what type of entry each horizontal line represents. The first character will usually be either a - or a d. Symbols in the first (left most) position on the line represent:

-

    = Regular File
d

    = Directory

You can tell, just by looking at the output from ls -l which entries are files and which are directories.

Permissions. The rest of the first column (after the - or d) indicates access permissions which have been set either explicitly, by you, using the chmod command (which we'll look at later) or automatically, by the system, when the file (or directory) was created. We'll discuss this part of the listing in more detail when we get to the chmod command, which deals with setting file access permissions.

Directory Entries (and Hard-Link Count). The next column (immediately to the right of the access permissions) is a number which tells how many directory entries are under that item. For a regular file, this will typically be 1. For a directory, this will always be at least 2. The reason for this is that every directory always contains pointers to both itself, and its parent directory. You can see these two entries as the first two items in our example listing in Figure 12: the entries for . and .. (i.e. one period, and two periods). The single period . ), is the pointer to the current directory--the directory that you are "in" right now. Two periods .. ), point to the parent directory--the directory which contains the current directory. You can use these as convenient nicknames/shortcuts in some commands, when you want to describe a relative pathname.

Owner. The next column to the right displays the userid of the owner of this file or directory. In our listing, most of the files are owned by our hypothetical user, picard. When you create files and directories, you will be their owner, and your userid will show here, in place of picard.

Group. The next column (mostly "STAFF" in our example) shows the name of the user group which is connected with this entry. Each userid is a member of one or more groups, and access permissions may be set which determine what sort of access other members of the same group have to the file or directory named. Again, more on that when we get to the chmod command.

Size. The next column shows the file size in bytes (characters).

Date/Time. The next 3 columns show the date and time the file was last modified.

File Name. Finally, we have the file name. As discussed earlier, UNIX is very accommodating with regard to the length of file names. However there are some qualifications: File names cannot (usually) contain blank spaces, or the characters /, *, or ?. This is because / is used to separate levels of a pathname, * is a "wildcard" character which is expanded by the system to mean "any number of any characters," and ? is a wildcard representing "any single character." MS-DOS users in particular should recognize these "wildcard" characters.


http://docweb.cns.ufl.edu/docs/d0107/ar07s04.html



  • Link counter


Most file systems that support hard links use reference counting. An integer value is stored with each physical data section. This integer represents the total number of links that have been created to point to the data. When a new link is created, this value is increased by one. When a link is removed, the value is decreased by one. If the link count becomes zero, the operating system usually automatically deallocates the data space of the file if no process has the file opened for access. The maintenance of this value assists users in preventing data loss. This is a simple method for the file system to track the use of a given area of storage, as zero values indicate free space and nonzero values indicate used space.

On POSIX-compliant operating systems, such as many Unix-variants, the reference count for a file or directory is returned by the stat() or fstat() system calls in the st_nlink field of struct stat.



  • Link count: File vs Directory 

One of the results of the ls -l command  is the link count.

1. What is the link count of a file?
The link count of a file tells the total number of links a file has.the number of hard-links a file has.
The soft-link is not part of the link count since the soft-link's inode number is different from the original file.

2. How to find the link count of a file or directory?
any new file created will have a link count 1.
By default, a file will have a link count of 1

$ touch  test.c
$ ln test.c test-hardlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:44 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test.c
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test-hardlink.c

$ ln -s test.c test-softlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:45 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test.c
3145744 -rw-rw-r-- 2 vagrant vagrant    0 Mar 31 11:44 test-hardlink.c
3145745 lrwxrwxrwx 1 vagrant vagrant    6 Mar 31 11:45 test-softlink.c -> test.c

3. Does the link count decrease whenever the hard-link is deleted?
When the hard link file is moved or deleted, the link count of the original file gets reduced.

rm test-hardlink.c
ls -lai
total 8
3145743 drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:47 .
3145730 drwxr-xr-x 6 vagrant vagrant 4096 Mar 31 11:44 ..
3145744 -rw-rw-r-- 1 vagrant vagrant    0 Mar 31 11:44 test.c
3145745 lrwxrwxrwx 1 vagrant vagrant    6 Mar 31 11:45 test-softlink.c -> test.c

4. When does the link count of a directory change?
A directory "xyz" is created and the default link count of any directory is 2. The extra count is because for every directory created, a link gets created in the parent directory to point to this new directory.
link count of a directory minus 2 gives you the total number of sub-directories present in the directory.

mkdir xyz
ls -ld xyz/
drwxrwxr-x 2 vagrant vagrant 4096 Mar 31 11:50 xyz/
mkdir -p xyz/abc
mkdir -p xyz/efg
ls -ldia xyz
3145746 drwxrwxr-x 4 vagrant vagrant 4096 Mar 31 11:51 xyz

http://www.theunixschool.com/2012/10/link-count-file-vs-directory.html

  • Q1: The Unix inode structure contains a reference count. What is the reference count for? Why can't we just remove the inode without checking the reference count when a file is deleted?


    Inodes contain a reference count due to hard links. The reference count is equal to the number of directory entries that reference the inode. For hard-linked files, multiple directory entries reference a single inode. The inode must not be removed until no directory entries are left (ie, the reference count is 0) to ensure that the filesystem remains consistent.


  • On a storage device, a file or directory is contained in a collection of blocks

Information about a file is contained in an inode, which records information such as the owner, when the file was last accessed, how large it is, whether it is a directory or not, and who can read from or write to it. The inode number is also known as the file serial number and is unique within a particular filesystem
https://developer.ibm.com/tutorials/l-lpic1-104-6/

1 comment: