xargs Notes

Useful Options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#-n max-args, --max-args=max-args
# Use at most max-args arguments per command line.
find . | xargs -n1 echo

#-I replace-str
find . | xargs -n1 -I% echo % suffix

#-P max-procs, --max-procs=max-procs
# Run up to max-procs processes at a time; the default is 1.
find . | xargs -P10 -n1 -I% sleep 1

#-0, --null
# Input items are terminated by a null character instead of by whitespace.
find . -type f -name "*.java" -print0 |xargs -0 grep -li "keyword"

#-p, --interactive
# Prompt the user about whether to run each command line and read a line from the terminal. Implies -t.
find . | xargs -p -n1 echo

#-t, --verbose
# Print the command line on the standard error output before executing it.

#--delimiter=delim, -d delim
# Input items are terminated by the specified character.

#-a file, --arg-file=file
# Read items from file instead of standard input.

Tricks

Execute Shell Scripts

1
2
3
4
5
6
# The word bash at the end of the line is interpreted by bash
# -c as special parameter $0.
# The single quote ' is critical, otherwise $filename will be
# replaced before the script is executed.
find /path -type f -name '*~' -print0 | xargs -0 bash -c \
'for filename; do cp -a "$filename" ~/backups; done' bash

Prevent xargs from Quitting on Error

1
2
# instead echo the commands and pipe to bash, echo will not return error
find . | xargs -n1 echo command_that_might_exit | bash

Reference

xargs - Wikipedia
prevent xargs from quitting on error

Increase Open File Limit

1
2
3
4
5
6
7
8
9
10
11
12
# Add the following line to /etc/security/limits.conf
# {user}/@{group} soft/hard/-(both) {item} {vaule}
* - nofile 999999

# make sure the file (/etc/pam.d/login for console, /etc/pam.d/lightdm for lightdm and so on) contains the following line
session required pam_limits.so

# login again

# Check
ulimit -Sn # soft limit; can be raised up to the hard limit
ulimit -Hn # hard limit

Reference

Increase the open files limit on Linux

Java Reflection

Import

1
import java.lang.reflect.*;

Get Class, Field and Method Objects

1
2
3
4
Class<?> class = ClassforName("<className>"");
Field field = class.getDeclaredField("<field_name>");
Method method = class.getDeclaredMethod("<methodName>", Class<?>... parameterTypes)
// e.g. method = class.getDeclaredMethod("func", String.class, String.class);

Make private fields or methods accessible

1
2
field.setAccessible(true);
method.setAccessible(true);

Read and write fields

1
2
field.get(Object instance); // return the value(return type: Object)
field.set(Object instance, Object value);

Invoke methods

1
2
method.invoke(Object instance, Object... args); // return Object
// instance is null for static methods

Modify final fields

1. Get the final field

1
2
3
4
// First get the final field we want to modify
Class<?> class = ClassforName("<className>"");
Field the_final_field = class.getDeclaredField("<field_name>");
the_final_field.setAccessible(true);

2. Cancel the final bit

For general JVM

1
2
3
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(the_final_field, the_final_field.getModifiers() & ~Modifier.FINAL);

For Android

The member of Field representing modifiers is different. modifiers -> accessFlags

1
2
3
4
Field modifiersField = Field.class.getDeclaredField("accessFlags");
// the following code is the same
modifiersField.setAccessible(true);
modifiersField.setInt(the_final_field, the_final_field.getModifiers() & ~Modifier.FINAL);

Reference

java.lang.Class
java.lang.reflect.Method
java.lang.reflect.Field
Java Reflection Tutorial