≡ Menu


How To Extract a Single File / Directory from Tarball Archive

Q. How do I extract a single file or directory form a tarball under UNIX / Linux shell prompt? How do I restore a single file from /dev/st0 tape device?
[click to continue…]

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


-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


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

Linux / UNIX – Display the permissions of a file

Q. How do I list or display the permission of a file using ssh? I don’t have GUI installed on a remote Linux computer.

A.You need to use ls command with -l option. File access permissions are displayed in the first column of the output, after the character for file type.

ls command List information about the FILEs. If no argument is given it will use the current directory by default.

Task: List a file's access permissions

Type ls -l command as follows to display permission for /etc/passwd file:
$ ls -l /etc/passwd

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

Understanding the file permission

File access permissions appear in the first column of the output i.e. -rw-r--r--

  • The first character - is nothing but the file type. means regular file and d means directory.
  • The next three characters (rw-) specify permissions for the user who owns the file
  • The next three (r--) specify permissions for all members of the group that owns the file.
  • Finally, the last three characters in the column (r--) specify permissions for all other users on the system.

Each character in permission has meaning as follows:

  • r : Read permission.
  • w : Write permission.
  • x : Execute permission.
  • - : No permission.

For example rw- permission means owner can read, write to a file but cannot execute the same.

Search All the Linux Man Pages For a Particular Command or Text

Under KDE or Gnome desktop I get nice facility to search all man pages for particular command or phrase. I am login to remote server over the ssh session. How do I search all the man pages for a particular command at Linux shell prompt?
[click to continue…]

Bash Read Comma Separated CVS File

How do I read comma separated CVS file under UNIX / Linux / BSD / Mac OS X bash script? My sample file is as follows:

FirstName LastName,DOB,SSN,Telephone,Status

[click to continue…]

HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD

How do I compile C program and create an executable file under Linux or UNIX operating systems?
Sample Unix C Program
[click to continue…]