Linux: Write a file

Linux: Write a file

Preface

In Linux, you can read and write files. Reading means getting information from a file, while writing means saving and changing data in a file. This feature is essential for many tasks like storing data, configuring settings, and making programs work better. It lets you create, edit, and manage files, making Linux powerful and flexible for various uses.

How to Write a File

cat

In Linux, cat is a command-line utility used to concatenate and display the content of files. The name "cat" stands for "concatenate," which means joining multiple files together. However, its most common use is to view the contents of a single file.

The basic syntax of the cat command is as follows:

cat [options] [file(s)]

Here, [options] are additional flags that modify the behaviour of the cat command, and [file(s)] represents the file or files whose content you want to display.

Without any options, cat simply displays the entire content of a file in the terminal. If you specify multiple files, cat will display the content of each file in the order they are listed.

Example usage:

  1. Display the content of a single file:
cat filename.txt
  1. Display the content of multiple files:
cat file1.txt file2.txt file3.txt
  1. Concatenate multiple files and display the result:
cat file1.txt file2.txt > combined.txt

This command will concatenate the content of file1.txt and file2.txt and save the result in a new file called combined.txt.

While cat is a simple and useful tool for displaying and concatenating file content, it is worth noting that for large files, other commands like less or more may be more appropriate for viewing content one screen at a time.

Example

  1. To create a new file "file.txt" and write "Hello World!!"

    Note: To come out of the editor press "Ctrl+d".

  2. Display the content of a single file.

  3. Display the content of multiple files.

  4. Concatenate multiple files and display the result

touch

In Linux, touch is a command-line utility used to create, update, or modify the timestamp of a file. Its primary purpose is to create an empty file if it doesn't exist or update the last modified timestamp of an existing file.

The basic syntax of the touch command is as follows:


touch [options] filename(s)

Here, [options] are optional flags that can modify the behavior of the touch command, and [filename(s)] represents the name of the file or files you want to create or update.

Commonly used options:

  • -a: Update only the access time (atime) of the file.

  • -m: Update only the modification time (mtime) of the file.

  • -c: Do not create the file if it doesn't exist (suppresses error messages).

  • -d: Use a specific date and time instead of the current time.

  • -r: Use the timestamp of another file (copy the timestamp from another file).

Example usage:

  1. Create an empty file if it doesn't exist:
touch newfile.txt
  1. Update the modification timestamp of a file (useful for marking updates without changing the file's content):
touch -m existingfile.txt
  1. Use a specific date and time to update the timestamp of a file:
touch -d "2023-07-21 14:30:00" myfile.txt
  1. Copy the timestamp from another file to a new file:
touch -r originalfile.txt copyfile.txt

The touch command is handy for creating placeholder files, updating timestamps for various purposes, and managing file metadata in Linux. It's a simple yet powerful tool for file management tasks.

Example

  1. Create an empty file if it doesn't exist

Update the modification timestamp of a file

vi/vim

In Linux, vi (Visual Editor) and vim (Vi IMproved) are text editors that come pre-installed on most Linux distributions. vi is the original text editor developed for Unix systems, while vim is an enhanced and more feature-rich version of vi. Both editors are widely used by developers, system administrators, and Linux users for editing text files, configuration files, code, and more.

vi and vim are modal editors, which means they have different modes for different operations:

  1. Normal mode: This is the default mode when you open a file. In this mode, you can navigate through the file, copy, paste, delete, and perform other operations on text.

  2. Insert mode: In this mode, you can directly edit and insert new text into the file.

  3. Command mode: In this mode, you can issue editor commands, such as saving changes, searching for text, or exiting the editor.

Here are some basic commands for vim:

  • To open a file using vim:
vim filename.txt
  • To switch to insert mode and start editing the file, press i.

  • To save changes and return to normal mode, press Esc followed by :w and then press Enter.

  • To save changes and exit vim, press Esc followed by :wq and then press Enter.

  • To exit vim without saving changes, press Esc followed by :q! and then press Enter.

  • To search for text in the file, press /, type the search term, and press Enter. To navigate to the next occurrence, press n.

  • To delete a line in normal mode, press dd.

  • To copy a line in normal mode, press yy.

  • To paste the copied or deleted content in normal mode, press p.

Example

  1. Creating a new file with vi new.txt

  2. Press Enter and you will go to the editor.

  3. Press "i" to go into insert mode.

    1. Once you are in insert mode you can write anything in the file.

      1. Press "esc" to come out of insert mode and to save and exit press ":wq!"

And your file is saved and exited.

vim has many powerful features and customizations, making it a favourite among experienced Linux users and developers. However, for newcomers or those who prefer a more user-friendly interface, there are also other text editors available on Linux, such as nano and gedit.

Happy Learning..!!!!

Reference

To develop deeper into the world of DevOps I highly recommend following Shubham Londhe on TrainWithShubham and Bhupinder Rajput on Technical Guftgu.