Chapter 4: Working With Files

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.


Why Learn File Manipulation?

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!


1. Creating Files And Directories

Creating Files

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.

Creating Directories

To create a directory, use the mkdircommand:

$ mkdir my_folder

This creates a directory named my_folder.

  • Creating Nested Directories: Use the -p option to create parent directories if they don’t exist. 
    $ mkdir -p parent/child/grandchild 
    This creates the parent, child, and grandchild directories in one go.

2. Copying Files And Directories

Copying Files

The cp command is used to copy files. Here’s how it works:

$ cp source_file destination_file

For example:

$ cp example.txt example_copy.txt

This creates a copy of example.txtnamed example_copy.txt.

Copying Directories

To copy a directory and its contents, use the -r (recursive) option:

$ cp -r source_directory destination_directory

For example:

$ cp -r my_folder my_folder_backup

This creates a copy of my_folder named my_folder_backup.


3. Moving And Renaming Files And Directories

Moving Files

The mv command is used to move files or directories from one location to another:

$ mv source destination

For example:

$ mv example.txt /home/user/Documents/

This moves example.txt to the Documents directory.

Renaming Files

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.

Moving And Renaming Directories

The mv command works the same way for directories:

$ mv old_folder new_folder

This renames old_folder to new_folder.


4. Deleting Files And Directories

Deleting Files

The rm command is used to delete files:

$ rm example.txt

This deletes example.txt.

  • Force Deletion: Use the -f option to force deletion without prompting for confirmation. 
    $ rm -f example.txt

Deleting Directories

To delete a directory and its contents, use the -r (recursive) option:

$ rm -r my_folder

This deletes my_folder and all its contents.

  • Force Deletion of Directories: Combine -r and -f to force deletion of directories. 
    $ rm -rf my_folder

Warning: Be very careful with rm -rf. It will delete files and directories permanently without any confirmation.


5. Wildcards For File Manipulation

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.


Extra: More File Management Tools

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.txt

Practice Time!

Let’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.


Prev: Chapter 3 | Next: Chapter 5