Home > Unix > UNIX – Lesson 003 – ls, touch, date commands

UNIX – Lesson 003 – ls, touch, date commands

Working with files

A file is a container of data, or a link to a device. There are three kind of files :

  • ordinary file (or regular file)
  • directory file
  • special file (or device file)

Ordinary : text, data, program, source code, etc.
Directory : special file that contains the name of the files and directories that it holds.
Special : provides the interface between the kernel and the hardware device

Note: files prefixed by a dot (.) are hidden files (i.e. “.cshrc”)


The “ls” command lists the content of a directory
Syntax :
ls [-adlFRtu] [pathname(s)]

Key argument:

  • -a   All entries (also hidden files)
  • -d   If argument is a directory list only the name (instead of the contents)
  • -l    Long list format (sorted by name)
  • -F   Put special characters to identify the file types (marks directories with /, executable files with *, and symbolic links with @)
  • -R   Recursively list subdirectories
  • -t    Sorts by “time stamp” (last modification time)
  • -u   Uses time of last access instead of last modification for sorting

Typing ls -l (long list) you see a group of rows like this:

-rwxrwxr-x 1 xarabas admins 22 Feb 20 14:53 test_tmp

  • Reading from right to left, you see that the current directory holds one file, named “test_tmp”
  • The last time that file’s contents was modified is 14:53 of February 20.
  • The file contains 22 bytes
  • The owner of the file, belongs to the group “admins”, and his login name is xarabas
  • The number 1, indicates the number of links to file “test_tmp”
  • The dash and letters tell you that user, group, and others have different permissions to read, write, and execute “test_tmp”

Something about the permissions (in the next lessons we speak about it in details mode) …

-rwxrwxr-x 1 xarabas admins 22 Feb 20 14:53 test_tmp

The first triplets are for owner
The second one triplets are for group
The last triples are for other (like the everyone in microsoft permission)

The permissions are indicated as follows:

  • r    the file is readable
  • w   the file is writable
  • x   the file is executable
  • –    Indicate that permission is not granted

$ ls -l h*
-r-xr-xr-x 1        root      root       916       Jun 10 1996  hostname
-r-xr-xr-x 1        root      root       1531     Jul 6 1999     hp-webadm
-r-xr-xr-x 1        root      root       5821     Jun 15 1999  hparamgr

$ ls –lat
drwx——    41 xarabas admins    3584     Feb 20 11:40   ./
drwxr-xr-x 9  xarabas admins    512       Feb 20 13:57   .dt/
-rwxr–r–    1   xarabas admins    7379      Feb 20 09:48  lista.txt*

$ ls -lF
drwxr-xr-x 4     root      sys     96           Jun 27 15:38  hpxt/
-rwxr-xr-x  1     root      sys     24576   Dec 4 2000     inq.hp*
lr-xr-xr-t    1      root      sys     8          Jun 10 1996    lib@ -> /usr/lib

The “touch” command creates an empty file or change modification and access time, if the file exists (if you run it on directory it changes the access time of directory).
Syntax :
touch filename or touch directory or touch “directory/*”

$ ls –la test*

-rwxrwxr-x 1     xarabas    admins   1261      Feb 20 14:53      test_tmp

$ touch test_tmp test_tmp1

$ ls –la test*
-rwxrwxr-x 1     xarabas    admins   1261      Feb 21 10:25      test_tmp
-rw-r–r–      1     xarabas    admins   0            Feb 21 10:25      test_tmp1

The “date” command writes the date and time
date [options]

To format a date provide a string beginning with + .

Key argument:

%a Weekday abbreviated %y Year in two digit
%A Weekday full %Y Year Full
%d Day of the month %l Hour (12 hour clock)
%j Day of the year %H Hour (24 hour clock) zero padded
%V Week of the year %M MM minutes
%m Month numeric %S Second
%b Month abbreviated %r Hours, minutes, seconds (12-hour clock)
%B Month full month %T Hours, minutes, seconds (24-hour clock)

# date +%a

# date +%y

# date +%Y

# date +%d

# date +%b

# date +%B

# date ‘+DATE: %m/%d/%y%nTIME:%H:%M:%S’
DATE: 02/27/08

Categories: Unix
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: