Skip to main content

Command Execution Examples (os.execute)

os.execute depends on the Shell. The Shell in a jailbroken iOS environment may be bash or zsh.
XXTouch TrollStore version cannot use os.execute.
It's recommended to avoid os.execute when possible.

Reboot device

-- os.execute('reboot')
-- Recommended replacement
sys.reboot()

Respring device

-- os.execute('killall -9 SpringBoard;killall -9 backboardd')
-- Recommended replacement
sys.killall(9, 'SpringBoard', 'backboardd')

Rebuild icon cache

-- os.execute('su mobile -c uicache')
-- Recommended replacement
clear.caches()

Common operations wrapper

--[[
Delete file, delete directory, rename file, rename directory, move file, move directory, copy file, copy directory,
create directory, create folder
Above are keywords for searchability in the handbook.
--]]

local function sh_escape(path) -- XXTouch original function; can be used commercially with XXTouch permission
path = string.gsub(path, "([ \\()<>\'\"`#&*;?~$|])", "\\%1")
return path
end

function fdelete(path) -- Delete a file or directory (recursively delete children)
assert(type(path)=="string" and path~="", 'fremove invalid argument')
os.execute('rm -rf '..sh_escape(path))
end

function frename(from, to) -- Rename (move) a file or directory
assert(type(from)=="string" and from~="", 'frename arg #1 invalid')
assert(type(to)=="string" and to~="", 'frename arg #2 invalid')
os.execute('mv -f '..sh_escape(from).." "..sh_escape(to))
end

function fcopy(from, to) -- Copy a file or directory (recursively copy children)
assert(type(from)=="string" and from~="", 'fcopy arg #1 invalid')
assert(type(to)=="string" and to~="", 'fcopy arg #2 invalid')
os.execute('cp -rf '..sh_escape(from).." "..sh_escape(to))
end

function mkdir(path) -- Create a directory (recursively create parents)
assert(type(path)=="string" and path~="", 'mkdir invalid argument')
os.execute('mkdir -p '..sh_escape(path))
end

function openurl(url) -- Open a URL
assert(type(url)=="string" and url~="", 'openurl invalid argument')
os.execute('uiopen '..sh_escape(url))
end

-- Usage examples

-- Delete /var/mobile/1.png
fdelete("/var/mobile/1.png")

-- Rename /var/mobile/2.png to /var/mobile/1.png
frename("/var/mobile/2.png", "/var/mobile/1.png")

-- Move /var/mobile/1.png to /var/mobile/Media/1ferver/res/3.png
frename("/var/mobile/1.png", "/var/mobile/Media/1ferver/res/3.png")

-- Copy /var/mobile/1.png to /var/mobile/Media/1ferver/res/4.png
fcopy("/var/mobile/1.png", "/var/mobile/Media/1ferver/res/4.png")

-- Create directory /var/mobile/1/2/3/4
mkdir("/var/mobile/1/2/3/4")

-- Open www.google.com
openurl("https://www.google.com")