Creation of Files in UNIX (Session-03).

 Creation of Files in UNIX (Session-03).


As discussed earlier this is one sort of security measure as soon as user switch on the system the user will be prompted for login id.This login id is created by the administrator(#).

At the Login: prompt, enter your username.

At the Password: prompt, enter your password. …



Being a normal user we can not create a password for the account but we can change if this password is used to preserve or protect the data in the account such that no devil minded user may login to your  account and do some irregular actions to the data content hence its recommended to have password for the user

Another safety measure is that whenever user types his passwd the cursor is least bothered to move from its position and if by mistake u entered wrong password the shell wont prompt u that is that a “wrong id” or “password”.

In below example from root user we trying to login into another user called oracle using “su” command .while logging from root use we don’t require any password.

I below example from “ec2-user” we trying to login into same “oracle” user .This time password required.

To check all the users : cat /etc/passwd



What exactly a file is ?

we can say a file is a collection of data or an information which a user stored for further reference.

Ways of creating a file :

A file can be created in 4 ways to create a file they are as follows :




4.VI(Visual editor)

At this time let us stick to cat command and remaining 3 will be commenced in future.


 Syntax :-

Uses of cat command :

1.We can create a new file.

2.We can view data inside a file.

3.We can append the data into existing file.

Examples : 

$cat >(filename)

This will create a new file and gives the privilege for user to edit few text in it after editing the text hit ctrl+d this will save and quit the file.

$cat >>(filename)

This command will append text in the existing file contents after entering hit ctrl+d to save and quit.

$cat >.(filename)

This will create a hidden file and remaining the same procedure for normal file creation.

 $cat >>.(filename)

Appends the text in the hidden file.

$cat (filename)

To view the data in the file.

 $cat .(filename)

To view data in the hidden file.

$cat (filename filename)

To view the content from multiple files.

Note : By using cat command we can’t create multiple files and also we are not allowed to write data in middle(we can append from bottom).



By using “touch” command we can create one or more empty files and we can change access time and timestamp of files.

Syntax :-  $touch <options> <filename1> <filename2> ….

Touch Command Options

  • -c, if the file does not exist, do not create it
  • -d, update the access and modification times
  • -m, change the modification time only
  • -r, use the access and modification times of file
  • -t, creates a file using a specified time


$touch t1 t2 t3

Here we are creating three empty files t1,t2,t3

use the -a option as follows. The following command sets the current time and date on a file. If the touchfile file does not exist, it will create the new empty file with the name.

 How to Avoid Creating New File

Using -c option with touch command avoids creating new files. For example the following command will not create a file called touchfile if it does not exists.

How to Change File Modification Time

If you like to change the only modification time of a file called touchfile, then use the -m option with touch command. Please note it will only updates the last modification times (not the access times) of the file.

 Explicitly Set the Access and Modification times

You can explicitly set the time using -c and -t option with touch command. The format would be as follows.

For example the following command sets the access and modification date and time to a file touchfile  as 17:30(17:30 p.m.) December 10 of the current year (2018).

Next verify the access and modification time of file touchfile, with ls -l command.

 How to Use the time stamp of another File

The following touch command with -r option, will update the time-stamp of file t1 with the time-stamp of touchfile file. So, both the file holds the same time stamp.

Create a File using a specified time

If you would like to create a file with specified time other than the current time, then the format should be.

For example the below command touch command with -t option will gives the newtouchfile file a time stamp of 18:30:55 p.m. on December 102012.

Note : Using touch command we can’t add the data.

We’ve almost covered all the options available in the touch command for more options use “man touch“.

3. TEE :-

By using “tee” command we can create multiple files with same data.

$tee <option>  <filename1> <filename2>  <filename3>

By using a option called “-a” we can append the data into existing files

4.   VI EDITOR :

Till now u have seen how to create a file and directory now if suppose i want to add some text in between the records is it possible using cat command i think its restricted to add the data at the end of all the data now whats next what would be the solution for this problem. well the solution is yes its possible using

VI-EDITOR yes the abbreviation is visual editor its similar to word pad in windows but the difference is that its fully based on commands.Now lets get deeper into the explanation of this mind twister tool offered by all the flavors of unix.

As i said we can create a file using vi-editor where it makes lots of difference as for instance while typing the text u have committed a mistake while creation of file using cat command then we cant rectify it so then vi would be the best option.

There will be three modes in vi editor



Explanation about vi editor will continue in the next article ……..


Thank you…..

Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5.00 out of 5)

Add Comment