Overview of the Fs Module
The Fs module provides comprehensive file system operation capabilities, including file reading and writing, directory management, path manipulation, and file attribute querying. All functions support home directory expansion (~
) and relative path handling, providing a unified error handling mechanism.
Function Overview
Function Category | Main Functions | Purpose |
---|---|---|
System Directories | dirs |
Get standard system directories |
Directory Operations | ls , glob , tree , mkdir , rmdir |
Directory browsing and management |
File Operations | mv , cp , rm |
Move, copy, and delete files |
Path Operations | canon , join , base_name |
Path handling and parsing |
Status Checks | exists , is_dir , is_file |
File system status queries |
File Reading and Writing | read , write , append , head , tail |
File content operations |
System Directory Functions
dirs
- Get standard system directories
- Parameters: None
- Returns:
Map
- A mapping containing system directory paths - Includes Directories:
home
- User home directoryconfig
- Configuration directorycache
- Cache directorydata
- Data directorypic
- Pictures directorydesk
- Desktop directorydocs
- Documents directorydown
- Downloads directorycurrent
- Current working directory
Directory Operation Functions
ls [path]
- List directory contents
- Parameters:
path
(optional):String
- Directory path, defaults to the current directory
- Returns:
List[Map]
- A list of file information - Supports Options:
-l
- Detailed information-a
- Show hidden files-L
- Follow symbolic links-U
- Unix timestamp-k
- Display size in KB-u
- Show user information-m
- Display permission mode-p
- Show full path
glob <pattern>
- Pattern match files
- Parameters:
pattern
(required):String
- File matching pattern
- Returns:
List[String]
- List of matched file paths
tree [path]
- Get directory tree structure
- Parameters:
path
(optional):String
- Directory path, defaults to the current directory
- Returns:
Map
- Nested directory tree structure
mkdir <path>
- Create a directory
- Parameters:
path
(required):String
- Path of the directory to create
- Functionality: Recursively create directories (similar to
mkdir -p
)
rmdir <path>
- Remove an empty directory
- Parameters:
path
(required):String
- Path of the directory to remove
- Note: Can only remove empty directories
File Operation Functions
mv <source> <destination>
- Move a file or directory
- Parameters:
source
(required):String
- Source pathdestination
(required):String
- Destination path
- Functionality: Supports renaming and moving operations
- Special Handling: If the destination path ends with
/
, the source file will be moved into that directory
cp <source> <destination>
- Copy a file or directory
- Parameters:
source
(required):String
- Source pathdestination
(required):String
- Destination path
- Functionality: Recursively copy directories and files
rm <path>
- Remove a file or directory
- Parameters:
path
(required):String
- Path to remove
- Functionality: Automatically determines whether to delete a file or directory
Path Operation Functions
canon <path>
- Normalize a path
- Parameters:
path
(required):String
- Path to normalize
- Returns:
String
- The normalized absolute path
join <path>...
- Join paths
- Parameters:
path
(variable):String...
- Path components to join
- Returns:
String
- The joined path
dir_name <path>
- Extract directory path
- Parameters:
path
(required):String
- File path
- Returns:
String
- Directory path
base_name [split_ext?] <path>
- Extract file name
- Parameters:
split_ext
(optional):Boolean
- Whether to separate the extensionpath
(required):String
- File path
- Returns:
String
- File name orList
-[base name, extension]
Status Check Functions
exists <path>
- Check if a path exists
- Parameters:
path
(required):String
- Path to check
- Returns:
Boolean
- Returns true if the path exists
is_dir <path>
- Check if it is a directory
- Parameters:
path
(required):String
- Path to check
- Returns:
Boolean
- Returns true if it is a directory
is_file <path>
- Check if it is a file
- Parameters:
path
(required):String
- Path to check
- Returns:
Boolean
- Returns true if it is a file
File Reading and Writing Functions
read <file>
- Read file content
- Parameters:
file
(required):String
- File path
- Returns:
String|Bytes
- File content (automatically detects text or binary) - Functionality: Prioritizes reading as text; if it fails, reads as a byte array
write <file> <content>
- Write to a file
- Parameters:
file
(required):String
- File pathcontent
(required):String|Bytes
- Content to write
- Functionality: Supports writing strings and byte arrays
append <file> <content>
- Append to a file
- Parameters:
file
(required):String
- File pathcontent
(required):String|Bytes
- Content to append
- Functionality: Appends content to the end of the file
head [n] <file>
- Read the first N lines of a file
- Parameters:
n
(optional):Integer
- Number of lines, defaults to 10file
(required):String
- File path
- Returns:
List[String]
- The first N lines of the file
tail [n] <file>
- Read the last N lines of a file
- Parameters:
n
(optional):Integer
- Number of lines, defaults to 10file
(required):String
- File path
- Returns:
List[String]
- The last N lines of the file
Usage Examples
Basic File Operations
# Read a file |
Directory Operations
# List current directory |
File Management
# Copy a file |
Path Operations
# Join paths |
Notes
The Fs module provides complete file system operation capabilities, with all path operations supporting home directory expansion (~
) and relative path handling. The file reading and writing functions can automatically handle text and binary content, offering a flexible file operation interface. Parameter type descriptions indicate that <>
denotes required parameters, []
denotes optional parameters, and ...
denotes variable parameters.