mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2025-07-05 20:48:48 +02:00
Compare commits
36 Commits
add-licens
...
add-code-o
Author | SHA1 | Date | |
---|---|---|---|
f27a4e1b9f | |||
e24959113a | |||
b6997413d3 | |||
3bab1594ae | |||
055bbc8790 | |||
0f1f62f507 | |||
ef9141b0a3 | |||
554ac88321 | |||
85f342fb20 | |||
931dc30a41 | |||
8f7169db16 | |||
7ce55e3e72 | |||
ec0ff9371c | |||
b1cced62ac | |||
83cd707f93 | |||
d8fed85f58 | |||
834ff1bfbb | |||
2bc18431d9 | |||
f5f7b2bd55 | |||
7910b94f25 | |||
a3f07d0c75 | |||
75d512518e | |||
719fdbb5eb | |||
7d539c3c1d | |||
2350f389ba | |||
8219df4f9b | |||
a31f63e247 | |||
48ad4c86ee | |||
f86d9ebf36 | |||
89290279a4 | |||
c64c6fbf50 | |||
1f64f38cc5 | |||
f19e1eb7fd | |||
8a552221bc | |||
babd99b730 | |||
f4b33d4ed5 |
76
CODE_OF_CONDUCT.md
Normal file
76
CODE_OF_CONDUCT.md
Normal 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
346
README.md
@ -1,5 +1,5 @@
|
|||||||
# Bash-Oneliner
|
# 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.
|
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/)~
|
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)
|
- [Terminal Tricks](#terminal-tricks)
|
||||||
- [Variable](#variable)
|
- [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 + u : cut the line before the cursor; then Ctrl + y paste it
|
||||||
Ctrl + x + Ctrl + e : launch editor define by $EDITOR
|
Ctrl + x + Ctrl + e : launch editor define by $EDITOR
|
||||||
Ctrl + _ : undo typing.
|
Ctrl + _ : undo typing.
|
||||||
|
Ctrl + l : equivalent to clear.
|
||||||
```
|
```
|
||||||
##### Change case
|
##### Change case
|
||||||
```bash
|
```bash
|
||||||
@ -90,6 +91,21 @@ Esc + c
|
|||||||
# run cat filename again
|
# 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
|
##### Some handy environment variables
|
||||||
```
|
```
|
||||||
$0 :name of shell or shell script.
|
$0 :name of shell or shell script.
|
||||||
@ -99,10 +115,19 @@ $? :most recent foreground pipeline exit status.
|
|||||||
$- :current options set for the shell.
|
$- :current options set for the shell.
|
||||||
$$ :pid of the current shell (not subshell).
|
$$ :pid of the current shell (not subshell).
|
||||||
$! :is the PID of the most recent background command.
|
$! :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
|
## Grep
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Type of grep
|
##### Type of grep
|
||||||
```bash
|
```bash
|
||||||
@ -284,7 +309,7 @@ grep -d skip 'bbo' /path/to/files/*
|
|||||||
|
|
||||||
|
|
||||||
## Sed
|
## Sed
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
##### Remove the 1st line
|
##### Remove the 1st line
|
||||||
```bash
|
```bash
|
||||||
sed 1d filename
|
sed 1d filename
|
||||||
@ -491,7 +516,7 @@ sed -r -e 's/^.{3}/&#/' file
|
|||||||
|
|
||||||
|
|
||||||
## Awk
|
## Awk
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Set tab as field separator
|
##### Set tab as field separator
|
||||||
```bash
|
```bash
|
||||||
@ -648,7 +673,7 @@ awk '{$6 = $4 - prev5; prev5 = $5; print;}'
|
|||||||
|
|
||||||
|
|
||||||
## Xargs
|
## Xargs
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Set tab as delimiter (default:space)
|
##### Set tab as delimiter (default:space)
|
||||||
```bash
|
```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
|
# "--" 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
|
##### Count lines in all file, also count total lines
|
||||||
```bash
|
```bash
|
||||||
ls|xargs wc -l
|
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
|
## Find
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
##### List all sub directory/file in the current directory
|
##### List all sub directory/file in the current directory
|
||||||
```bash
|
```bash
|
||||||
find .
|
find .
|
||||||
@ -807,7 +827,7 @@ find . -name "*.mso" -size -74c -delete
|
|||||||
|
|
||||||
|
|
||||||
## Condition and loop
|
## Condition and loop
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### If statement
|
##### If statement
|
||||||
```bash
|
```bash
|
||||||
@ -906,7 +926,7 @@ esac
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Variable
|
## Variable
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
##### Variable substitution within quotes
|
##### Variable substitution within quotes
|
||||||
```bash
|
```bash
|
||||||
# foo=bar
|
# foo=bar
|
||||||
@ -967,8 +987,16 @@ echo ${var,,}
|
|||||||
helloworld
|
helloworld
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
##### Expand and then execute variable/argument
|
||||||
|
```bash
|
||||||
|
cmd="bar=foo"
|
||||||
|
eval "$cmd"
|
||||||
|
echo "$bar" # foo
|
||||||
|
```
|
||||||
|
|
||||||
## Math
|
## Math
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
##### Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)
|
##### Arithmetic Expansion in Bash (Operators: +, -, *, /, %, etc)
|
||||||
```bash
|
```bash
|
||||||
echo $(( 10 + 5 )) #15
|
echo $(( 10 + 5 )) #15
|
||||||
@ -1026,7 +1054,7 @@ echo "var=5;--var"| bc
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Time
|
## Time
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Find out the time require for executing a command
|
##### Find out the time require for executing a command
|
||||||
```bash
|
```bash
|
||||||
@ -1061,7 +1089,7 @@ job 1 at Wed Apr 18 11:16:00 2018
|
|||||||
|
|
||||||
|
|
||||||
## Download
|
## 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)
|
##### Download the content of this README.md (the one your are viewing now)
|
||||||
```bash
|
```bash
|
||||||
@ -1122,9 +1150,20 @@ wget -O filename "http://example.com"
|
|||||||
wget -P /path/to/directory "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
|
## 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
|
##### Random pick 100 lines from a file
|
||||||
```bash
|
```bash
|
||||||
shuf -n 100 filename
|
shuf -n 100 filename
|
||||||
@ -1156,7 +1195,7 @@ echo $(((RANDOM %10)+1))
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Xwindow
|
## 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.
|
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
|
## 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
|
```bash
|
||||||
if [ "$EUID" -ne 0 ]; then
|
if [ "$EUID" -ne 0 ]; then
|
||||||
echo "Please run this as root"
|
echo "Please run this as root"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
##### Snapshot of the current processes
|
||||||
```bash
|
```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
|
##### Show IP address
|
||||||
@ -1241,6 +1344,27 @@ $ip add show
|
|||||||
ifconfig
|
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
|
##### Check system version
|
||||||
```bash
|
```bash
|
||||||
cat /etc/*-release
|
cat /etc/*-release
|
||||||
@ -1251,11 +1375,29 @@ cat /etc/*-release
|
|||||||
man hier
|
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
|
##### List job
|
||||||
```bash
|
```bash
|
||||||
jobs -l
|
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
|
##### Export PATH
|
||||||
```bash
|
```bash
|
||||||
export PATH=$PATH:~/path/you/want
|
export PATH=$PATH:~/path/you/want
|
||||||
@ -1267,8 +1409,11 @@ chmod +x filename
|
|||||||
# you can now ./filename to execute it
|
# you can now ./filename to execute it
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Check system (x86-64)
|
##### Print system information
|
||||||
```bash
|
```bash
|
||||||
|
uname -a
|
||||||
|
|
||||||
|
# Check system hardware-platform (x86-64)
|
||||||
uname -i
|
uname -i
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1297,15 +1442,36 @@ passwd username
|
|||||||
2. alias pd="pwd" //no more need to type that 'w'!
|
2. alias pd="pwd" //no more need to type that 'w'!
|
||||||
3. source ~/.bash_profile
|
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)
|
##### List environment variables (e.g. PATH)
|
||||||
```bash
|
```bash
|
||||||
$echo $PATH
|
echo $PATH
|
||||||
# list of directories separated by a colon
|
# list of directories separated by a colon
|
||||||
```
|
```
|
||||||
##### List all environment variables for current user
|
##### List all environment variables for current user
|
||||||
```bash
|
```bash
|
||||||
$env
|
env
|
||||||
```
|
```
|
||||||
##### Unset environment variable (e.g. unset variable 'MYVAR')
|
##### Unset environment variable (e.g. unset variable 'MYVAR')
|
||||||
```bash
|
```bash
|
||||||
@ -1317,6 +1483,11 @@ unset MYVAR
|
|||||||
lsblk
|
lsblk
|
||||||
```
|
```
|
||||||
|
|
||||||
|
##### Inform the OS of partition table changes
|
||||||
|
```bash
|
||||||
|
partprobe
|
||||||
|
```
|
||||||
|
|
||||||
##### Soft link program to bin
|
##### Soft link program to bin
|
||||||
```bash
|
```bash
|
||||||
ln -s /path/to/program /home/usr/bin
|
ln -s /path/to/program /home/usr/bin
|
||||||
@ -1338,7 +1509,7 @@ rsh node_name
|
|||||||
netstat -tulpn
|
netstat -tulpn
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Find which link to a file
|
##### Print resolved symbolic links or canonical file names
|
||||||
```bash
|
```bash
|
||||||
readlink filename
|
readlink filename
|
||||||
```
|
```
|
||||||
@ -1411,6 +1582,7 @@ init 3
|
|||||||
#or
|
#or
|
||||||
telinit 3
|
telinit 3
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Permanently modify runlevel
|
##### Permanently modify runlevel
|
||||||
```bash
|
```bash
|
||||||
1. edit /etc/init/rc-sysinit.conf
|
1. edit /etc/init/rc-sysinit.conf
|
||||||
@ -1452,8 +1624,19 @@ chown -R user_name /path/to/directory/
|
|||||||
# chown user:group filename
|
# 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
|
##### List current mount detail
|
||||||
```bash
|
```bash
|
||||||
|
mount
|
||||||
|
# or
|
||||||
df
|
df
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1514,12 +1697,14 @@ setquota username 120586240 125829120 0 0 /home
|
|||||||
quota -v username
|
quota -v username
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Fork bomb
|
##### Display current libraries from the cache
|
||||||
# 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.
|
|
||||||
```bash
|
```bash
|
||||||
# :(){:|:&};:
|
ldconfig -p
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Print shared library dependencies (e.g. for 'ls')
|
||||||
|
```bash
|
||||||
|
ldd /bin/ls
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Check user login
|
##### Check user login
|
||||||
@ -1533,20 +1718,11 @@ joe /etc/environment
|
|||||||
# edit this file
|
# 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
|
##### Show and set user limit
|
||||||
```bash
|
```bash
|
||||||
ulimit -u
|
ulimit -u
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Which ports are listening for TCP connections from the network
|
##### Which ports are listening for TCP connections from the network
|
||||||
```bash
|
```bash
|
||||||
nmap -sT -O localhost
|
nmap -sT -O localhost
|
||||||
@ -1827,7 +2003,7 @@ systemctl list-unit-files|grep enabled
|
|||||||
|
|
||||||
|
|
||||||
## Hardware
|
## Hardware
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Collect and summarize all hardware info of your machine
|
##### Collect and summarize all hardware info of your machine
|
||||||
```bash
|
```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)
|
#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
|
```bash
|
||||||
|
lspci
|
||||||
|
# List information about NIC
|
||||||
lspci | egrep -i --color 'network|ethernet'
|
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)
|
##### Controlling IPMI-enabled devices (e.g. BMC)
|
||||||
```bash
|
```bash
|
||||||
# Remotely finding out power status of the server
|
# 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
|
## Networking
|
||||||
[[back to top](#handy-bash-oneliner-commands)]
|
[[back to top](#handy-bash-one-liners)]
|
||||||
|
|
||||||
##### Display IP address
|
##### Display IP address
|
||||||
```bash
|
```bash
|
||||||
@ -1981,7 +2180,24 @@ hostnamectl set-hostname "mynode"
|
|||||||
|
|
||||||
|
|
||||||
## Others
|
## 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
|
##### Get parent directory of current directory
|
||||||
```bash
|
```bash
|
||||||
dirname `pwd`
|
dirname `pwd`
|
||||||
@ -2071,8 +2287,7 @@ zmore filename
|
|||||||
zless filename
|
zless filename
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Run in background, output error file
|
##### Run command in background, output error file
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
some_commands &>log &
|
some_commands &>log &
|
||||||
|
|
||||||
@ -2090,6 +2305,21 @@ some_commands 2>&1 >>outfile
|
|||||||
#0: standard input; 1: standard output; 2: standard error
|
#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
|
##### Send mail
|
||||||
```bash
|
```bash
|
||||||
echo 'heres the content'| mail -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
|
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
|
##### Python simple HTTP Server
|
||||||
```bash
|
```bash
|
||||||
python -m SimpleHTTPServer
|
python -m SimpleHTTPServer
|
||||||
|
# or when using python3:
|
||||||
|
python3 -m http.server
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Read user input
|
##### Read user input
|
||||||
@ -2332,6 +2564,15 @@ basename filename.gz .gz
|
|||||||
|
|
||||||
zcat filename.gz> $(basename filename.gz .gz).unpacked
|
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)
|
##### Add file extension to all file(e.g add .txt)
|
||||||
```bash
|
```bash
|
||||||
rename s/$/.txt/ *
|
rename s/$/.txt/ *
|
||||||
@ -2605,19 +2846,24 @@ script output.txt
|
|||||||
# to logout the screen session (stop saving the contents), type exit.
|
# 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
|
```bash
|
||||||
tree
|
tree
|
||||||
# go to the directory you want to list, and type tree (sudo apt-get install tree)
|
# go to the directory you want to list, and type tree (sudo apt-get install tree)
|
||||||
# output:
|
# output:
|
||||||
# one/
|
# home/
|
||||||
# └── two
|
# └── project
|
||||||
# ├── 1
|
# ├── 1
|
||||||
# ├── 2
|
# ├── 2
|
||||||
# ├── 3
|
# ├── 3
|
||||||
# ├── 4
|
# ├── 4
|
||||||
# └── 5
|
# └── 5
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# set level directories deep (e.g. level 1)
|
||||||
|
tree -L 1
|
||||||
|
# home/
|
||||||
|
# └── project
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Set up virtualenv(sandbox) for python
|
##### Set up virtualenv(sandbox) for python
|
||||||
|
Reference in New Issue
Block a user