Listing Files and Going Deep Into Filesystems

  1. Using the Linux Command Line
  2. The Linux Directory Tree
  3. Linux Shells and Basic Commands
  4. Changing Passwords
  5. Your Home Directory
  6. User Information
  7. Linux User Default Files
  8. Linux User Profiles and Paths
  9. sudo and su –
  10. Listing Files and Going Deep Into Filesystems
  11. Linux: Creating and Deleting Directories and Files

The ls Command

ls = List of objects in the current working directory.

Typing:

ls

will bring up a list of files and subdirectories. It may also be flagged to provide some different ways of looking at the list of files, such as

ls -l

which will provide a long directory listing (similar to a DOS dir), and

ls -a

which will show all of the files in the directory, including hidden ones and

ls | more

which will provide a directory listing one screen at a time. The ‘|’ is called a ‘pipe’, and passes data from one command to another.

ls -F

gives you a list of files with special notation of their type:

* means “executable”

/ means “directory”

= means “socket”

| means “pipe”

man ls

dir

Columns in the ls –la listing:

permissions

number of hard links

owner

group

size (in bytes)

last-modified date

file name

The permissions column:

directory, link or file?

r = read

w = write

x = execute

user triplet

group triplet

others triplet

The hard links column:

What the heck is a hard link? You can always:

man linkinfo coreutils link

man link

info coreutils link

But we’ll go in-depth below in More About File Systems

The Owner column:

The owner and the author are the same in most *nix, but not in all *nix. The owner is referenced in the owner triad of the permissions. Generally, files under your ownership are in your home directory, but not necessarily (see /var/mail/username).

The Group column:

When you are created as a user, a group with the same name is created, with you as its only member. You may be assigned to other groups, for instance, the wheel group. Your file permissions are set upon file creation. These defaults can be changed.

The Size column:

Size is displayed in bytes, though there are options for showing it in blocks (often 4096 byte blocks).

The Last-Modified column:

Month, Day, Year (if not current year)

Time in “military” format

The File Name column:

Regular files can be named almost anything within this keyspace:

a-z A-Z 0-9 _ – ~ .

You will run into certain special characters that you can’t use.

Unix does NOT use file extensions to identify file type; Gnome and KDE however DO.

Hidden files start with a dot: .

Backup files end with a tilde: ~

Some programs like emacs create backups automatically, creating near-duplicate file names.

Soft links (symlinks) are designated by an l. (That’s the letter l, not the number 1.) Both these and hard links are discussed below in More About File Systems

Hidden files:

Start with a dot

Contain system configuration

Usually text files

Can be directly modified

Now is a good time to become familiar with another useful web site: LinuxCommand.org. I provides some interesting alternative coverage of commands and shell scripting.

For an easy online way to view man pages, see https://ss64.com/bash/. Take a look at some other useful options for the ls commnd. Do you notice if any of them are set by default?

Some Common Options to ls
-a
–all
List all files, even hidden la -a
-A
–almost-all
List all files, except the . and .. usually listed at the top ls -A
-C List in columns ls -C
–color=always Controls whether color is used to distinguish file types: ‘never’, ‘always’, or ‘auto’. ls –color=never
-d
–directory
List directory names, rather than their contents ls -d
-f List without sorting ls -f
-F
–classify
List filenames with markers to indicate type: executable* directory/ socket= or pipe| ls -F
–full-time Gets a long listing with full modification time ls –full-time
-l Long listing ls -l
-lh
-l –human-readable
Long listing with human-readable (simple) file sizes ls -lh
-lG
-l –no-group
-o
Long listing omitting group information ls -lG
-r
–reverse
Lists files in reverse ASCII-betic order ls -r
-R
–recursive
List files in directory, and in subdirectories ls -R
-s List files and size in kilobytes (KB) ls -s
-S List files sorted by size ls -S
-t List files sorted by modification time ls -t
-U Lists files without sorting ls -U
-x List files in rows rather than columns ls -x

Mix and match! Any of the above may be used together.

You can use an initial capital letter to name your directories. Because files are listed “ASCII-betically,” capital letters come before lower-case letters.
The command ll is an alias, on many systems, for ls -l.

Research the file command. What does it do? How does it compare to ls?

More About File Systems: Inodes, Hard Links and Symlinks

Creating a Hard Link

You create a hard link with a command like this:

ln /bin/bash ~/mybash

where the syntax is:

ln target_of_link link_file_name

So you choose a target file (like the bash binary above) and then a location for the hard link (in this case, my home directory, with the name “mybash”).

At the very root of the file system is the superblock. The superblock is the physical location of the inode table. And the inode table in turn is a tabular list of inodes (information nodes), each of which is uniquely numbered, and holds the master information for a file: file size, data block location, last modification time and date; ownership; and permissions. The inode more-or-less “is” the file; when a file is deleted (unlinked), what is actually deleted is its inode. The file’s data blocks are only overwritten when they are needed. And we do need them; in most systems there is a limit of 64K inodes. Yes, we can run out of “file space” even when there is still space on the hard disk. (Want more explanation? Search for “inode” when you get to this page.)

Nothing Prevents A File From Being Listed In Two Different Directories
(or even twice in one directory)

With the above concept clearly in mind, now think of a file that appears in two different directories. What the directory listing is really referring to is an inode. There are not two copies of the file; there is only one. It just shows up in two different places. You can verify this by creating a link, then using the -i option to the ls command:

touch myfile ln ./myfile ./myfile2

ln -li

In this listing, note the inode number on the left. Both myfile and myfile2 have the same inode.

Can You Hard Link Directories?

Normal users can’t. Root can. Generally it’s a bad idea.

There Are Also Soft Links (Symlinks)

Symlinks are somewhat similar to Windows shortcuts. Most actions are passed through the link to the target, i.e. opening a program. Only creation and deletion actually act on the link file itself.

How Do You Create A Symlink?

With a command like this:

ln -s /bin/bash ~/mybash

where the syntax is:

ls -s target_of_link link_file_name

Note the -s option, which makes ln create a soft link (symlink) rather than a hard link.

Can You Soft Link Directories?

Absolutely. This is commonly done to simplify navigation:

ln -s /var/tmp /usr/tmp

Further Discussion

See the System V Unix page that discusses links.