In this chapter, we’ll cover the basics of files and directories in linux. You’ll learn how to create, copy, move, rename, and delete files and directories. These commands are the building blocks of file management in Linux.
Why Learn about Linux Files and Directories in Linux?
ls) to teach beginners how to handle files like a pro.
Creating Files and Directories in Linux
Start your Linux file management journey by creating files and directories.
Creating Files with simple Commands
- touch: Creates an empty file or updates its timestamp.
$ touch example.txt
Output: Createsexample.txt(empty file). - Text Editor (nano): Creates and edits files.
$ nano example.txt
Output: Opens nano; save withCtrl+O, exit withCtrl+X.
Creating Directories
- mkdir: Creates a directory.
$ mkdir my_folder
Output: Createsmy_folder. - Nested Directories: Use
-pfor parent directories.
$ mkdir -p parent/child/grandchild
Output: Creates the full directory structure.
Copying Files and Directories in Linux
The cp command is key for copying in Linux file management.
Copying Files
- Basic Copy:
$ cp example.txt example_copy.txt
Output: Createsexample_copy.txt.
Copying Directories
- Recursive Copy: Use
-rfor directories.
$ cp -r my_folder my_folder_backup
Output: Createsmy_folder_backupwith all contents.
Moving and Renaming Files and Directories
The mv command handles moving and renaming in file manipulation commands.
Moving Files
- Move:
$ mv example.txt /home/user/Documents/
Output: Movesexample.txttoDocuments.
Renaming Files and Directories
- Rename File:
$ mv old_name.txt new_name.txt
Output: Renames tonew_name.txt. - Rename Directory:
$ mv old_folder new_folder
Output: Renames tonew_folder.
Deleting Files and Directories
The rm command deletes files and directories, a critical part of Linux file management.
Deleting Files
- Basic Delete:
$ rm example.txt
Output: Deletesexample.txt. - Force Delete:
$ rm -f example.txt
Output: Deletes without prompting.
Deleting Directories
- Recursive Delete:
$ rm -r my_folder
Output: Deletesmy_folderand contents. - Force Recursive Delete:
$ rm -rf my_folder
Output: Deletes without prompting. Warning: Use cautiously; no recovery!
Using Wildcards for File Manipulation
Wildcards simplify operations on multiple files, enhancing file manipulation commands.
- * (Asterisk): Matches any characters.
$ cp *.txt /home/user/Documents/
Output: Copies all.txtfiles toDocuments. - ? (Question Mark): Matches one character.
$ rm file?.txt
Output: Deletesfile1.txt,file2.txt, etc. - [] (Brackets): Matches one of listed characters.
$ rm file[123].txt
Output: Deletesfile1.txt,file2.txt,file3.txt.
Modern Terminal File Tools
Modern tools, building on Chapter 3’s ranger and nnn, enhance Linux file management. Install on Debian 12:
Note: mmv may not be in stock Debian 12 repositories. Enable contrib or non-free repositories or install from source. See the Debian package page. WE have also covered this aspect in Chapter 2.
Example commands and outputs:
- ranger (visual file manager, see Chapter 3):
$ ranger
Output: Launches a visual interface with file previews. - nnn (lightweight file manager, see Chapter 3):
$ nnn
Output: Opens a fast, text-based file navigator. - rename:
$ rename 's/old/new/' *.txt
Output: Renamesold1.txttonew1.txt, etc. - mmv:
$ mmv "*.txt" "#1.old"
Output: Renamesfile.txttofile.old. - rsync:
$ rsync -av /source/ /destination/
Output:sending incremental file list source/file.txt
Checksum tools verify file integrity:
$ md5sum file.txt
$ sha256sum file.txt
Output: d41d8cd98f00b204e9800998ecf8427e file.txt (md5sum)
Note: Compared to cp (basic file copying, see Chapter 3), rsync offers incremental backups and remote synchronization, ideal for efficient file management.
Glossary of Commands and Tools
Reference: For detailed command documentation, visit Linux Manpages. For package installation, search on Debian APT.
| Command/Tool | Description |
|---|---|
| touch | Creates empty files or updates timestamps. |
| nano | Simple text editor for file creation/editing. |
| mkdir | Creates directories. |
| cp | Copies files or directories. |
| mv | Moves or renames files/directories. |
| rm | Deletes files or directories. |
| ranger | Vim-like file manager with previews. |
| nnn | Lightweight, fast file manager. |
| rename | Batch renames files. |
| mmv | Flexible batch file renamer. |
| rsync | Efficient file transfer and backup tool. |
| md5sum | Computes MD5 checksums for file integrity. |
| sha256sum | Computes SHA-256 checksums for file integrity. |

Practice Linux File Management
Test your skills:
$ mkdir practice: Create a directory.$ touch practice/file1.txt practice/file2.txt: Create two files.$ cp practice/file1.txt practice/file1_backup.txt: Copy a file.$ mkdir backup; mv practice/file2.txt backup/: Move a file.$ rm -r practice: Delete the directory.
Conclusion
You’ve mastered file and directory related commands for in this chapter on files and directories in linux, from creating to deleting files. Practice these skills to organize your system efficiently. Next, we’ll explore permissions and ownership! (See Chapters 2 and 3 for navigation and exploration.)