Create file in unix using multiple ways | Cat Command with examples | Touch command with examples | Difference between Cat and Touch | Unix Commands to create file | Create file using Vi editor

Create file in unix using multiple ways:

In previous article i have explained the basic unix commands which are useful to the programmers.In this article i will explain the different ways to create a file with real life examples in detail.All data in unix is organized in to files.This article is about to create a file using multiple ways in unix.You can create file in unix using different unix commands.I will explain the Cat command and Touch command with real life examples also.The 100% asked interview question is what is difference between CAT and touch Command in unix?You can create file in unix using Command line prompt or using unix editors.

User can create a file in unix using following ways:Create file in unix

1.Using CAT command

2.Using touch command

3.Using Echo and printf command

4.Using Different text editors-Vi,emac

 

 

1.Cat Command in unix:

User can create a new file using ‘Cat’ command in unix.Using shell prompt directly user can create a file.Using ‘Cat’ command user will able to open a specific file also.If user wants to process the file and append data to the specific file use ‘Cat’ command.

To create new file:

Syntax:

$ Cat >File_Name

Example:

$ Cat >Amit_new.txt

This is my first unix file

Press: CTRL+D

After Completion of your text you need to press Control + d  which is used to save the file and come out of the prompt.The above statement will create a file named ‘Amit_new.txt’.

For Creating file operator  (>) is mandatory.

To Open Created file:

Syntax:

$ Cat <File_Name

or

$ Cat File_Name

Example:

$ Cat <Amit_new.txt

or

$ Cat Amit_new.txt

For opening the file  < operator is not mandatory

To open multiple files you  can use following syntax:

$ cat file1 file2 ……File_N

E.g.- $ cat emp1 emp2 emp3…empn

 It displays emp1 table contents first, followed by emp2, emp3…empn and so on.

To change or append data in file:

Syntax:

$ cat>>File_Name

——-

——-

[Ctrl+d] è[to save data & close file]

Example:

$ cat>>Amit_new.txt

This is first unix file and appending.

CTRL+D

To append the data or to change the data in file we need to use >> Operator.

 

2.touch Command in unix:

touch command is used to create the empty or zero byte file,touch command also used to update the access or modification time of the file.touch command is basically used to create multiple files in linux or unix.using Cat command one can create or update one file at a time but using touch command user can only create multiple zero byte files but can not update multiple files at a time.

Syntax:

To create single zero byte file:

$ touch File_Name

To create multiple zero byte files:

$ touch file1 file2….File_N

Example:

To create single zero byte file:

$ touch Yodhini.txt

To create multiple zero byte files:

$ touch Yodhini.txt,Amit.txt

The above command will create 2 zero byte files named Yodhini and Amit.

Deleting files:-

$ rm: – To delete a file

E.g. $rm File_Name

$ rm –i :- To delete a file with permission/confirmation.

E.g: $ rm –I file_name

$ rm –f :- It deletes a file forcefully

E.g:- $ rm –f File_Name.

 

Deleting multiple files:-

$ rm file1 file2 ……File_N

E.g: rm Amit_Emptyfile

3.Create a file using Echo and printf command:

Using Echo as well as printf command user can create file in unix.

Syntax:

echo ………lines of file.[\n]…. > nameoffile.txt

printf………lines o file………> nameoffile.txt

Example:

echo this is one line file . > Amit.txt

printf this is 2 lines file \n  this is second line > Amit1.txt

\n is used as new line character which will create more than 1 lines of the file.

4.Using Text Editor/Create a file using Vi Editor:

Using Vi editor user can create a file.To create file you can use Vi command to create file.

Syntax:

Vi Filename.txt

Example:

Vi Amit.txt

Use i command to insert the text in the file. After completing your text to leave from the Vi Editor using :

ESC+:+x (press ESC key, type : followed by x and [enter] key).

Question may ask in interview: What does (.) And (..) means?

Answer:

The current directory (.)

In UNIX, (.) means the current directory, so typing cd .

NOTE: there is a space between cd and the dot

Means stay where you are (the unixstuff directory).

This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing.

The parent directory (..)

(..) Means the parent of the current directory, so typing cd .. Will take you one directory up the hierarchy (back to your home directory).

Note: Typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system. And, cd / takes directly to Root Directory.

These are four different ways to create a file in unix server.If you like this article  dont forget to comment here.

 

 

 

 

 

 

 

About admin 138 Articles
Oracle Consultant with Vast Experience in Oracle BI and SQL PLSQL Development

4 Comments

  1. Hello sir Thank you for the such a useful information but I have doubt that we can also create a file using open and create system call that is you are not mention here…(you have mentioned that we can create using editors that’s why I’m asking for this)

12 Trackbacks / Pingbacks

  1. Working with Unix Directory | Create Directory in Unix | Mkdir Command with Example | PWD command with example | Rmdir command to remove directory | Cd command to change directory
  2. Unix File Commands with Examples | WC command with Example | CP Command with Example | CMP Command with Example | LS command with Example | RM command with Example
  3. Grep Command in unix | Grep Command with Examples | Grep Command with Options | Search String in Unix File | Practical Examples of Grep command | fgrep command with example | egrep command with example
  4. SQL Functions List,Functions in SQL,SQL Functions example,Aggregate functions,scalar functions,Aggregate functions in sql,scalar functions in sql,functions
  5. Chmod command in unix | Unix File Permissions | chmod with examples | Chwn command | Chgrp command | UNMASK
  6. vi Editor | vi Editor with its options | how to use vi Editor | Step by Step Learning of vi Editor | ftp Commands
  7. Table partition | Table Partitioning in Oracle | Partitioning in SQL | How Partitioning helps improving Performance | Table Partitioning with Examples | Partitioning on existing table
  8. Database Normalization Examples | Normalization in Database with Examples | Database Normalization with Real Life Examples | Designing Normalized Database
  9. Pivot in SQL with Example | Pivot Clause | Pivot and unpivot in SQL | Pivot in Oracle | SQL Pivot with Real Life Example
  10. Difference between Truncate and Delete, Drop in SQL | Truncate Delete and Drop Commands in SQL | SQL Difference between Truncate,Delete and Drop Commands | Real Life Examples of Truncate Delete and Drop Command
  11. Interview Questions for HCL | SQL Interview Questions for HCL | Interview Questions Asked in HCL | HCL Interview Questions | 20 Most Important SQL Interview Questions for HCL
  12. UNIX Communication Commands | Ping Command | Ping Command Examples

Leave a Reply

Your email address will not be published.


*