Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating file / writing in a file using command line interface

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 6.08k
    Comment on it

    In this small blog we will learn how to create a file with different file extensions and how can we write some text or paragraph in the file? how to write content of one file to another file. We can also use cat command to append the binary data.We can use the following commands to create and write in a file :

    1- touch

    2- cat

    3- echo

    4- printf


    Let us check a demo for each above command with the help of an example:

    Example 1: using “touch” command

    Let us create a file having extension “.php” using command “touch”

    Below we are creating a php file named demo (i.e. demo.php)

    username@machinename:~$ touch demo.php  

    This file will be created in our home directory


    Here we will use command "cat" to create a file in a given path.

    Example 2: using “cat >” command

    Let us create a file having extension “.txt” using command “cat >”

    username@machinename:~$ cat > demo2.txt   
      This text will be written in the file demo2.txt
      Hello world

    for returning to the command prompt we will use combination of keys “Ctrl” + “D”


    Let us create a file having extension “.txt” using command “echo > filename”

    Example 3: using “echo > filename” command

    1)  username@machinename:~$ echo  > demo3.txt   
    2)  username@machinename:~$ echo 'First line in the text ' > demo3.txt   
    3)  username@machinename:~$ echo 'Second line in the text' > demo3.txt

    command in 1) will create a file demo3.txt in home directory.

    command in 2) will create a file demo3.txt in home directory, also it will contain the text “First line in the text ”

    command in 3) will over write the existing text in the file demo3.txt


    Let us create a file having extension “.txt” using command “printf > filename”

    Example 4: using “printf > filename” command

    1)  username@machinename:~$ printf  > demo4.txt

    this command will create the file demo4.txt. But on command line interface we will get a message as: printf: usage: printf [-v var] format [arguments]

    to avoid this we can write the command as below:

    2) username@machinename:~$ printf '' > demo4.txt

    Now if we want to write the content of one file to another file we again use the “cat” command as below:

    username@machinename:~$ cat demo3.txt > demo5.txt

    The above command will over write all the text in the demo5.txt from demo3.txt

    Now to check the content of the file demo5.txt we again use the command “cat”

    username@machinename:~$ cat demo5.txt

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: