36 Commits

Author SHA1 Message Date
f27a4e1b9f Create CODE_OF_CONDUCT.md 2019-09-09 10:14:03 +08:00
e24959113a add commands in 'Other'
Repeat printing string n times
2019-09-06 18:08:52 +08:00
b6997413d3 update introduction 2019-09-04 16:32:10 +08:00
3bab1594ae add command to Random section
Random generate password (e.g. generate 5 password each of length 13)
2019-08-15 18:38:57 +08:00
055bbc8790 add to "variable" section
eval command to expand and execute variables
2019-07-10 16:40:14 +08:00
0f1f62f507 add 'system' section
1. Eliminate the zombie
2. Show memory usage
2019-07-09 10:49:04 +08:00
ef9141b0a3 add to 'download'
- add 'Follow any redirect until it reaches the final destination'
- remove a command for installing python dependencies.
2019-07-02 15:30:21 +08:00
554ac88321 add to others
Do not echo the trailing newline
2019-07-02 15:09:38 +08:00
85f342fb20 add to 'Others'
Encode strings as Base64 strings
2019-07-02 15:03:47 +08:00
931dc30a41 add some common environment variables 2019-06-11 14:22:34 +08:00
8f7169db16 Merge pull request #11 from dimisjim/origin/patch-1
add clear trick
2019-06-10 09:52:15 +08:00
7ce55e3e72 add clear trick
Signed-off-by: dimisjim <dimitris.moraitidis@gmail.com>
2019-06-07 14:21:39 +03:00
ec0ff9371c Merge pull request #9 from HanEmile/patch-1
added the python3 way to the python server
2019-06-03 14:18:02 +08:00
b1cced62ac added the python3 way to the python server
Signed-off-by: Emile <hanemile@protonmail.com>
2019-06-02 12:34:06 +02:00
83cd707f93 add others
Run multiple commands in background
2019-05-31 18:23:54 +08:00
d8fed85f58 edit description for 'shopt' command 2019-05-17 15:59:33 +08:00
834ff1bfbb fix 'back to top' link 2019-05-16 23:53:13 +08:00
2bc18431d9 fix 'back to top' link 2019-05-16 23:50:25 +08:00
f5f7b2bd55 Add another example for tree
set level directories deep
2019-05-16 15:50:37 +08:00
7910b94f25 Add command for system
Display file status (access, modify and change time) of a file using `stat`
2019-05-16 12:02:20 +08:00
a3f07d0c75 fix typo 2019-05-16 11:52:54 +08:00
75d512518e Add commands for 'system' section
New commands:
lsusb, lsmod, rmmod, modprobe, pstree, dmesg, chkconfig, systemctl, partprobe, mount, idconfig, idd, chsh, unalias, shopt, chroot, nohup, uptime, nice
2019-05-16 11:51:30 +08:00
719fdbb5eb Update README.md 2019-05-15 01:40:01 +08:00
7d539c3c1d Update README.md 2019-05-15 01:39:35 +08:00
2350f389ba Update README.md 2019-05-15 01:39:23 +08:00
8219df4f9b Delete googledcd3095108df68e4.html 2019-05-15 01:26:30 +08:00
a31f63e247 Verification for google search control 2019-05-15 01:25:20 +08:00
48ad4c86ee rename repo 2019-05-15 00:52:38 +08:00
f86d9ebf36 [hot fix] comment out descriptions 2019-05-14 17:27:13 +08:00
89290279a4 Add CPU IO and network monitoring on [system] 2019-05-14 15:22:30 +08:00
c64c6fbf50 add more bash globbing examples 2019-05-14 12:30:18 +08:00
1f64f38cc5 fix typos 2019-05-14 12:22:35 +08:00
f19e1eb7fd edit bash globbing 2019-05-14 12:21:42 +08:00
8a552221bc Merge pull request #8 from acmello/bash-inference
expand documentation for terminal tricks
2019-05-14 12:17:57 +08:00
babd99b730 expand documentation:
* Add session about bash inference

Signed-off-by: Antonio Mello <acmello.ti@gmail.com>
2019-05-13 11:22:59 +02:00
f4b33d4ed5 Merge pull request #7 from onceupon/add-license-1
Create LICENSE
2019-05-12 01:26:35 +08:00
2 changed files with 372 additions and 50 deletions

76
CODE_OF_CONDUCT.md Normal file
View File

@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at nim.1111.ou@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq

346
README.md
View File

@ -1,5 +1,5 @@
# Bash-Oneliner
I am glad that you are here! I started working on bioinformatics a few years ago (recently switched to cloud computing), and was amazed by those single-word bash commands which are much faster than my dull scripts, time saved through learning command-line shortcuts and scripting. Not all the code here is oneliner, but i put effort on making them brief and swift. I am mainly using Ubuntu, RedHat and Linux Mint and CentOS, sorry if the commands don't work on your system.
I am glad that you are here! I was working on bioinformatics a few years ago and was amazed by those single-word bash commands which are much faster than my dull scripts, time saved through learning command-line shortcuts and scripting. Recent years I am working on cloud computing and I keep recording those useful commands here. Not all of them is oneliner, but i put effort on making them brief and swift. I am mainly using Ubuntu, RedHat and Linux Mint and CentOS, sorry if the commands don't work on your system.
This blog will focus on simple bash commands for parsing data and Linux system maintenance that i acquired from work and LPIC exam. I apologize that there are no detailed citation for all the commands, but they are probably from dear Google and Stackoverflow.
@ -8,7 +8,7 @@ If you know other cool commands, please teach me!
Here's a more stylish version of [Bash-Oneliner](http://onceupon.github.io/Bash-Oneliner/)~
## Handy Bash oneliner commands
## Handy Bash one-liners
- [Terminal Tricks](#terminal-tricks)
- [Variable](#variable)
@ -47,6 +47,7 @@ Ctrl + w : cut the word before the cursor; then Ctrl + y paste it
Ctrl + u : cut the line before the cursor; then Ctrl + y paste it
Ctrl + x + Ctrl + e : launch editor define by $EDITOR
Ctrl + _ : undo typing.
Ctrl + l : equivalent to clear.
```
##### Change case
```bash
@ -90,6 +91,21 @@ Esc + c
# run cat filename again
```
##### Bash globbing
```bash
# '*' serves as a "wild card" for filename expansion.
/b?n/?at #/bin/cat
# '?' serves as a single-character "wild card" for filename expansion.
/etc/pa*wd #/etc/passwd
# [] serves to match the character from a range.
ls -l [a-z]* #list all files with alphabet in its filename.
# {} can be used to match filenames with more than one patterns
ls {*.sh,*.py} #list all .sh and .py files
```
##### Some handy environment variables
```
$0 :name of shell or shell script.
@ -99,10 +115,19 @@ $? :most recent foreground pipeline exit status.
$- :current options set for the shell.
$$ :pid of the current shell (not subshell).
$! :is the PID of the most recent background command.
$DESKTOP_SESSION current display manager
$EDITOR preferred text editor.
$LANG current language.
$PATH list of directories to search for executable files (i.e. ready-to-run programs)
$PWD current directory
$SHELL current shell
$USER current username
$HOSTNAME current hostname
```
## Grep
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Type of grep
```bash
@ -284,7 +309,7 @@ grep -d skip 'bbo' /path/to/files/*
## Sed
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Remove the 1st line
```bash
sed 1d filename
@ -491,7 +516,7 @@ sed -r -e 's/^.{3}/&#/' file
## Awk
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Set tab as field separator
```bash
@ -648,7 +673,7 @@ awk '{$6 = $4 - prev5; prev5 = $5; print;}'
## Xargs
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Set tab as delimiter (default:space)
```bash
@ -748,11 +773,6 @@ echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l'
# "--" signals the end of options and display further option processing
```
##### Download dependencies files and install (e.g. requirements.txt)
```bash
cat requirements.txt| xargs -n1 sudo pip install
```
##### Count lines in all file, also count total lines
```bash
ls|xargs wc -l
@ -769,7 +789,7 @@ grep -rl '192.168.1.111' /etc | xargs sed -i 's/192.168.1.111/192.168.2.111/g'
## Find
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### List all sub directory/file in the current directory
```bash
find .
@ -807,7 +827,7 @@ find . -name "*.mso" -size -74c -delete
## Condition and loop
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### If statement
```bash
@ -906,7 +926,7 @@ esac
```
## Variable
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Variable substitution within quotes
```bash
# foo=bar
@ -967,8 +987,16 @@ echo ${var,,}
helloworld
```
##### Expand and then execute variable/argument
```bash
cmd="bar=foo"
eval "$cmd"
echo "$bar" # foo
```
## Math
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)
```bash
echo $(( 10 + 5 )) #15
@ -1026,7 +1054,7 @@ echo "var=5;--var"| bc
```
## Time
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Find out the time require for executing a command
```bash
@ -1061,7 +1089,7 @@ job 1 at Wed Apr 18 11:16:00 2018
## Download
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Download the content of this README.md (the one your are viewing now)
```bash
@ -1122,9 +1150,20 @@ wget -O filename "http://example.com"
wget -P /path/to/directory "http://example.com"
```
##### Instruct curl to follow any redirect until it reaches the final destination:
```bash
curl -L google.com
```
## Random
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Random generate password (e.g. generate 5 password each of length 13)
```bash
sudo apt install pwgen
pwgen 13 5
#sahcahS9dah4a xieXaiJaey7xa UuMeo0ma7eic9 Ahpah9see3zai acerae7Huigh7
```
##### Random pick 100 lines from a file
```bash
shuf -n 100 filename
@ -1156,7 +1195,7 @@ echo $(((RANDOM %10)+1))
```
## Xwindow
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
X11 GUI applications! Here are some GUI tools for you if you get bored by the text-only environment.
@ -1218,19 +1257,83 @@ xcowsay
## System
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
# Check if it's root running
##### Eliminate the zombie
```bash
# A zombie is already dead, so you cannot kill it. You can eliminate the zombie by killing its parent.
# First, find PID of the zombie
ps aux| grep 'Z'
# Next find the PID of zombie's parent
pstree -p -s <zombie_PID>
# Then you can kill its parent and you will notice the zombie is gone.
sudo kill 9 <parent_PID>
```
###### Show memory usage
```bash
free -c 10 -mhs 1
# print 10 times, at 1 second interval
```
##### Display CPU and IO statistics for devices and partitions.
```bash
# refresh every second
iostat -x -t 1
```
##### Display bandwidth usage on an network interface (e.g. enp175s0f0)
```bash
iftop -i enp175s0f0
```
##### Tell how long the system has been running and number of users
```bash
uptime
```
##### Check if it's root running
```bash
if [ "$EUID" -ne 0 ]; then
echo "Please run this as root"
exit 1
fi
```
##### Change shell of a user (e.g. bonnie)
```bash
chsh -s /bin/sh bonnie
# /etc/shells: valid login shells
```
##### Change root / fake root / jail (e.g. change root to newroot)
```bash
chroot /home/newroot /bin/bash
# To exit chroot
exit
```
##### Display file status (size; access, modify and change time, etc) of a file (e.g. filename.txt)
```bash
stat filename.txt
```
##### Snapshot of the current processes
```bash
ps
ps aux
```
##### Display a tree of processes
```bash
pstree
```
##### Find maximum number of processes
```bash
cat /proc/sys/kernel/pid_max
```
##### Print or control the kernel ring buffer
```bash
dmesg
```
##### Show IP address
@ -1241,6 +1344,27 @@ $ip add show
ifconfig
```
##### Print previous and current SysV runlevel
```bash
runlevel
# or
who -r
```
##### Change SysV runlevel (e.g. 5)
```bash
init 5
#or
telinit 5
```
##### Display all available services in all runlevels,
```bash
chkconfig --list
# update-rc.d equivalent to chkconfig in ubuntu
```
##### Check system version
```bash
cat /etc/*-release
@ -1251,11 +1375,29 @@ cat /etc/*-release
man hier
```
##### Control the systemd system and service manager
```bash
# e.g. check the status of cron service
systemctl status cron.service
# e.g. stop cron service
systemctl stop cron.service
```
##### List job
```bash
jobs -l
```
##### Run a program with modified priority (e.g. ./test.sh)
```bash
# nice value is adjustable from -20 (most favorable) to +19
# the nicer the application, the lower the priority
# Default niceness: 10; default priority: 80
nice -10 ./test.sh
```
##### Export PATH
```bash
export PATH=$PATH:~/path/you/want
@ -1267,8 +1409,11 @@ chmod +x filename
# you can now ./filename to execute it
```
##### Check system (x86-64)
##### Print system information
```bash
uname -a
# Check system hardware-platform (x86-64)
uname -i
```
@ -1297,15 +1442,36 @@ passwd username
2. alias pd="pwd" //no more need to type that 'w'!
3. source ~/.bash_profile
```
##### Print all alias
```bash
alias -p
```
##### Unalias (e.g. after alias ls='ls --color=auto')
```bash
unalias ls
```
##### Set and unset shell options
```bash
# print all shell options
shopt
# to unset (or stop) alias
shopt -u expand_aliases
# to set (or start) alias
shopt -s expand_aliases
```
##### List environment variables (e.g. PATH)
```bash
$echo $PATH
echo $PATH
# list of directories separated by a colon
```
##### List all environment variables for current user
```bash
$env
env
```
##### Unset environment variable (e.g. unset variable 'MYVAR')
```bash
@ -1317,6 +1483,11 @@ unset MYVAR
lsblk
```
##### Inform the OS of partition table changes
```bash
partprobe
```
##### Soft link program to bin
```bash
ln -s /path/to/program /home/usr/bin
@ -1338,7 +1509,7 @@ rsh node_name
netstat -tulpn
```
##### Find which link to a file
##### Print resolved symbolic links or canonical file names
```bash
readlink filename
```
@ -1411,6 +1582,7 @@ init 3
#or
telinit 3
```
##### Permanently modify runlevel
```bash
1. edit /etc/init/rc-sysinit.conf
@ -1452,8 +1624,19 @@ chown -R user_name /path/to/directory/
# chown user:group filename
```
##### Mount and unmount
```bash
# e.g. Mount /dev/sdb to /home/test
mount /dev/sdb /home/test
# e.g. Unmount /home/test
umount /home/test
```
##### List current mount detail
```bash
mount
# or
df
```
@ -1514,12 +1697,14 @@ setquota username 120586240 125829120 0 0 /home
quota -v username
```
##### Fork bomb
# Don't try this at home!
# It is a function that calls itself twice every call until you run out of system resources.
# A '# ' is added in front for safety reason, remove it when seriously you are testing it.
##### Display current libraries from the cache
```bash
# :(){:|:&};:
ldconfig -p
```
##### Print shared library dependencies (e.g. for 'ls')
```bash
ldd /bin/ls
```
##### Check user login
@ -1533,20 +1718,11 @@ joe /etc/environment
# edit this file
```
##### Show running processes
```bash
ps aux
```
##### Find maximum number of processes
```bash
cat /proc/sys/kernal/pid_max
```
##### Show and set user limit
```bash
ulimit -u
```
##### Which ports are listening for TCP connections from the network
```bash
nmap -sT -O localhost
@ -1827,7 +2003,7 @@ systemctl list-unit-files|grep enabled
## Hardware
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Collect and summarize all hardware info of your machine
```bash
@ -1899,11 +2075,34 @@ lsblk -io KNAME,TYPE,MODEL,VENDOR,SIZE,ROTA
#where ROTA means rotational device / spinning hard disks (1 if true, 0 if false)
```
##### List information about NIC
##### List all PCI (Peripheral Component Interconnect) devices
```bash
lspci
# List information about NIC
lspci | egrep -i --color 'network|ethernet'
```
##### List all USB devices
```bash
lsusb
```
##### Linux modules
```bash
# Show the status of modules in the Linux Kernel
lsmod
# Add and remove modules from the Linux Kernel
modprobe
# or
# Remove a module
rmmod
# Insert a module
insmod
```
##### Controlling IPMI-enabled devices (e.g. BMC)
```bash
# Remotely finding out power status of the server
@ -1932,7 +2131,7 @@ ipmitool -I bmc lan set 1 defgw ipaddr 192.168.0.1
## Networking
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Display IP address
```bash
@ -1981,7 +2180,24 @@ hostnamectl set-hostname "mynode"
## Others
[[back to top](#handy-bash-oneliner-commands)]
[[back to top](#handy-bash-one-liners)]
##### Repeat printing string n times (e.g. print 'hello world' five times)
```bash
printf 'hello world\n%.0s' {1..5}
```
##### Encode strings as Base64 strings
```bash
echo test|base64
#dGVzdAo=
```
##### Do not echo the trailing newline
```bash
username=`echo -n "bashoneliner"`
```
##### Get parent directory of current directory
```bash
dirname `pwd`
@ -2071,8 +2287,7 @@ zmore filename
zless filename
```
##### Run in background, output error file
##### Run command in background, output error file
```bash
some_commands &>log &
@ -2090,6 +2305,21 @@ some_commands 2>&1 >>outfile
#0: standard input; 1: standard output; 2: standard error
```
##### Run multiple commands in background
```bash
# run sequentially
(sleep 2; sleep 3) &
# run parallelly
sleep 2 & sleep 3 &
```
##### Run process even when logout (immune to hangups, with output to a non-tty)
```bash
# e.g. Run myscript.sh even when log out.
nohup bash myscript.sh
```
##### Send mail
```bash
echo 'heres the content'| mail -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
@ -2239,6 +2469,8 @@ file=${1#*.}
##### Python simple HTTP Server
```bash
python -m SimpleHTTPServer
# or when using python3:
python3 -m http.server
```
##### Read user input
@ -2332,6 +2564,15 @@ basename filename.gz .gz
zcat filename.gz> $(basename filename.gz .gz).unpacked
```
##### Fork bomb
```bash
# Don't try this at home!
# It is a function that calls itself twice every call until you run out of system resources.
# A '# ' is added in front for safety reason, remove it when seriously you are testing it.
# :(){:|:&};:
```
##### Add file extension to all file(e.g add .txt)
```bash
rename s/$/.txt/ *
@ -2605,19 +2846,24 @@ script output.txt
# to logout the screen session (stop saving the contents), type exit.
```
##### list contents of directories in a tree-like format.
##### List contents of directories in a tree-like format.
```bash
tree
# go to the directory you want to list, and type tree (sudo apt-get install tree)
# output:
# one/
# └── two
# home/
# └── project
# ├── 1
# ├── 2
# ├── 3
# ├── 4
# └── 5
#
# set level directories deep (e.g. level 1)
tree -L 1
# home/
# └── project
```
##### Set up virtualenv(sandbox) for python