Learning Objectives
Create files
Use commands like touch and echo to create files and add basic content.
Copy files
Use cp to create another copy of a file without changing the original.
Move and rename
Use mv both for moving files between directories and renaming files.
Delete files
Use rm carefully because deleted files are usually not moved to a recycle bin.
View content
Use cat, less, head, and tail to read file content.
Search inside files
Use grep to find matching words or lines inside text files.
Core Concepts
In Linux, most work is done with files and directories. A directory is like a folder. A file can contain text, logs, configuration, scripts, or application data.
File management means controlling files: creating, reading, copying, moving, renaming, searching, and deleting them.
| Action | Command | Simple meaning |
|---|---|---|
| Create empty file | touch file.txt | Create a new blank file or update timestamp. |
| Add text | echo "text" > file.txt | Write text into a file. |
| Copy | cp source target | Make a duplicate file. |
| Move / Rename | mv old new | Move or rename a file. |
| Delete | rm file.txt | Remove a file permanently. |
| Search | grep "word" file.txt | Find lines containing a word. |
Command Reference
Create a directory
mkdir creates a new directory.
mkdir project
Create an empty file
touch creates a file if it does not exist.
touch app.log
Write text to a file
> writes output into a file. It replaces old content.
echo "Linux training" > app.log
View file content
cat prints the complete file content on screen.
cat app.log
Copy a file
cp copies content from source file to destination file.
cp app.log backup.log
Rename a file
mv can rename a file when source and destination are in the same directory.
mv backup.log old.log
Move a file
mv can also move a file into another directory.
mv app.log project/
Delete a file
rm deletes a file. Use it carefully.
rm old.log
Search inside a file
grep prints lines that match a search word or pattern.
grep "Linux" app.log
Important Notes for Beginners
rm file usually deletes the file permanently from the command line. There may be no recycle bin.
Avoid running rm -rf unless you clearly understand the path and impact.
> overwrites file content.
>> appends new content at the end of the file without removing existing content.
echo "Second line" >> app.log
Hands-on Lab: File Management
Run the following commands in a Linux terminal. The goal is to create a project folder, create a log file, add text, view it, copy it, rename the copy, and delete the renamed file.
Create a project directory
This creates a folder named project.
mkdir project
Create an empty log file
This creates app.log in the current directory.
touch app.log
Write text into the file
This writes Linux training into app.log.
echo "Linux training" > app.log
View file content
This displays the content of app.log.
cat app.log
Copy the file
This creates a second file named backup.log with the same content.
cp app.log backup.log
Rename the copied file
This renames backup.log to old.log.
mv backup.log old.log
Delete the renamed file
This removes old.log.
rm old.log
mkdir project touch app.log echo "Linux training" > app.log cat app.log cp app.log backup.log mv backup.log old.log rm old.log
Practice Checks
What command creates an empty file named notes.txt?
touch notes.txt
Which command copies app.log to backup.log?
cp app.log backup.log
Which command renames backup.log to old.log?
mv backup.log old.log
Which command searches for the word Linux inside app.log?
grep "Linux" app.log
What is the difference between > and >>?
> overwrites file content. >> appends content to the end of the file.
Module Summary
| Need | Use this command | Example |
|---|---|---|
| Create directory | mkdir | mkdir project |
| Create file | touch | touch app.log |
| Add text | echo with > | echo "Linux training" > app.log |
| View file | cat | cat app.log |
| Copy file | cp | cp app.log backup.log |
| Rename file | mv | mv backup.log old.log |
| Delete file | rm | rm old.log |
| Search text | grep | grep "Linux" app.log |