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.
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!
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
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
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
- Create the folder structure:
project/ data/ logs/
- Create the files:
README.md
inproject/
.data.csv
inproject/data/
.
- Make a backup of the folder
project/
into the folderbackup/
. - Rename the file
README.md
toREADME_backup.md
. - 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!
GO TO FULL VERSION