CodeGym /Courses /Docker SELF /Creating, Deleting, Copying, and Moving Files: mkdir, rm,...

Creating, Deleting, Copying, and Moving Files: mkdir, rm, touch, cp, mv

Docker SELF
Level 1 , Lesson 6
Available

Creating, Deleting, Copying, and Moving Files: mkdir, rm, touch, cp, mv

1. Creating Folders and Files

Working with files and folders is the foundation of any operating system, including Linux. You'll need to create directories to store data, move files around, destroy unnecessary stuff (and sometimes accidentally the useful stuff, but more on that later), and also copy all this treasure trove wherever you want. Knowing the basic commands for file management will make working in Linux convenient and fast, and will also show your colleagues that you're a real pro.

In Linux, you can easily create folders and files right from the terminal, which is super handy.

mkdir — creating folders (directories)

The mkdir command is used to create new directories (folders).

Syntax:

mkdir [folder_name]

Example:

mkdir my_folder

This command will create the folder my_folder in the current directory.

Creating nested directories

Sometimes you need to create several nested folders at once. For this, use the -p flag:

mkdir -p parent_folder/child_folder/sub_child

Now we have the folders parent_folder, child_folder inside it, and sub_child inside them.

Tip with a joke:

If you forget the -p flag, Linux will remind you that the parent folder doesn’t exist. Just like in life: you can’t create a child without parents!


touch — creating empty files

When you need to quickly create a file, use touch.

Syntax:

touch [file_name]

Example:

touch my_file.txt

The file my_file.txt will instantly appear in the current directory. Magic, right?

Lifehack: creating multiple files at once

touch file1.txt file2.txt file3.txt

Congrats, now you’re multitasking!

Fun Fact:

touch not only creates files, but also updates their timestamps (information about file creation and modification dates), even if they already exist.


2. Deleting Files and Folders

First, we create, and then (sometimes with tears in our eyes) we delete.

rm — deleting files

Deleting files in Linux is done using the rm command.

Syntax:

rm [file_name]

Example:

rm my_file.txt

Deleting multiple files

rm file1.txt file2.txt

No dialogs or warnings. Be careful with this command!

rm -r — deleting folders

To delete folders and their contents, use the -r flag (recursive).

Example:

rm -r my_folder

Special case: protection from accidental actions

If you want Linux to ask for confirmation, add the -i flag:

rm -ri my_folder
Common mistake:

Never run rm -rf / (or rm -rf *) without understanding the consequences. It will delete everything. As they say, Linux trusts you, but later you might regret it.


3. Copying Files and Folders

cp — copying files

Copying files in Linux is as simple as doing it on your desktop.

Syntax:

cp [source] [destination]

Example:

cp my_file.txt backup_file.txt

Now you have two files: my_file.txt and its copy backup_file.txt.

Copying multiple files into a folder

Imagine you want to move three files into a folder called backup:

cp file1.txt file2.txt file3.txt backup/

Recursive copying of folders

To copy directories, use the flag -r:

cp -r my_folder backup_folder

This will create a copy of the entire folder my_folder along with its content in backup_folder.


4. Moving and Renaming

mv — moving files and folders

The mv command moves a file or folder from one place to another.

Syntax:

mv [source] [destination]

Example:

mv my_file.txt /home/user/documents/

Now the file my_file.txt is located in the folder /home/user/documents/.

Renaming using mv

You can use the same command to rename a file:

mv old_name.txt new_name.txt
Fun fact:

There is no separate command for renaming in Linux. We simply move a file with a new name in the same directory.


5. Practical Task

Task

  1. Create the folder structure:
    project/
        data/
        logs/
    
  2. Create the files:
    • README.md in project/.
    • data.csv in project/data/.
  3. Make a backup of the folder project/ into the folder backup/.
  4. Rename the file README.md to README_backup.md.
  5. Delete the folder logs/.

Solution


# 1. Creating folders
mkdir -p project/data
mkdir project/logs

# 2. Creating files
touch project/README.md project/data/data.csv

# 3. Creating a backup
cp -r project/ backup/

# 4. Renaming a file
mv project/README.md project/README_backup.md

# 5. Deleting a folder
rm -r project/logs

6. Breakdown of Common Mistakes

  • Error: mkdir with an existing name. If the folder already exists, the command will throw an error. Use unique names.
  • Error: Deleting essential files. If you accidentally delete an important file without a backup, recovering it will be extremely difficult.
  • Error with invalid paths. Make sure the path you specify exists. For example, cp myfile.txt /nonexistent_path/ will result in an error.

Real-Life Application

Understanding file management in Linux will come in handy for:

  • Developers. Working with project files, backups, and quickly creating directory structures.
  • Administrators. Creating and deleting temporary files, automating work with logs.
  • Students. Organizing study materials conveniently.

Simple example: automatically creating a folder structure for a new project without needing to use the graphical interface.

mkdir -p project/{src,docs,tests}
touch project/README.md

Now you know how to turn the command line into a magic wand for managing files!

1
Task
Docker SELF, level 1, lesson 6
Locked
Creating a folder and a file
Creating a folder and a file
1
Task
Docker SELF, level 1, lesson 6
Locked
Deleting a File and Folder
Deleting a File and Folder
1
Task
Docker SELF, level 1, lesson 6
Locked
Copying and Moving
Copying and Moving
1
Task
Docker SELF, level 1, lesson 6
Locked
Working with directory structure
Working with directory structure
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION