File Management in Linux: cat, touch, and vi Commands Explained

SaikiranSaikiran
2 min read

File management is a basic skill, whether you are working with primarily Linux as a system administrator, a developer, or future DevOps engineers! In this article, we will go through three essential critical Linux commands – cat, touch, and vi – that will allow you to manage files.

1. touch – Create New Files

This command is used to create new files or change the timestamps of existing ones.

Basic Usage:

Create a new empty file.

Update the modification time of a file.

Create several files at once:

touch filename.txt

To touch a specific timestamp

touch -t YYYYMMDDHHMM.ss filename

To create a several files at once

touch file1 file2 file3
touch filename{1..9}

2. cat – View and Concatenate File Contents

This command reads files consecutively and displays them as standard output.

To view file content

cat filename.txt

create a new file or overwrite an existing one (If the filename.txt exists before, then this command will rewrite the file else, if it’s a not present, this will create a file after editing, you need to press Ctrl + D to save and quit )

cat > filename.txt

This command appends content to the end of an existing file (or creates a new file if one does not already exist) without overwriting existing data.

cat >> filename.txt

This command concatenate the contents of multiple files and write the combined output to a new file

cat file1.txt file2.txt > merged.txt

3. vi – Powerful File Editor

Vi is a popular, screen-based text editor in Unix/Linux environments. It may need some practice, but once learnt, it is quick and powerful.

To open a file (If the file is not present, then this command will create a file)

vi filename.txt

Basic navigation:

  • Use the arrow keys to move the cursor around.

  • h (left), j (down), k (up), l (right) are the keys used to navigate if arrow keys don’t work.

    vi modes

  • Normal Mode: When you start vi you start in Normal Mode, where you can navigate, delete, copy, and paste. Insert Mode: Enter Insert Mode by pressing i to begin editing text. Command Mode: Enter command Mode by pressing :, to save, quit, or anything else you typically perform on a file.

  • Basic Commands: Inserting text: You enter Insert Mode by pressing i, and then you can begin typing text.

  • Exiting vi: :wq will save and quit. :q! will quit and not save. :w will write the file without quitting.

0
Subscribe to my newsletter

Read articles from Saikiran directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saikiran
Saikiran

Hey there, I'm a Linux enthusiast who's super excited about sharing my knowledge. Right now I'm putting together a comprehensive course aimed at helping beginners get a solid grasp of Linux basics. I'm all about making things clear and providing hands-on examples so my mission is to ensure that anyone can dive into linux no matter their tech background Keep an eye out for updates on my course as I guide you through the fascinating world of Linux.