Posts
Get Random Number in Bash
system variable $RANDOM can be used to generate a random number between 0 and 32767, in case you want to get a random number between 0 and 10, you may do the following
$ declare -i number=$RANDOM*10/32767 $ echo $number declare options/typeset: -i integer: following variable will be an integer -r readonly: following variable will be readonly, cannot be reset. -a array: following variable will be an array. -f function -x export: following variable will be exported.
Posts
Double Parentheses
Double-Parentheses permits arithmetic expansion and evaluation. it allowing C-style manipulation of variables; for example, ((var++))
$ a=$((8+8)) $ echo $a #16 $ ((a++)) $ echo $a #17 # we can also use let $ let a++ $ echo $a #18
Posts
Remove Duplicate Files in Bash
Using the following command can remove duplicate files in current directory:
md5 * | sort -k 4 | uniq -f 3 -d | tr -d '()' | cut -d " " -f 2| xargs rm -v md5: generate file checksums sort -k 4: sort output by column 4 uniq -f 3 -d: ignore the first 3 columns and only shows duplicated lines tr -d ‘()’: remove the ‘(’, ‘)’ characters so we can get duplicated file name by cut cut -d " " -f 2: using space as delimiter and get the file name we want to remove xargs rm -v: remove the duplicated files you may need to modify the command based on your md5 standard output.
Posts
Systemd System Services
#testecho.service [Unit] Description=testecho.service Documentation=null [Service] Type=simple User=jxyu ExecStart=/bin/bash -c "java -jar /home/jxyu/datedemo.jar >> /home/jxyu/testecho.log" ExecStop=/bin/kill -2 $MAINPID ExecStop=/bin/bash -c "echo stop >> /home/jxyu/testecho.log" [Install] WantedBy=multiuser.target
Posts
Systemd User Services
user service location: ~/.config/systemd/user/testecho.service # testecho.service [Unit] Description=echotest service Documentation=null [Service] Type=simple ExecStart=/bin/bash -c "/usr/bin/java -jar /home/jxyu/datedemo.jar >> /home/jxyu/test.txt" # following commands can start/stop or check it status # systemctl –user start testecho.service # systemctl –user stop testecho.service # systemctl –user status testecho.service reference: systemd.service
Posts
Gpg2 Command
# alias gpg=gpg2 gpg --verify tomcat_embaded.asc apache-tomcat-9.0.8-embed.zip gpg --keyserver pgpkeys.mit.edu --recv-key 6FB21E8933C60243 gpg --fingerprint 6FB21E8933C60243
Posts
Ffmpeg Command
# copy mp4 video file from time 00:40:21 to 00:41:12 to 40-21_41-12.mp4 ffmpeg -i source.mp4 -ss 00:40:21 -to 00:41:12 -c copy 40-21_41-12.mp4
Posts
Pass Commands
you may use terminal command pass to manager passwords. $ sudo apt intall pass $ gpg2 --full-gen-key $ gpg2 --list-keys # pub rsa2048/**your_key_id** 2018-04-28 [SC] $ pass init **your_key_id** $ pass insert test # enter your password $ pass #see your passwords list $ pass show test
Posts
JDK Commands
jps -l: list all java processes and main class -m: output parameters sent to main method jstat monitor java vm man jstat jmap jstack
Posts
Sudo Command
# edit sudo config file $ sudo visudo # running command as another user $ sudo -u another_user whoami # root shell $ sudo bash config file – what means ALL linuxtechi ALL=(ALL) ALL 1st ALL: allow using sudo from any terminal, any machine 2nd ALL: allow using sudo to run command as other users 3rd ALL: allow using sudo to run any command as root
config file – run sudo without password userA ALL=(ALL) NOPASSWD: ALL