≡ Menu

ls command

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


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

752010 /etc/passwd

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

stat command

$ stat /etc/passwd

  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.

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

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

See Linux / UNIX file permissions for more information


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

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

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


  • 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).