1. Practical Task: Setting the Stage
Now it's time to practice all this stuff. We'll work with files and directories, create and move them, plus open them for viewing and editing. This lecture will help you connect all the previously learned commands into one smooth workflow. Ready? Let's go!
To start the task, open your terminal. If you're on WSL, just launch your WSL terminal. For Linux or virtual machine users, open a regular terminal window as well.
Make sure you're in your home directory by running this command:
cd ~
To kick things off, create a directory named test. We'll use it as the main workspace.
mkdir test
Now, let's verify that the directory was created using the ls command:
ls
You should see the folder test in the list. If it's there, congrats! You've just set up the foundation.
2. Creating a file in a directory
Let's move to the created directory:
cd test
Now let's create a new file named example.txt in it using the touch command:
touch example.txt
Double-check that the file has appeared using the ls command. You'll see that the test folder now contains the file example.txt.
To check the current directory, type:
pwd
You should see a path ending with /test.
3. Editing File Contents
Let's open the created file in the nano text editor and add some text:
nano example.txt
After opening the editor, type something like the following line:
Hello, world! This is my first text file in Linux!
Now save the file using the Ctrl + O combination (this means "Write Out"). Press Enter to confirm saving. After that, exit the editor using Ctrl + X.
The cat command will help you make sure the text was actually saved in the file:
cat example.txt
The terminal should display the text you just wrote.
4. Copying a File
Let’s make a backup copy of our file. We’ll call it example_backup.txt. To do this, use the cp command:
cp example.txt example_backup.txt
Now you should have two files in your test folder: example.txt and example_backup.txt. To confirm this, run the ls command again.
Want to check if the backup has the same text? Use the command:
cat example_backup.txt
The text output should be identical to the original.
5. Creating a New Directory and Moving a File
Time to put some order in place. Let's create a new directory called backup where we will store all backup copies:
mkdir backup
Let's move the file example_backup.txt into the backup folder using the mv command:
mv example_backup.txt backup/
Now let's make sure the file was actually moved. First, run ls in the current directory. The file example_backup.txt is no longer here. Then check the contents of the backup folder:
ls backup
The backup directory should now contain our file. Awesome!
6. Accidental File Deletion and Recovery
Imagine this: you accidentally deleted a file. No need to panic! First, let's delete the file example.txt (pretend it was accidental):
rm example.txt
Now the file is gone (check with ls). But no worries, we have a backup! Let's copy it back from the backup folder:
cp backup/example_backup.txt example.txt
Let’s verify the recovery by checking the file:
cat example.txt
There you go, your text is safe and sound.
7. Viewing a large file with less
For a change, let's create a bigger file and see how to conveniently view it. The easiest way is to generate it using the seq utility, which outputs a sequence of numbers. Let's create the file bigfile.txt:
seq 1 1000 > bigfile.txt
Now let's open it with less to view the content page by page:
less bigfile.txt
- Use the
↑and↓keys for navigation. - Press
qto exit.
8. The Result of Our Experiment
You did an awesome job! We created directories and files, made changes to their contents, backed up files, moved them to a new directory, and even recovered a mistakenly deleted file. These are all basic but super important Linux skills.
Now, when you face a new task, like managing files on a server or writing scripts for automation, you've got the essential tools. Seriously, doesn't the command line give you that "wow" feeling? Welcome to the world of Linux, where every task feels like an adventure!
GO TO FULL VERSION