- Published on
Basic Terminal Commands: mkdir, touch, and vi
- Authors
- Name
- Hieu Cao
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.
mkdir
Command: Create Directories
The The mkdir
command is used to create new directories.
Syntax
mkdir [options] directory_name
Common Usage
Create a Single Directory
mkdir my_project
Create Nested Directories
mkdir -p my_project/src/components
-p
: Ensures parent directories are created as needed.
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 thejava_app
subdirectory if they do not already exist.
touch
Command: Create Files
The The touch
command is used to create empty files or update the timestamp of existing files.
Syntax
touch [options] file_name
Common Usage
Create an Empty File
touch index.html
Create Multiple Files
touch file1.txt file2.txt file3.txt
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
andstyle.css
.
vi
Command: Edit Files
The 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
Open a File
vi my_file.txt
- Opens the file for editing.
Insert Mode
- Press
i
to enter insert mode and start editing the file.
- Press
Save Changes and Exit
- Press
Esc
to exit insert mode, then type:wq
and hitEnter
. wq
: Write (save) and quit.
- Press
Quit Without Saving
- Press
Esc
, then type:q!
and hitEnter
.
- Press
Search Within a File
- Type
/keyword
and hitEnter
to search forkeyword
in the file.
- Type
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:
Create a Project Directory
mkdir my_project
Create Files in the Directory
cd my_project touch index.html style.css script.js
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!