Linux (9): Creating Files & Folders

1. Core Purpose & Concepts

  • Core Purpose: Use specific command-line tools to create new, empty files and directories (folders), thereby starting to modify the file system.
  • Core Terminology:
    • <span>touch</span>: A command used to create empty files or update the timestamp of existing files.
    • <span>mkdir</span> (Make Directory): A command used to create empty directories.
    • Redirection <span>></span>: A method to create files with content. It redirects the output of another command (like <span>echo</span>) to a file, and if the file does not exist, the shell will create it automatically.
    • <span>-p</span> option (parents):<span>mkdir</span> has a powerful option that can create an entire directory path at once, even if the parent directories do not exist.
    • Brace Expansion <span>{}</span>: An advanced feature of the shell that generates a series of strings through a concise pattern, allowing the creation of a large number of regularly named files or directories with a single command.

2. Key Command & Options

Basic Creation Commands

Command Syntax Core Function Key Options
<span>touch</span> <span>touch [filename]</span> Create a new, empty text file. N/A
<span>mkdir</span> <span>mkdir [dirname]</span> Create a new, empty directory. <span>-p</span>: Create the entire path, including non-existent parent directories.

Brace Expansion

This is an advanced technique for batch creation.

  • Discrete Items:<span>command prefix_{item1,item2,item3}_suffix</span>
  • Continuous Sequences:<span>command prefix_{start..end}_suffix</span> (the sequence can be numbers or letters)

Classic Command Examples

Bash

# Example 1: Create an empty file named file1.txt in the current directorytouch file1.txt
# Example 2: Create a directory named my_projectmkdir my_project
# Example 3: Use the -p option to create nested directories at once# This command will succeed even if the 'project' and 'assets' directories do not exist.mkdir -p project/assets/images
# Example 4: Create a file with initial content using redirectionecho "Hello, World!" &gt; hello.txt
# Example 5: Use brace expansion to create three related empty files at once.touch report_{jan,feb,mar}.txt# Result: report_jan.txt, report_feb.txt, report_mar.txt
# Example 6: Use brace expansion and sequences to create 10 numbered directoriesmkdir chapter_{1..10}# Result: chapter_1/, chapter_2/, ..., chapter_10/

3. Practical Use Cases

  • Project Initialization: Quickly set up a standard directory structure for a new project using <span>mkdir</span>, for example, <span>mkdir my_app/{src,dist,tests}</span>.
  • Script Automation: In automation scripts, use <span>mkdir -p /var/log/my_app</span> to ensure the log directory exists before writing log files, avoiding script failures due to non-existent directories.
  • Batch Generation of Placeholder Files: Before starting a writing project, quickly create the framework for all chapter Markdown files using <span>touch chapter_{01..12}.md</span>.
  • Quickly Create Configuration Files: Generate simple configuration files quickly without opening a text editor using <span>echo "setting=value" > config.ini</span>.

4. Common Pitfalls

  • Spaces in Filenames: Executing <span>mkdir my folder</span> will create two separate directories: <span>my</span> and <span>folder</span>. To create a directory with spaces, you must use quotes, such as <span>mkdir "my folder"</span>.
    • Best Practices: Try to avoid using spaces in filenames and directory names, prefer using underscores (<span>_</span>) or hyphens (<span>-</span>), for example, <span>my_folder</span>.
  • Forgetting the <span>-p</span> option: Trying to create nested directories, such as <span>mkdir project/assets</span>, will fail and return an error if the <span>project</span> directory does not exist. This is one of the most common mistakes made by beginners; using <span>mkdir -p</span> will solve this.
  • Case Sensitivity: In Linux, <span>MyFolder</span> and <span>myfolder</span> are two completely different directories. This can confuse users accustomed to Windows (which is case-insensitive).
  • Brace Expansion Syntax Errors: Using spaces inside braces (for example, <span>{a, b, c}</span>) or forgetting commas between items can lead to expansion failures or unexpected results. The correct format is <span>{a,b,c}</span>, with no spaces inside.

Leave a Comment