Working with Files

Let’s discuss about how to work with files, how to create files, how to read the content in files, how to delete the file, how to print the text in file, how to copy the file.

Let’s discuss about the command “echo”.

- echo is used to display text on the console.

  • Its primary use is to print text to the console or to redirect output to a file.

For Example: when we write a text with echo command, the same will redirect as output.

Create a file:

  • To create a file from the command prompt, you can use the “echo” command with the “>” operator to redirect the output to a file.

Syntax: echo [text] > [filename]

  • For example, to create a text file named “filename.txt” with the text “hello world” in it, you can use the following command:

echo hello world > filename.txt

  • When you want see the text in the files, you have to use the command “type”.

Example:

Print a file:

  • To print a contents of file in the command prompt, you can use the “type” command followed by the name of the file you want to print.

Syntax: type [filename]

  • For example, to print the contents of a text file named “filename.txt”, you can use the following command:

Delete a file:

  • To delete a file from the command prompt, you can use the “del” command followed by the name of the file you want to delete.

Syntax: del [filename]

  • For example, to delete a file named “filename.txt” in the current directory, you can use the following command:

del filename.txt

  • This command will permanently delete the file “example.txt” from your file system.

NOTE: To check if a file has been deleted in the command prompt, you can use the “dir” command to list the contents of the directory. If the file is no longer present in the directory, it means it has been deleted Or you can verify by opening the file explorer in the GUI.

Copy a file:

  • To copy a file from the command prompt, you can use the “copy” command.

Syntax: copy [Source_filename] [destination_filename]

  1. “source_filename” means the file that you want to copy.
  2. “destination_filename” means the copied file. (TBD)
  • For example, to copy a file named “source_filename” to another file named “destination_filename” in the same directory, you can use the following command:

Questions? : Reach Out