In this activity, you will be building off of the CLI navigation commands you learned in the previous activity while adding onto that knowledge by exploring basic CLI (Command-Line Interface) file manipulation commands. You’ll learn how to create files, move, copy, and delete files and directories using commands like touch
, mv
, cp
, and rm
.
By the end of this exercise, you should feel confident using these commands to manage files and directories in a command-line environment.
cli_file_manipulation/
├── example.txt
└── backup/
└── copy_example.txt
- Navigate to the Home Directory
- Create a folder named
cli_file_manipulation
to store all your project files:
mkdir cli_file_manipulation
Explanation:
mkdir cli_file_manipulation
creates a new directory namedcli_file_manipulation
.
- Change your working directory to the newly created
cli_file_manipulation
directory:
cd cli_file_manipulation
Explanation:
cd cli_file_manipulation
changes the current directory tocli_file_manipulation
.
- Open your
cli_file_manipulation
directory in Visual Studio Code by using the following command:
code .
Explanation:
code .
opens the current directory (cli_file_manipulation
) in Visual Studio Code. If VS Code is not installed, this command will not work, and you may need to install it or open the directory manually.
- Create a new text file named
example.txt
using thetouch
command:
touch example.txt
Explanation:
touch example.txt
creates an empty file namedexample.txt
in the current directory.
- Open the
example.txt
file and add the following content:
This is an example file for CLI file manipulation practice.
Explanation:
- Open the file inside of VS Code and manually add the text for demo.
- Copy the
example.txt
file to create a new file namedcopy_example.txt
using thecp
command:
cp example.txt copy_example.txt
Explanation:
cp example.txt copy_example.txt
creates a copy ofexample.txt
and names itcopy_example.txt
.
- Move the
copy_example.txt
file to a new directory namedbackup
:
mkdir backup
mv copy_example.txt backup/
Explanation:
mkdir backup
creates a new directory namedbackup
.mv copy_example.txt backup/
moves the copied file into thebackup
directory.
- List the contents of the
backup
directory to verify that the file was moved successfully:
ls backup/
Explanation:
ls backup/
lists the contents of thebackup
directory, showing thatcopy_example.txt
is now in this directory.
- Delete the original
example.txt
file using therm
command:
rm example.txt
Explanation:
rm example.txt
deletes theexample.txt
file from the current directory.
- Delete the
backup
directory and its contents using therm -rf
command:
rm -rf backup
Explanation:
rm -rf backup
recursively deletes thebackup
directory and all its contents. The-rf
is required to delete directories.
Great job! You’ve successfully used basic CLI commands to manipulate files and directories.
In this activity, you learned how to perform basic file manipulation tasks in the command line using commands like touch
, mv
, cp
, and rm
. These commands are fundamental for managing files and directories efficiently in a terminal environment. Mastering these will improve your workflow and prepare you for more advanced CLI operations in the future.
Remember to use your AI assistant to ask questions and troubleshoot issues.
© All rights reserved to ThriveDX