LinuxQuestions.org
Review your favorite Linux distribution.
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 12-07-2010, 04:30 PM   #1
badger_fruit
Member
 
Registered: Dec 2008
Posts: 43

Rep: Reputation: 1
What are "ls -l" 's columns?


I did an ls -l of two of my folders on my filesystem and got these two outputs:-

Code:
drwxr-xr-x 1 badger_fruit users 0 2010-03-16 23:59 NAME
and

Code:
drwxr-xr-x 2 badger_fruit users          0 2010-05-17 23:01 NAME
I get that
Col1 = attributes (drwxr-xr-x)
Col2 = ??? (1 or 2)
Col3 = Owning user (badger_fruit)
Col4 = Owning user group (users)
Col5 = file size (0 as they're directories)
Col6 = Date & time modified/created (2010-05-17 23:01)
Col7 = File/Folder name (NAME)

Note in the first ls -l output, column 2 is set to 1 whereas in the second ls -l, it's 2. Any ideas why or what column 2 actually is
 
Old 12-07-2010, 04:40 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
For a file col2 is the number of hard links to it,
for a directory is the number of blocks its content occupies
(according to "info coreutils ls").



Cheers,
Tink
 
1 members found this post helpful.
Old 12-07-2010, 04:41 PM   #3
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
http://www.unix.com/unix-dummies-que...-headings.html
Column 2 is the number of links (hard links?) to that file.

Curses! Late again!
 
1 members found this post helpful.
Old 12-07-2010, 04:59 PM   #4
badger_fruit
Member
 
Registered: Dec 2008
Posts: 43

Original Poster
Rep: Reputation: 1
Tink and Stress, thank you, I appreciate the replies very much; I tried reading man and info on ls but it messed up my head too much lol!
 
Old 12-07-2010, 05:05 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I recommend discarding some of the comments in the thread that stress linked
to - obviously one of the guys who responded there didn't have a clue of what
he was talking about ... *sigh* =o)



Cheers,
Tink
 
Old 12-07-2010, 05:35 PM   #6
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by Tinkster View Post
I recommend discarding some of the comments in the thread that stress linked
to - obviously one of the guys who responded there didn't have a clue of what
he was talking about ... *sigh* =o)
Yes. I automatically skip over the garbage when reading stuff like that. I didn't consider that others may not be so capable of separating the wheat from the chaff.
 
Old 12-08-2010, 12:39 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
A good description of ls output at docweb.cns.ufl.edu
 
Old 12-08-2010, 02:08 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by catkin View Post
A good description of ls output at docweb.cns.ufl.edu
In regards to the 2nd column and directories that links'
explanations is actually wrong, too ...



Cheers,
Tink
 
Old 12-08-2010, 02:14 AM   #9
chickenjoy
Member
 
Registered: Apr 2007
Distribution: centos,rhel, solaris
Posts: 200

Rep: Reputation: 30
based on what I have read and did a test on it before; the 2nd column shows how many hard links (not soft links) are existing for that particular inode number (or in layman's term: a file name)
 
Old 12-08-2010, 02:34 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Tinkster View Post
In regards to the 2nd column and directories that links'
explanations is actually wrong, too ...
So it is!
Code:
c@CW8:/tmp$ mkdir dir
c@CW8:/tmp$ ls -ld dir
drwxr-xr-x 2 c users 1 Dec  8 12:58 dir
c@CW8:/tmp$ for i in $( seq 5000 ); do touch dir/file$i; done
c@CW8:/tmp$ ls -ld dir
drwxr-xr-x 2 c users 40960 Dec  8 13:01 dir
So what does the second column indicate?
 
Old 03-03-2013, 06:22 AM   #11
thomanski
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Rep: Reputation: Disabled
Lightbulb To summarise

The second column of ls -l output shows number of hardlinks for files and the number of contained directories for directories. The latter includes the always-present directories . and .. so the count for directories will always be at least 2:

$mkdir ~/test-2nd-ls-column
$cd ~/test-2nd-ls-column
$ls -ld .
drwxr-xr-x 2 thomas users 96 2013-03-03 12:13 .
$for i in $( seq 10 ); do mkdir dir$i; done
$ls -ld .
drwxr-xr-x 12 thomas users 24576 2013-03-03 12:14 .
$ cd ..
$ rm -rf test-2nd-ls-column
 
Old 03-03-2013, 08:18 PM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
In all cases, it is the number of hard links.

A subdirectory always has a link to its parent (the .. entry). Thus ANY directory will always have a minimum of two links. One from its parent directory, and one for its "." entry.

Thus a directory "a" will have two links (one for itself - the "." entry, and one from its parent directory). If a has a subdirectory then it will have 3 links.
 
Old 03-04-2013, 06:11 AM   #13
thomanski
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Rep: Reputation: Disabled
Thanks for your explanation. So what gets counted is not the contained directories including . and .. but instead the named link of the parent, the . hard link in the dir in question and the .. hard links in directories contained in the dir in question:

x # the containing directory's link to x
x/. # the x directory's own link to itself
x/a/.. # contained directory's uplink
x/b/.. # "
x/c/.. # "


So this should be five hardlinks and of course it is:

$mkdir x; cd x; mkdir a b c; ls -ld .
drwxr-xr-x 5 thomas users 96 2013-03-04 12:01 .


While it gets to the same number as my previous understanding, it sure does make more sense this way.
 
Old 03-04-2013, 09:35 AM   #14
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Yes..

It is designed to be doubly linked tree - each node contains pointers to its children. Each child contains a point to its parent. To make some operations easier each node has a link to itself.

In the simple case, a directory is nothing but a file containing a list:

inode - the number that is an index to where pointers to the data storage of the file is.
name - an ascii string to provide the name.

You can examine the inode number using "ls -i". If you want to examine a directory in detail you can use "ls -ail" which will show the hidden files (the a), the inode number (the i) and the long listing format. The result is to put the inode number as the first column. The rest come from the long listing format.

In a base structure, starting at the beginning of the filesystem, inode 1 may or may not get some odd use (additional mounts can hide things). So warnings about doing this on the root filesystem (it has lots of mounts there, so each mount shows its base inode of 1 - you have to get into deep details to see which filesystem that inode might be on).

But the simple case is:

inode 1 represents the filesystem beginning. Therefore this file is a directory, containing two entries, "." and "..". Now because this is the beginning of the tree (the root), BOTH entries point to the same inode - a possible listing (just inodes):

1 .
1 ..

Other files created will add to the link count (.lost+found is nearly always there). On older systems lost+found was always inode 2. It isn't anymore because there are a number of files created during the creation of a filesystem - lost+found is one, quotas, special attributes, readahead information...

But for a simple one, I'll just stick to the minimum.

If a directory (named a) is created on the filesystem, then the new list would be:

1 .
1 ..
2 a

So the directory a has been created - it is named "a" in the root directory, and has two entries:

2 .
1 ..

Thus the link count for the root directory (inode 1) has to increase by 1. The inode 2 gets two links - one for the root directory entry (named a), and one for itself (the "." entry within the directory a which is inode 2.

Now regular files can also be created - so lets call it "text", and put it in the root directory. The list now appears:

1 .
1 ..
2 a
3 text

This makes the number of links to the regular file "text" (inode 3 here) to be 1.

SUPPOSE, an hard link to the "text" file is created in the directory a and is called "test". The result in the directory a is:

2 .
1 ..
3 test

The link count for the file test (represented by inode 3) now has the value 2. The data for the file has not been copied (that would require another inode).

If the file "text" is removed from the root directory (using the rm command), then the link count for inode 3 goes back to 1. But no data is lost - the link count is still greater than 0.

If the file "test" is removed from the directory a, then the link count for inode 3 goes to 0, and the file may be reclaimed for use. What happens is that there is a flag in the inode that says "inuse/free" and it is either set (in use) or clear (free). Until the inode is reused, you can actually recover the deleted file... but note: this just recovers the inode. Any data blocks that have been used by the file were also released. So recovery of data can still be difficult (it is a special process to go out and mark the free data blocks as being in use again...)

In the early UNIX/Linux implementations, a file rename is done by the link/unlink system calls. The link just adds a name to a directory using to pre-existing file name to get the inode number to use. The unlink system call then just removes the directory entry, thus renaming the file. The problem this had is that it allows problems to occur - what happens if the program doing the link/unlink gets interrupted between the link and unlink... you get both the original name and the new name. To prevent the race condition, a new system call was added - the rename, which has exactly the same parameters as the link system call.

I should quit now...
 
Old 03-04-2013, 05:17 PM   #15
thomanski
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Rep: Reputation: Disabled
Wow, thank you ever so much for this, I've learnt a lot!
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 07:58 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 08:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
Removing the "enter" symbols in the columns of emacs? Erik_the_Red Linux - Software 2 07-18-2005 05:03 PM


All times are GMT -5. The time now is 09:57 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