Read/Write Files

Creating a File

touch

touch <file>

This will create an empty file if it doesn’t already exist. If the file exists, the command will update the file’s timestamp.

cat

cat > <file>

This will create a file (if it doesn't exist) and allow you to input text directly into it. Press Ctrl+D to save and exit the input mode.

echo

echo <Input> > <file>

This will create a new file or overwrite the content of the specified file with the given input. The file content is replaced completely.

printf

printf <Input> > <file>

Similar to echo, but offers more control over formatting (e.g., new lines, tab spaces, etc.). It will overwrite the content of the file.

Modifying a File

Accessing the Entire File

Step-by-Step Modification

Example

This creates a file called info.txt and writes the formatted text into it. The ensures that each piece of information is on a new line.

This appends additional lines to the existing file without overwriting the previous content.

The file content will be:

Last updated