How to Use the Command 'mksquashfs' (with Examples)
- Linux
- December 17, 2024
mksquashfs
is a versatile and powerful command-line tool used in Unix-like operating systems for creating or appending files and directories to a SquashFS filesystem. SquashFS is a compressed, read-only filesystem typically used for live CDs, embedded systems, and other scenarios where storage efficiency is crucial. By using mksquashfs
, users can create a compact and efficient compressed archive of files that can be easily transported and mounted.
Use Case 1: Create or Append Files and Directories to a SquashFS Filesystem
Code:
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs
Motivation: When you need to consolidate multiple directories and files into a single compressed filesystem image, using the mksquashfs
command is a practical approach. This is especially useful for creating backups or preparing files for distribution in a minimal storage footprint.
Explanation:
mksquashfs
: The command initiates the creation of a SquashFS filesystem.path/to/file_or_directory1 path/to/file_or_directory2 ...
: These are the paths to the files and directories you want to include in the SquashFS. You can specify multiple paths.filesystem.squashfs
: This is the output file, the destination SquashFS archive where all specified files and directories will be compressed into.
Example Output:
Running this command will create a SquashFS file named filesystem.squashfs
comprising the contents of the defined directories and files, compressed using gzip by default. You will see terminal output summarizing the compression process and listing the files added.
Use Case 2: Create or Append Files and Directories with a Specific Compression Algorithm
Code:
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -comp gzip|lzo|lz4|xz|zstd|lzma
Motivation: Different compression algorithms offer trade-offs between compression ratio and speed. If your goal is to prioritize one over the other—such as achieving maximum compression or favoring faster decompression—you can specify the desired algorithm.
Explanation:
-comp gzip|lzo|lz4|xz|zstd|lzma
: The-comp
option followed by the chosen algorithm (gzip
,lzo
,lz4
,xz
,zstd
,lzma
) allows users to define which compression method the tool should employ. Each algorithm serves different use-cases based on the user’s need for speed or efficiency.
Example Output:
When executed, this command results in a SquashFS file compressed using the specified algorithm. For example, using xz
might create a smaller file but could require more processing time compared to gzip
.
Use Case 3: Create or Append Files and Directories, Excluding Certain Ones
Code:
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -e file|directory1 file|directory2 ...
Motivation: Sometimes, certain files or directories aren’t necessary in your SquashFS archive, such as temporary files or specific folders that you want to omit from inclusion.
Explanation:
-e file|directory1 file|directory2 ...
: The-e
option lets you specify files or directories to exclude from the SquashFS. This is useful for curating the content of your archive.
Example Output:
The command leads to a SquashFS file that excludes the specified files or directories. The output will confirm the exclusion of the listed items during the creation process.
Use Case 4: Excluding Files Ending with a Specific Extension
Code:
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -wildcards -e "*.gz"
Motivation: Often, files with certain extensions like .gz
are intermediate files or alternate compression formats that you might want to exclude from your SquashFS archive.
Explanation:
-wildcards
: This enables the use of wildcard patterns for matching file names.-e "*.gz"
: Here, the exclusion pattern*.gz
specifies that all files ending in.gz
are not to be included in the archive.
Example Output:
The SquashFS file generated will not include any files with the .gz
extension, showcased in the summary of the operation.
Use Case 5: Excluding Files Matching a Regular Expression
Code:
mksquashfs path/to/file_or_directory1 path/to/file_or_directory2 ... filesystem.squashfs -regex -e "regular_expression"
Motivation: Using regular expressions provides more granular control over the exclusion criteria for files, suiting complex file-naming conventions or patterns.
Explanation:
-regex
: This option indicates the use of regular expressions for exclusions.-e "regular_expression"
: The regular expression defines the pattern of file names to exclude. This allows for a wide range of exclusion possibilities based on naming patterns.
Example Output:
The resulting SquashFS archive will ignore files that match the specified regular expression, a process reported in the detailed output upon completion.
Conclusion:
By utilizing mksquashfs
, users can create efficient and tailored compressed archives that suit various needs, whether for storage efficiency, specific file inclusion, or distribution purposes. Understanding the options available with mksquashfs
empowers users to make decisions that match their project requirements, from excluding unwanted files to choosing the optimal compression algorithm.