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?

Linux file management is essential for organizing documents, backing up data, or cleaning up your system. Using file manipulation commands, you can efficiently manage files and directories in the terminal. This guide builds on Chapter 2 (navigation) and Chapter 3 (exploring with 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: Creates example.txt (empty file).
  • Text Editor (nano): Creates and edits files.
    $ nano example.txt
    Output: Opens nano; save with Ctrl+O, exit with Ctrl+X.

Creating Directories

  • mkdir: Creates a directory.
    $ mkdir my_folder
    Output: Creates my_folder.
  • Nested Directories: Use -p for 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: Creates example_copy.txt.

Copying Directories

  • Recursive Copy: Use -r for directories.
    $ cp -r my_folder my_folder_backup
    Output: Creates my_folder_backup with 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: Moves example.txt to Documents.

Renaming Files and Directories

  • Rename File:
    $ mv old_name.txt new_name.txt
    Output: Renames to new_name.txt.
  • Rename Directory:
    $ mv old_folder new_folder
    Output: Renames to new_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: Deletes example.txt.
  • Force Delete:
    $ rm -f example.txt
    Output: Deletes without prompting.

Deleting Directories

  • Recursive Delete:
    $ rm -r my_folder
    Output: Deletes my_folder and 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 .txt files to Documents.
  • ? (Question Mark): Matches one character.
    $ rm file?.txt
    Output: Deletes file1.txt, file2.txt, etc.
  • [] (Brackets): Matches one of listed characters.
    $ rm file[123].txt
    Output: Deletes file1.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: Renames old1.txt to new1.txt, etc.
  • mmv:
    $ mmv "*.txt" "#1.old"
    Output: Renames file.txt to file.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.
Files and Directories in Linux
Files and Directories in Linux

Practice Linux File Management

Test your skills:

  1. $ mkdir practice: Create a directory.
  2. $ touch practice/file1.txt practice/file2.txt: Create two files.
  3. $ cp practice/file1.txt practice/file1_backup.txt: Copy a file.
  4. $ mkdir backup; mv practice/file2.txt backup/: Move a file.
  5. $ 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.)


Previous : Chapter 3 | Next: Chapter 5