≡ Menu

ls command

Find Out Linux Kernel Modules ( Drivers ) Location / Directory

Can you tell me an exact location of Linux kernel driver under Linux file system? Where to find all available modules under Linux operating systems?
[click to continue…]

Linux / UNIX: List Open Files for Process

Q. How do I list all open files for a Linux or UNIX process using command line options?
[click to continue…]

Explain Linux / UNIX dot-files

Q. Can you tell me more about dot-files that shell and many UNIX command reads?
[click to continue…]

Bash Shell Check Whether a Directory is Empty or Not

How do I check whether a directory is empty or not under Linux / UNIX using a shell script? I would like to take some action if directory is empty on a Linux or Unix like system. How can I check from bash/ksh shell script if a directory contains files? How do I check whether a directory is empty or not?
[click to continue…]

How to: Linux / UNIX create soft link with ln command

Q. How do I create a soft link (symbolic link) under UNIX or Linux operating system?

A. To make links between files you need to use ln command. A symbolic link (also known as a soft link or symlink) consists of a special type of file that serves as a reference to another file or directory. Unix/Linux like operating systems often uses symbolic links.

Two types of links

There are two types of links

  • symbolic links: Refer to a symbolic path indicating the abstract location of another file
  • hard links : Refer to the specific location of physical data.

How do I create soft link / symbolic link?

To create a symbolic link in Unix or Linux, at the shell prompt, enter the following command:
ln -s {target-filename} {symbolic-filename}

For example create softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command:
ln -s /webroot/home/httpd/test.com/index.php /home/vivek/index.php
ls -l

Output:

lrwxrwxrwx 1 vivek  vivek    16 2007-09-25 22:53 index.php -> /webroot/home/httpd/test.com/index.php

Linux / UNIX: Display file inode (index number)

Q. How do I display file inode number using command line?

A. An inode is a data structure on a Unix / Linux file system. An inode stores basic information about a regular file, directory, or other file system object. You can use following two commands to display an inode:
[a] ls command : list directory contents

[b] stat command : display file or file system status

ls command

Type ls command with -i ( print the index number of each file ) option:
$ ls -i /etc/passwd
Output:

752010 /etc/passwd

752010 is index number (inode) for /etc/passwd file.

stat command

$ stat /etc/passwd
Output:

  File: `/etc/passwd'
  Size: 2026            Blocks: 8          IO Block: 4096   regular file
Device: 811h/2065d      Inode: 752010      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-08-20 23:19:20.000000000 +0530
Modify: 2007-07-07 00:06:56.000000000 +0530
Change: 2007-07-07 00:06:56.000000000 +0530

You can use inode number to delete or search a file.

Linux Rules for file names

Q. Can you specify rules for naming a file name under Linux?

A. When you save a data, you nee to select a file name, you need to follow certain rules and regulation regarding the length of the name and the types of characters you include.

Rules for naming file names

The rules for naming a file names under Linux (and UNIX like oses) are as follows:

[a] The file names can be up to 255 characters (or bytes) long

[b] You cannot use all special characters, try to use:
=> Uppercase or lowercase letters
=> Digits
=> Special characters, such as: +, -, _, .

[c] File names are case-sensitive. Thus, the following file names all are different:

vivek Vivek VIVEK VIVek

[d] Try to avoid non printable and following characters in filenames:
/, >, <, ?, “, ‘, blank space

How do I show hidden files?

Use ls -a or ls -la command:
$ ls -la
$ ls -a

Use -a to see invisible file names.

How do I access filenames with spaces?

To access a filename with spaces, enclose the filename in quotes:
$ cat "my bio data.txt"
$ cd "a long dir nane"

Please note that above rules almost apply to other UNIX like operating system such as HP-UX, Solaris, FreeBSD etc.

Examining the Linux / FreeBSD / UNIX filesystem with ls command

Q. I’m new to Linux and how do I examine filesystem and identify regular files or directories?

A. Both Linux and UNIX comes with ls command for examining the filesystem. You can use ls command to look at the filesystem. ls command can display:
=> Character devices
=> Regular files
=> Sym links (symbolic links)
=> Directories
=> Pipes
=> Sockets
=> Block devices

ls command examples

Display /etc directory files, enter:
$ ls /etc
When invoked without any arguments, ls lists the files in the current working directory:
$ ls
Use -l (long option) to lists filenames, sizes, permissions, type and all other information:
$ ls /etc/passwd
Output:

-rw-r--r-- 1 root root 2453 Jul 17 16:25 /etc/passwd

See Linux / UNIX file permissions for more information

Directory

A directory is marked with a d as the first letter of the permissions field:
ls -ld /etc
Output:

drwxr-xr-x 88 root root 12288 Aug  5 23:46 /etc

Symbolic link

A symbolic link is marked with an l (lower case L) as the first letter of the permissions string:
ls -l /bin/nisdomainname
Output:

lrwxrwxrwx 1 root root 8 Jul 10 08:50 /bin/nisdomainname -> hostname

Similarly,

  • A named pipe is marked with a p as the first letter of the permissions string.
  • A socket is marked with a s as the first letter of the permissions string.
  • A character device is marked with a c as the first letter of the permissions strings. (ls -l /dev/console)
  • A block device is marked with a b (ls -l /dev/sdb1).

Linux / UNIX: Device files

Q. Can you explain me what is device files and how do I access or see device files? Why UNIX / Linux has device files?

A. Under Linux and UNIX each and every hardware device treated as a file. A device file allows to accesses hardware devices so that end users do not need to get technical details about hardware.

In short, a device file (also called as a special file) is an interface for a device driver that appears in a file system as if it were an ordinary file. This allows software to interact with the device driver using standard input/output system calls, which simplifies many tasks.

Device file two types

There are two types of device files based upon how data written to them and read from them is processed by the operating system and hardware:

  • Character special files or Character devices
  • Block special files or Block devices

Understanding Character special files or Character devices

  • Talks to devices in a character by character (1 byte at a time)
  • Examples: Virtual terminals, terminals and serial modems etc

Understanding Block special files or Block devices

  • Talks to devices 1 block at a time ( 1 block = 512 bytes to 32KB)
  • Examples: Hard disk, DVD/CD ROM, and memory regions etc

Why use device files?

Device file allows transparent communication between user space applications and computer hardware.

Device file location

All device files are stored in /dev directory. Use cd and ls command to browse the directory:
cd /dev/
ls -l

How do I find out the device file type?

Simply use ls -l command:
ls -l /dev
Look for file’s type in the first column output.

A character device is marked with a c as the first letter of the permissions strings.
$ ls -l /dev/console
A block device is marked with a b as the first letter of the permissions strings:
$ ls -l /dev/sdb1

Linux / UNIX find the smallest directories or files in current directory

Q. How do I list or find the smallest directories or files in the current directory under Linux or UNIX like operating system?

A. There is no direct command exists for this task. However by using shell pipes and combination of other commands one can produced the desired result.

Task: Display list of smallest files

You need to use ls command and pass the option -l (long format) -S (sort) -r (in reverse order), enter:
$ ls -lSr
$ ls -lSr
$ ls -lSr | head
$ ls -lSr | head -5

Output:

-rw-r--r--  1 root root         0 May 29 07:08 Muttrc.local
-rw-r--r--  1 root root         0 Jan 12  2000 motd
-rw-r--r--  1 root root         0 Jan 12  2000 exports
-rw-r--r--  1 root root         0 Nov 28  2006 environment
-rw-rw-r--  1 root disk         0 Aug  7  2006 dumpdates
-rw-r--r--  1 root root         0 Jul 10 08:50 cron.deny
-rw-------  1 root root         1 Aug 23  2006 at.deny
lrwxrwxrwx  1 root root         7 Jul 10 08:50 rc -> rc.d/rc
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc6.d -> rc.d/rc6.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc5.d -> rc.d/rc5.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc4.d -> rc.d/rc4.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc3.d -> rc.d/rc3.d
lrwxrwxrwx  1 root root        10 Jul 10 08:50 rc2.d -> rc.d/rc2.d

Task: Display list of smallest directories

You need to use du command to display sorted (-S option) output. Use pipe to send du command output to sort command for sorting purpouse:
$ du -S . | sort -n
$ du -S . | sort -n | head -10

Output:

du -S . | sort -n | head -10
4       ./lighttpd/ssl
4       ./monit.d
8       ./acpi
8       ./acpi/actions
8       ./alchemist
8       ./alchemist/namespace
8       ./alternatives
8       ./desktop-profiles
8       ./dev.d
8       ./dev.d/default

Read the man page of ls, sort and du for more options:
man ls
man du
man sort