xargs的用法-wiki

本文详细介绍了xargs命令在Unix及类Unix系统中的应用,包括如何解决参数列表过长的问题,以及如何处理包含特殊字符的文件名。

原址:http://en.wikipedia.org/wiki/Xargs


xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. Commands like grep and awk can accept the standard input as a parameter, or argument by using a pipe. However others like cp and echo disregard the standard input stream and rely solely on the arguments found after the command[1][unreliable source?] . Additionally, under the Linux kernel before version 2.6.23, arbitrarily long lists of parameters could not be passed to a command,[2] so xargs breaks the list of arguments into sublists small enough to be acceptable.

For example, commands like:

rm /path/*

or

rm `find /path -type f`

will fail with an error message of "Argument list too long" if there are too many files in /path.

However the version below (functionally equivalent to rm `find /path -type f`) will not fail:

find /path -type f -print0 | xargs -0 rm

In the above example, the find utility feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist.

The previous example is more efficient than this functionally equivalent version which calls rm once for every single file:

find /path -type f -exec rm '{}' \;

Note however that with modern versions of find, the following variant does the same thing as the xargs version:

find /path -type f -exec rm '{}' +

xargs often covers the same functionality as the backquote (`) feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input. It is a good companion for commands that output long lists of files like find, locate and grep, but only if you use -0, since xargs without -0 deals badly with file names containing ', " and space. GNU Parallel is the perfect companion to find, locate and grep if file names may contain ', " and space (newline still requires -0).

The separator problem

Many Unix utilities are line oriented. These may work with xargs as long as the lines do not contain ', " or space. Some of the Unix utilities can use NUL as record separator (e.g. Perl (requires -0 and \0 instead of \n), locate (requires using -0), find (requires using -print0), grep (requires -z or -Z), sort (requires using -z)). Using -0 for xargs deals with the problem, but many Unix utilities cannot use NUL as separator (e.g. head, tail, ls, echo, sed, tar -v, wc, which).

But often people forget this and assume xargs is also line oriented, which is not the case (per default xargs separates on newlines and blanks within lines, substrings with blanks must be single or double-quoted).

The separator problem is illustrated here:

touch important_file
touch 'not important_file'
find . -name not\* | tail -1 | xargs rm
mkdir -p '12" records'
find \! -name . -type d | tail -1 | xargs rmdir

Running the above will cause important_file to be removed but will remove neither the directory called 12" records, nor the file called not important_file.

The proper fix is to use find -print0, but tail (and other tools) do not support NUL-terminated strings:

touch important_file
touch 'not important_file'
find . -name not\* -print0 | xargs -0 rm
mkdir -p '12" records'
find \! -name . -type d -print0 | xargs -0 rmdir

When using the syntax find -print0, entries are separated by a null character instead of a end-of-line. This is equivalent to the more verbose command:

find . -name not\* | tr \\n \\0 | xargs -0 rm

or shorter, by switching xargs to line oriented mode with the -d (delimiter) option:

find . -name not\* | xargs -d '\n' rm

but in general the using the -0 option should be preferred, since newlines in filenames are still a problem.

GNU Parallel is an alternative to xargs that is designed to have the same options, but be line oriented. Thus, using GNU Parallel instead, the above would work as expected.[3]

For Unix environments where xargs does not support the -0 option (e.g. Solaris, AIX), the following can not be used as it does not deal with ' and " (GNU Parallel would work on Solaris, though):

find . -name not\* | sed 's/ /\\ /g' | xargs rm

For Solaris, use these examples to fix file perms:

find . -type d -print | sed -e 's/^/"/' -e 's/$/"/' | xargs chmod 755
find . -type f -print | sed -e 's/^/"/' -e 's/$/"/' | xargs chmod 644

<think>好的,用户想了解如何在Termux中使用termux-notification-list设置通知为已读。首先,我需要回忆Termux的相关API和工具。Termux可以通过Termux:API提供通知管理功能,其中termux-notification-list用于列出通知,而设置已读可能需要其他命令。 接下来,要确认用户是否安装了Termux:API,因为这是必要的前提。如果没有安装,用户可能无法执行相关命令。然后,查阅Termux的官方文档,确认如何标记通知为已读。根据记忆,可能需要使用termux-notification-remove命令来移除通知,从而间接设置为已读。 然后,考虑用户可能的操作步骤:首先列出通知,获取每个通知的ID,然后使用移除命令。需要检查是否存在直接设置已读的方法,或者是否只能通过移除来实现。同时,可能需要编写脚本来自动化处理,比如使用jq解析JSON输出。 还需要注意权限问题,用户需要授予Termux通知访问权限,否则命令无法生效。此外,提醒用户命令的正确语法和参数,避免误操作。可能还需要举例说明,比如具体命令和脚本示例,让用户更清楚如何操作。 最后,总结步骤,确保用户能够按照指导逐步执行,并强调权限和安装依赖的重要性,确保解决方案的可行性和准确性。</think>在 Termux 中,`termux-notification-list` 是用于列出设备通知的 API 命令。若要将通知标记为"已读",需结合 `termux-notification-remove` 命令操作。以下是分步说明: --- ### **1. 前提条件** - 已安装 `Termux:API` 插件(Google Play 或 F-Droid 下载) - 在 Termux 中运行命令授权通知权限: ```bash termux-notification-list ``` 按提示勾选"允许通知访问"。 --- ### **2. 设置通知为已读的原理** Termux 目前没有直接的"标记已读" API,但可通过 **移除通知** 间接实现。Android 系统在通知被移除后,会默认将其状态更新为已读。 --- ### **3. 操作步骤** #### **(1) 列出当前通知** ```bash termux-notification-list ``` 输出为 JSON 格式,例如: ```json [ { "id": 1001, "title": "新消息", "content": "来自用户的提醒" } ] ``` #### **(2) 移除指定通知** 使用 `termux-notification-remove` 并传入通知 ID: ```bash termux-notification-remove 1001 ``` #### **(3) 自动化脚本示例** 批量移除所有通知: ```bash termux-notification-list | jq -r '.[].id' | xargs -n1 termux-notification-remove ``` *需先安装 `jq` 工具:`pkg install jq`* --- ### **4. 注意事项** - **权限问题**:未授权通知权限会返回 `API_ERROR`。 - **临时性操作**:移除通知后,相关 App 可能再次发送新通知。 - **系统限制**:部分 App(如微信)的通知无法通过此方式移除。 --- ### **5. 替代方案** 如果需要更精细的控制,可通过 `termux-job-scheduler` 编写定时任务脚本,定期清理通知。 如有其他问题,建议查阅官方文档:[Termux:API Wiki](https://wiki.termux.com/wiki/Termux:API)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值