In this chapter, we’ll cover the basics of file manipulation. 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.
Files and directories are the backbone of any operating system. Whether you’re organizing your documents, backing up important data, or cleaning up your system, knowing how to manipulate files efficiently is a must. Let’s dive in!
There are several ways to create files in Linux. Here are the most common methods:
Using touch: The touch command creates an empty file or updates the timestamp of an existing file. $ touch example.txt
This creates an empty file named example.txt.
Using a Text Editor: You can use a text editor like nano or vi to create and edit files. $ nano example.txt
This opens the nano editor, where you can add content to the file.
To create a directory, use the mkdircommand:
$ mkdir my_folderThis creates a directory named my_folder.
-p option to create parent directories if they don’t exist. $ mkdir -p parent/child/grandchild parent, child, and grandchild directories in one go.The cp command is used to copy files. Here’s how it works:
$ cp source_file destination_fileFor example:
$ cp example.txt example_copy.txtThis creates a copy of example.txtnamed example_copy.txt.
To copy a directory and its contents, use the -r (recursive) option:
$ cp -r source_directory destination_directoryFor example:
$ cp -r my_folder my_folder_backupThis creates a copy of my_folder named my_folder_backup.
The mv command is used to move files or directories from one location to another:
$ mv source destinationFor example:
$ mv example.txt /home/user/Documents/This moves example.txt to the Documents directory.
The mv command can also be used to rename files:
$ mv old_name.txt new_name.txt
This renames old_name.txt to new_name.txt.
The mv command works the same way for directories:
$ mv old_folder new_folderThis renames old_folder to new_folder.
The rm command is used to delete files:
$ rm example.txtThis deletes example.txt.
-f option to force deletion without prompting for confirmation. $ rm -f example.txtTo delete a directory and its contents, use the -r (recursive) option:
$ rm -r my_folderThis deletes my_folder and all its contents.
-r and -f to force deletion of directories. $ rm -rf my_folderWarning: Be very careful with rm -rf. It will delete files and directories permanently without any confirmation.
Wildcards are special characters that allow you to perform operations on multiple files at once. Here are the most common wildcards:
* (Asterisk): Matches any number of characters. $ cp *.txt /home/user/Documents/
This copies all .txt files to the Documentsdirectory.
? (Question Mark): Matches a single character. $ rm file?.txt
This deletes files like file1.txt, file2.txt, etc.
[] (Square Brackets): Matches any one of the characters inside the brackets. $ rm file[123].txt
This deletes file1.txt, file2.txt, and file3.txt.
Modern file management tools like ranger and nnn bring a visual approach to the terminal, offering features such as image previews and batch operations. For batch renaming, tools like rename and mmv simplify the process of renaming multiple files at once.
Rsync is ideal for efficient file transfers and backups, allowing incremental copying and synchronization over ssh. Additionally, verifying file integrity with checksums using md5sum, sha1sum, or sha256sum is crucial for ensuring data remains intact during transfers or archiving.
# Install ranger
$ sudo apt install ranger
# Install nnn
$ sudo apt install nnn
# Install rename
$ sudo apt install rename
# Install mmv
$ sudo apt install mmv
# Install rsync
$ sudo apt install rsync
# Example usage:
$ rename 's/old/new/' *.txt
mmv "*.txt" "#1.old"
$ rsync -av /source/ /destination/
$ md5sum file.txt
$ sha256sum file.txtLet’s put your new skills to the test:
1. Create a new directory called practice.
2. Inside practice, create two files: file1.txt and file2.txt.
3. Copy file1.txt to file1_backup.txt.
4. Move file2.txt to a new directory called backup.
5. Delete the practice directory and its contents.
That’s it for Chapter 4! You’ve now learned how to manipulate files and directories like a pro. In the next chapter, we’ll dive into working with commands—understanding their syntax, options, and arguments. Until then, practice creating, copying, moving, and deleting files to solidify your skills.