Published on

Basic Terminal Commands: mkdir, touch, and vi

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

The terminal is a vital tool for developers, allowing efficient management of files and directories. Among the foundational commands are mkdir, touch, and vi. These commands let you create directories, generate files, and edit text directly from the terminal.

In this guide, we’ll explore these commands and provide practical examples for getting started.

The mkdir Command: Create Directories

The mkdir command is used to create new directories.

Syntax

mkdir [options] directory_name

Common Usage

  1. Create a Single Directory

    mkdir my_project
    
  2. Create Nested Directories

    mkdir -p my_project/src/components
    
    • -p: Ensures parent directories are created as needed.
  3. Set Permissions During Creation

    mkdir -m 755 my_project
    
    • -m: Sets the permission mode (e.g., 755).

Example

mkdir -p projects/java_app
  • Creates the projects directory and the java_app subdirectory if they do not already exist.

The touch Command: Create Files

The touch command is used to create empty files or update the timestamp of existing files.

Syntax

touch [options] file_name

Common Usage

  1. Create an Empty File

    touch index.html
    
  2. Create Multiple Files

    touch file1.txt file2.txt file3.txt
    
  3. Update Timestamp of a File

    touch existing_file.txt
    
    • Updates the last modified time without changing the content.

Example

touch app.js style.css
  • Creates two files: app.js and style.css.

The vi Command: Edit Files

The vi command is a text editor built into most Unix-based systems. It allows you to view and edit file content directly from the terminal.

Syntax

vi file_name

Common Usage

  1. Open a File

    vi my_file.txt
    
    • Opens the file for editing.
  2. Insert Mode

    • Press i to enter insert mode and start editing the file.
  3. Save Changes and Exit

    • Press Esc to exit insert mode, then type :wq and hit Enter.
    • wq: Write (save) and quit.
  4. Quit Without Saving

    • Press Esc, then type :q! and hit Enter.
  5. Search Within a File

    • Type /keyword and hit Enter to search for keyword in the file.

Example Workflow

vi notes.txt
  • Enter insert mode by pressing i, type your text, and save by typing :wq.

Practical Example

Here’s how these commands can be used together:

  1. Create a Project Directory

    mkdir my_project
    
  2. Create Files in the Directory

    cd my_project
    touch index.html style.css script.js
    
  3. Edit a File

    vi index.html
    
    • Add basic HTML structure, save, and exit.

Conclusion

The mkdir, touch, and vi commands are essential tools for terminal-based file and directory management. By mastering these commands, you can efficiently create and edit your projects without relying on a graphical interface. Practice these commands to enhance your terminal skills and boost productivity!