βοΈ Editing Files Tools
Utilities for editing files.
savestateβ
Records a fileβs current state into a variable.
Argument | Description |
---|---|
VAR | Variable to store state |
FILE | File to check |
savestate test /system/build.prop
update_file_string "ro.product.system.model=Just Test" /system/build.prop
savestate test2 /system/build.prop
if [[ "$test" == "$test2" ]]; then
ui_print "build.prop was not edited"
else
ui_print "build.prop changed"
fi
patch_fstabβ
Replaces properties in fstab files.
Argument | Description |
---|---|
LINE | Pattern to find line |
PROP | Property to find |
NEW | New property value |
FILE | Fstab file |
# Replace fileencryption=ice with encryptable in userdata line
patch_fstab userdata "fileencryption" "encryptable" "/vendor/etc/fstab.qcom"
update_fileβ
Updates existing prop lines in a file.
Argument | Description |
---|---|
SRC | File with new props |
DEST | Destination file |
-force | If any of the new props do not exist in the destination file, ensure they are added to the end |
# Only props that already exist in the destination file will be updated
update_file "NewPropLines.txt" "/system/build.prop"
# Ensures to add all the props even if they do not exist in the destination file
update_file -force "NewPropLines.txt" "/system/build.prop"
update_file_stringβ
Updates prop lines directly with strings.
Argument | Description |
---|---|
LINE | Line(s) to update |
FILE | Destination file |
-force | If any of the new props do not exist in the destination file, ensure they are added to the end |
# Only props that already exist in the destination file will be updated
update_file_string "ro.product.system.model=NEW MODEL" /system/build.prop
# Ensures to add all the props even if they do not exist in the destination file
update_file_string -force "ro.product.system.model=NEW MODEL" "ro.product.system.name=NEW NAME" "ro.build.display.id=NEW ID" /system/build.prop
xml_kitβ
Edits XML files by targeting specific sections.
Argument | Description |
---|---|
-open START END | Start and end tags of XML section (multiple allowed) |
-in PATTERN | Pattern to filter section |
-change-value ATTR VAL | Change attribute value (ATTR="VAL" ) |
-change-tag TAG VAL | Change value between tags (<TAG> VAL </TAG> ) |
-change-content OLD NEW | Replace text in section |
-add TEXT | Add text after section |
-add-inside TEXT | Add text inside section |
-after-line LINE TEXT | Add text after line |
-before-line LINE TEXT | Add text before line |
-get-value | Returns the current content of the section without including opening and closing tags. |
-remove | Removes the entire current section of the last -open done |
-print | Print full result instead of editing |
-print-result | Similar to -print but only prints the modified -open sections without merging them into the rest of the structure. |
-no-auto-spaces | Disable auto-spacing |
FILE | XML file to edit |
Test.xml
<users version="1.0">
<user id="1234">
<name>BlassGO</name>
<age>22</age>
</user>
<user id="5678">
<name>Ismael</name>
<age>22</age>
</user>
</users>
# Open section
xml_kit -open 'users' 'users' /sdcard/Test.xml
# Nested sections
# If modifications are not specified only the sections that matches are printed.
xml_kit -open 'users' 'users' -open 'user' 'user' /sdcard/Test.xml
# Filter section
xml_kit -open 'users' 'users' -open 'user' 'user' -in 5678 /sdcard/Test.xml
# Change attribute
xml_kit -open 'users' 'users' -open 'user' 'user' -in 5678 -change-value id "NEW_ID" /sdcard/Test.xml
# Change tag value
xml_kit -open 'users' 'users' -open 'user' 'user' -in 1234 -change-tag name "Just me" /sdcard/Test.xml
# Add text
xml_kit -open 'users' 'users' -open 'user' 'user' -in 1234 -add "Just Test" /sdcard/Test.xml
# Add text inside
# If a filtering is not applied, the changes will be applied to all macthes
xml_kit -open 'users' 'users' -open 'user' 'user' -add-inside "Add me in all users" /sdcard/Test.xml
# After line
xml_kit -open 'users' 'users' -open 'user' 'user' -after-line "BlassGO" "Just Test" /sdcard/Test.xml
# Before line
xml_kit -open 'users' 'users' -open 'user' 'user' -in 5678 -before-line "<age>" "Just Test" /sdcard/Test.xml
# Get the content of a section
user_names=$(xml_kit -open 'users' 'users' -open 'user' 'user' -open 'name' 'name' -get-value /sdcard/Test.xml)
ui_print "The user names are: $user_names"
# Remove section
# The entire user section with id 1234 will be removed
xml_kit -open 'users' 'users' -open 'user' 'user' -in 1234 -remove /sdcard/Test.xml
-in
+ -get-value
case:
Although -in
is very convenient, its use forces to filter absolutely any open section. This also applies to the -get-value
case, so unfortunately the current value extraction method does not allow you to filter sections and retrieve only a value from one of them. Instead, -in
would check if the value to be retrieved itself matches the pattern, which implies already knowing the value or part of it, which doesn't make much practical sense for this unique case.
add_linesβ
Adds lines from multiple files to a destination file.
Argument | Description |
---|---|
-after-line LINE | Add after this line (multiple allowed) |
-before-line LINE | Add before this line (multiple allowed) |
SRC | Source file(s) |
DEST | Destination file |
add_lines "Add.txt" /system/build.prop
add_lines "Add.txt" "Add2.txt" "Add3.txt" /system/build.prop
add_lines -after-line "Some line" "Add.txt" /system/build.prop
add_lines -before-line "Some line" "Add.txt" /system/build.prop
add_lines -after-line "Some line" "Add.txt" -after-line "Some line 2" "Add2.txt" -before-line "Some Line 3" "Add3.txt" /system/build.prop
add_lines_stringβ
Adds lines directly as strings to a file.
Argument | Description |
---|---|
-after-line LINE | Add after this line (multiple allowed) |
-before-line LINE | Add before this line (multiple allowed) |
LINE | Line(s) to add |
DEST | Destination file |
add_lines_string "New line" "New line 2" /system/build.prop
add_lines_string "" /system/build.prop # Empty line
add_lines_string -after-line "Some line" "add this after" -after-line "Some line 2" "add this after" /system/build.prop
add_lines_string -before-line "Some line" "add this before" -before-line "Some line 2" "add this before" /system/build.prop
add_lines_string "New line" " " "New line 2" " " /system/build.prop
replaceβ
Replaces text in a file or directory.
Argument | Description |
---|---|
OLD | Text to replace |
NEW | Replacement text |
PATH | File or directory |
-r | Recursive (whole directory) |
-a | Replace entire line containing text |
-only-first | Just replace the first coincidence (if used -r this applies to each file, not only the first) |
-follow-symlinks | Follow Symlinks especially when used -r |
-bytes | How many bytes read for the check of readability (Default: 32 ) |
-min-bytes | How many bytes should be readable as text to approve readability check (Default: 4 ) |
-no-check | Disable the check of readability |
# All matches will be replaced
replace "HUH" "OLE" /system/build.prop
# Replace entire line containing text
replace -a "HUH" "OLE" /system/build.prop
# Search and replace in all files in a directory
replace -r "HUH" "OLE" /system
# Only replace if the file as a minimum of 50 readable bytes
replace -bytes 100 -min-bytes 50 "HUH" "OLE" /system/whats_this
# Do multiple replacements
replace "HUH" "OLE" "HAH" "YAH" /system/build.prop
Exclusive for text files, so the readability check is important to avoid corrupting binary files or ending the execution abruptly.
removeβ
Removes text from a file or directory.
Argument | Description |
---|---|
TEXT | Text to remove |
PATH | File or directory |
-r | Recursive (whole directory) |
-a | Remove entire line containing text |
-only-first | Just remove the first coincidence (if used -r this applies to each file, not only the first) |
-follow-symlinks | Follow Symlinks especially when used -r |
-bytes | How many bytes read for the check of readability (Default: 32 ) |
-min-bytes | How many bytes should be readable as text to approve readability check (Default: 4 ) |
-no-check | Disable the check of readability |
# All matches will be removed
remove "HUH" /system/build.prop
# Remove entire line containing text
remove -a "HUH" /system/build.prop
# Remove all empty lines
remove -a "" /system/build.prop
# Search and remove in all files in a directory
remove -r "HUH" /system
# Only remove if the file as a minimum of 50 readable bytes
remove -bytes 100 -min-bytes 50 "HUH" /system/whats_this
# Do multiple removals
remove "HUH" "HAH" /system/build.prop
Exclusive for text files, so the readability check is important to avoid corrupting binary files or ending the execution abruptly.
replace_nameβ
Renames files/folders matching a pattern.
Argument | Description |
---|---|
OLD | Pattern to match |
NEW | New name/pattern |
PATH | File or directory |
-r | Recursive (whole directory) |
-f | Files only |
-d | Directories only |
-regex | Use regular expressions |
replace_name ".jpg" ".png" /system/test.jpg
replace_name "old" "new" /system/folder_old
replace_name ".bak" "" /system/test.prop.bak
replace_name -r ".jpg" ".png" /system
replace_name -f -r ".bak" "" /system
replace_name -d -r "_ext" "_new" /system
replace_name -regex -r ".bak$" "" /system