Linux File Management — Creating, Copying, Moving and Deleting Files Safely
Working with files is one of the most basic tasks in Linux administration. However, basic does not mean unimportant.
A Linux administrator constantly creates configuration files, copies files for backups, moves files between directories, renames files and removes files that are no longer needed.
The important part is not only knowing the commands. It is knowing what they will change before executing them.
Creating Files and Directories
To create an empty file, I can use:
touch config.txtI can verify that the file was created with:
ls -lTo create a directory:
mkdir backupAnd then check it:
ls -ld backupI can also create a directory structure at once:
mkdir -p project/config/nginxThe -p option allows me to create the required parent directories if they do not already exist.
Copying Files
The cp command is used to copy files.
For example:
cp config.txt backup/This creates a copy of config.txt inside the backup directory.
I can verify the result:
ls -l backup/I can also copy a file and give the copy a different name:
cp config.txt backup/config.backupThis is useful when I want to create a backup before modifying a configuration file.
For directories, I need to copy their contents recursively:
cp -r project project_backupThe -r option means that directories and their contents are copied recursively.
Moving and Renaming Files
The mv command is used to move files and directories.
For example:
mv config.txt backup/The file is moved from the current directory into backup.
The same command can also be used to rename a file:
mv config.txt config.oldThere is no separate rename command required for this basic operation.
I can verify the change with:
ls -lah
Deleting Files
To remove a file:
rm config.oldAfter deleting it, I should verify that it is no longer present:
ls -lahRemoving directories requires more attention.
An empty directory can be removed with:
rmdir backupIf the directory contains files, rmdir will not remove it.
A directory and its contents can be removed recursively with:
rm -r backupThis is a destructive operation, so I should always verify the path before executing it.
Working Safely
The most important lesson is not the syntax of cp, mv or rm.
It is the workflow.
Before modifying the filesystem, I should know:
Where am I?
What files are here?
What exactly am I changing?
What will the command do?
Can I recover the data if something goes wrong?For example, before removing something, I can inspect the target:
pwd
ls -lahThen execute the operation and verify the result:
rm config.old
ls -lahThis simple habit becomes much more important when working with real servers and important configuration files.
Practical Example
Imagine that I have an Nginx configuration file:
nginx.confBefore modifying it, I can create a backup:
cp nginx.conf nginx.conf.backupNow I have:
nginx.conf
nginx.conf.backupIf I make a mistake while editing the configuration, I still have the original copy available.
This is a simple example of how basic file-management commands can be used as part of a safer administration workflow.
Key Takeaways
The commands covered in this lesson are:
touch → create a file
mkdir → create a directory
cp → copy files/directories
mv → move or rename files/directories
rm → remove files/directories
rmdir → remove empty directories
ls → inspect files and directories
pwd → show the current directoryThe commands themselves are easy to learn.
The important skill is using them carefully:
Inspect → Change → Verify.