02 January 2012

How to Create File in Linux/Unix

1 comment
Different Methods To Create a File in Linux/Unix :


                                                                                      There are different ways to create a file in Linux/Unix. These are 


1. Using touch command :
                                           
                                          touch command create an empty file of size Zero.


syntax:


            touch file(s)name


Example


a. creating a single file


                                            touch file1
  
$ touch file1
$ ls -l file1
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 18:13 file1


*note that the size of file is zero


b. creating multiple files


                                touch file1 file2 file3


$ touch file1 file2 file3
$ ls -l file*
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 18:14 file1
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 18:14 file2
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 18:14 file3
$



*note that the size of files are zero


2. Using echo or cat command:


                                           touch command create an empty file , if we want to create a file which contain some data then we have to use echo or cat command.


Syntax for echo command :


                                         echo "content of file" > file-name


Example:


                       echo "my name is file1" > file1


$ echo "my name is file " > file1
$ cat file1
my name is file
$ ls -l file1
-rw-r--r-- 1 sandeep sandeep 17 2012-01-08 18:15 file1
$



* note that the size of file is not zero
* you can see the content by writing cat file1 or vi file1
* we cannot create multiple files with echo command , if we tried and write something like this

                           echo "my name is abc" > file1 file2 file3

output:


$ echo "my name is abc" > file1 file2 file3
$ ls -l file*
-rw-r--r-- 1 sandeep sandeep 27 2012-01-08 18:16 file1
$ cat file1
my name is abc file2 file3
$
                            


Syntax for cat command:

                                     cat > file-name[press enter]
write your content
then press ctrl+c or ctrl+d


Example


                      cat > file1
my name is abc
iam a good person
^c
$ cat > file1
my name is abc
iam a good person
^C
$ ls -l file1
-rw-r--r-- 1 sandeep sandeep 33 2012-01-08 18:17 file1
$ cat file1
my name is abc
iam a good person
$



note that the size of file is not zero
* you can see the content by writing cat file1 or vi file1
* we cannot create multiple files with cat command , if we tried and write something like this

                                            cat  > file1 file2 file3

then result is unexpected

$ cat > file3 file4 file5
cat: file4: No such file or directory
cat: file5: No such file or directory
$ ls -l file[3-5]
-rw-r--r-- 1 sandeep sandeep 0 2012-01-08 18:18 file3
$


only file3 is created and the size of the file3 is Zero.


*we can also create file using editors like vi file-name or nano file-name


Example






            




                                            















If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

1 comment: