<div style="position: relative; padding-bottom: 64.90384615384616%; height: 0;"><iframe src="https://www.loom.com/embed/acb63e31f96a4729ab7235c3711fe4a1?sid=64d92fc9-d041-4c3d-abc7-664effd4f479" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div> ## Overview In this lesson, we'll cover essential file operations in Zsh, focusing on navigating the file system and manipulating files and directories. We'll explore commands like `ls`, `cd`, `cp`, `mkdir`, `rm`, and delve into special Linux directories such as `/dev/null` and `/dev/random`. ### Objectives - Understand how to list, navigate, copy, and remove files and directories using Zsh. - Familiarize with common use cases and flags for each command. - Learn about special Linux directories and their purposes. ## Listing Files and Directories: `ls` The `ls` command lists files and directories in the current directory. It's a versatile command with several options to customize the output. ### Common Usage Patterns 1. **Basic Listing** ```zsh ls ``` 2. **Detailed Listing** ```zsh ls -l ``` This option displays detailed information, including permissions, number of links, owner, group, size, and modification date. 3. **All Files Including Hidden** ```zsh ls -a ``` Hidden files (those starting with a dot) are included in the listing. 4. **Human-Readable Sizes** ```zsh ls -lh ``` Combines the detailed listing with human-readable file sizes. 5. **Recursive Listing** ```zsh ls -R ``` Lists all files and directories recursively. ### Example ```zsh ls -lahR /path/to/directory ``` This command lists all files, including hidden ones, with detailed information and human-readable sizes, recursively for the specified directory. ## Changing Directories: `cd` The `cd` command changes the current directory. ### Common Usage Patterns 1. **Change to a Specific Directory** ```zsh cd /path/to/directory ``` 2. **Go to Home Directory** ```zsh cd ``` This command without arguments takes you to your home directory. 3. **Navigate Up One Level** ```zsh cd .. ``` 4. **Navigate Using Relative Path** ```zsh cd ../another-directory ``` ### Example ```zsh cd ~/Documents/Projects ``` This command changes the current directory to `Projects` within the `Documents` directory in your home directory. ## Creating Directories: `mkdir` The `mkdir` command creates new directories. ### Common Usage Patterns 1. **Basic Directory Creation** ```zsh mkdir new_directory ``` 2. **Create Parent Directories** ```zsh mkdir -p parent/child/new_directory ``` The `-p` option creates parent directories as needed. ### Example ```zsh mkdir -p ~/Projects/2024/NewProject ``` This command creates the `NewProject` directory and any necessary parent directories under `Projects/2024` in your home directory. ## Copying Files and Directories: `cp` The `cp` command copies files and directories. ### Common Usage Patterns 1. **Copy a File** ```zsh cp source_file.txt destination_file.txt ``` 2. **Copy a Directory Recursively** ```zsh cp -r source_directory/ destination_directory/ ``` 3. **Preserve Attributes** ```zsh cp -a source_file.txt destination_file.txt ``` The `-a` option preserves attributes such as timestamps and permissions. ### Example ```zsh cp -r ~/Projects/2024 ~/Backup/Projects ``` This command copies the `2024` directory and its contents from `Projects` to `Backup/Projects`. ## Removing Files and Directories: `rm` The `rm` command removes files and directories. ### Common Usage Patterns 1. **Remove a File** ```zsh rm file_to_remove.txt ``` 2. **Remove a Directory Recursively** ```zsh rm -r directory_to_remove/ ``` 3. **Force Removal** ```zsh rm -rf directory_to_remove/ ``` The `-f` option forces removal without prompting for confirmation. ### Example ```zsh rm -rf ~/Projects/OldProject ``` This command forcefully removes the `OldProject` directory and its contents. ## Special Linux Directories ### `/dev/null` `/dev/null` is a special file that discards all data written to it and returns EOF on read. ### Usage Example ```zsh echo "This will be discarded" > /dev/null ``` ### `/dev/random` and `/dev/urandom` `/dev/random` provides random data and blocks if there's not enough entropy. `/dev/urandom` provides random data without blocking. ### Usage Example ```zsh head -c 16 /dev/random > random_data.bin ``` This command generates 16 bytes of random data and writes them to `random_data.bin`. ## Cheat Sheet - **`ls`**: List files and directories. - `-l`: Detailed listing. - `-a`: Include hidden files. - `-h`: Human-readable sizes. - `-R`: Recursive listing. - **`cd`**: Change directory. - `cd`: Go to home directory. - `cd ..`: Navigate up one level. - **`mkdir`**: Create directories. - `-p`: Create parent directories. - **`cp`**: Copy files and directories. - `-r`: Recursive copy. - `-a`: Preserve attributes. - **`rm`**: Remove files and directories. - `-r`: Recursive removal. - `-f`: Force removal. - **Special Directories**: - `/dev/null`: Discards data. - `/dev/random` and `/dev/urandom`: Provides random data. ## Exercise 1. Create a directory structure `~/Test/Files` and navigate into it. 2. Create three files: `file1.txt`, `file2.txt`, and `file3.txt`. 3. List the files in the directory with detailed information. 4. Copy `file1.txt` to `file1_copy.txt`. 5. Create a subdirectory `Subdir` and move `file2.txt` into it. 6. Remove `file3.txt`. 7. Write a string to `/dev/null` and read 10 bytes from `/dev/random`. ### Solution ```zsh mkdir -p ~/Test/Files cd ~/Test/Files touch file1.txt file2.txt file3.txt ls -l cp file1.txt file1_copy.txt mkdir Subdir mv file2.txt Subdir/ rm file3.txt echo "Discard this string" > /dev/null head -c 10 /dev/random > random_data.bin ``` By following these steps and examples, you'll gain a solid understanding of file operations in Zsh and their common use cases.