Learn Version Control with Git
)}

Managing files

In this article, we will be using commands that directly interact with files such as creating, writing, and appending to a file.

Let's start back in our root directory:

cd

and Navigate to our test folder:

cd Desktop/test
  • We're going to create a new file in our Desktop Folder:
touch test-file.txt

the file should be created, let's list the contents of the file to make sure:

ls

>> test-file.txt

  • Before we write some text into our file, let's print that text into the terminal:
echo "Hello World"

>> Hello World

  • Now in order to write the following text into the file, we will use the > prefix:
echo "Hello World" > test-file.txt
  • Let's list the contents of the file:
cat test-file.txt

>> Hello World

  • Appending text to our file requires the use of >>:
echo "How are you?" >> test-file.txt
>> Hello World
   How are you

using the single > will overwrite all the content of the file.

  • There are also ways to directly interact with file content using the nano command:
nano test-file.txt

This should open an editor where you can directly modify the contents of the file.

Add the following line below: "I'm good, how about you?"

You can modify the contents of the editor and use the commands below to manage your changes. In order to save your content, use the command ctrl+o, and ctrl+x to exit.

Let's verify that our content was modified:

cat test-file.txt
>> Hello World
   How are you
   I'm good, how about you?
  • Now that we're done modifying our file, let's try to get rid of the file since we won't be using it anymore:
rm test-file.txt

Our file is deleted. Now let's go to the parent directory and delete the folder:

cd ..
rm test

we went back to Desktop and deleted our test folder successfully.

Now let's attempt to delete a folder with a file in it, we'll start off by creating the folder and a file inside:

mkdir another-folder
cd another-folder
touch another-file.txt
cd ..

We just created the file another-folder , navigated inside the folder and created the file another-file.txt, then navigated back to Desktop, Let's attempt to delete the folder:

rm another-folder

>> rm: another-folder: is a directory We get the error above, which forbids us from making the deletion. The rm command only allows us to delete files or empty folders. We can delete non-empty folder by using the -rf prefix:

rm -rf another-folder

This should delete the folder and all the files within it.

If you have a text editor open, some of them might not update the content automatically. You might have to open and close the editor to observe the changes.

Feedback Summary
5.0
3 students
5

100%
4

0%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.