Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Ten to improve the efficiency of the Linux bash tricks     - Python object-oriented programming (Programming)

- Manually compile Hadoop 2.6.0 under Ubuntu 14.04 (Server)

- Logging information through the web GUI (LogAnalyzer) (Server)

- Install Web-based monitoring tool: Linux-Dash (Server)

- Docker build private warehouse (Server)

- Oracle query start with connect by tree (Database)

- Oracle online redefinition (Database)

- Setting Derby as Linux / Windows running as a service from the start (Server)

- To install Redis under Linux (Database)

- Use Visual Studio 2015 to develop Android program (Programming)

- MySQL 5.6 master-slave replication configuration (Database)

- Construction Spark source and application development environment (Server)

- Linux kernel source tree to establish load module hello (Linux)

- Lua4.0 interpreter documents (Programming)

- Android project using the command to create and install the package (Programming)

- GCC library link order problems (Programming)

- Upgrade to Linux Mint 16 petra Mint 17 Qiana (Linux)

- CentOS / RHEL 6 was repeated prohibited under the SNMP connection log (Server)

- Linux / Centos anti CC attack script (Linux)

- Linux how to view the graphics models notebook (Linux)

  Ten to improve the efficiency of the Linux bash tricks
  Add Date : 2018-11-21      
  I like studying bash environment. In many cases, the use bash programming, some of the problems encountered over and over again. Every time I need to rethink solutions to these problems. Until one day I can not stand, then sit down and write a generic function, into my .bashrc file and deployed to the computer.

I hope to pursue these efforts to maximize the efficiency of the results of the command line can also give other friends like to use bash to bring some help. I have greater expectations of my other friends such behavior can cause interactions - give me suggestions, come up with better bash tricks

Tips 1, the command line to the top of the file add text

Every time I will be looking to re-wording of this command. Here's how to use sed to the top of the file to add a line of approach:

sed -i '1s / ^ / line to insert \ n /' path / to / file / you / want / to / change.txt

Tip 2, use the command line to the configuration file to insert multiple lines of text

This method is very simple, a lot of people know, here is how to use the command line (>>) to insert a multi-line text file. As used herein, the "here document" syntax, it allows you to block text symbols by the paragraph insert a file, usually in line with the EOF (which means "End Of File"):

cat >> path / to / file / to / append-to.txt << "EOF"
export PATH = $ HOME / jdk1.8.0_31 / bin: $ PATH
export JAVA_HOME = $ HOME / jdk1.8.0_31 /
All content will be two "EOF" between being added to the file.

Tip 3, use the command line recursively global search and replace file directory

If you use Eclipse, ItelliJ or other IDE, powerful refactoring capabilities of these tools may make you easily achieve a lot of things. But I guess a lot of time in your development environment is no such integration tools.

How to use the command line to perform a recursive directory search and replace? Do not try to Perl, you can use the find and sed. Thanks for the guidance provided by Stack Overflow:

# OSX version
find. -type f -name '* .txt' -exec sed -i '' s / this / that / g {} +
After use for some time, I wrote a summary function, added to the .bashrc, like this:

function sr {
    find. -type f -exec sed -i '' s / $ 1 / $ 2 / g {} +
You can use it like this:

sr wrong_word correct_word

Tip 4, using the command line vim and open a temporary file in Dropbox

I used to like to use Emacs in the scratch facility functions. Vim also often used to quickly create temporary files. Below these two functions is to use openssl to generate a random string as the file name:

function sc {
  gvim ~ / Dropbox / $ (openssl rand -base64 10 | tr -dc 'a-zA-Z'). txt

function scratch {
  gvim ~ / Dropbox / $ (openssl rand -base64 10 | tr -dc 'a-zA-Z'). txt
In the command line window, type sc or scratch, a new gvim or macvim window will pop up, which will load a random file name of the temporary file.

Tip 5, use the command line to download the file, including a link steering, HTTPS encryption and security situation.

Output terminal to download a page, follow the link steering, ignore the security exception:

curl -Lks
A download link, follow the link steering, ignore the security exception:

curl -OLks
Here with a lot of parameters, you can read this document to learn simple curl them.

Tips 6, Bashmarks

You have not used bashmarks in .bashrc, too? So what are you waiting for? It's really very useful. It can help you keep the history of the operation, jump back to the directory you use most often. Here is my profile in the script, but I think the above link you can provide more tips:

# S bookmarkname - saves the curr dir as bookmarkname
# G bookmarkname - jumps to the that bookmark
# G b [TAB] - tab completion is available
# L - list all bookmarks

# Save current directory to bookmarks
touch ~ / .sdirs
function s {
  cat ~ / .sdirs | grep -v "export DIR_ $ 1 ="> ~ / .sdirs1
  mv ~ / .sdirs1 ~ / .sdirs
  echo "export DIR_ $ 1 = $ PWD" >> ~ / .sdirs

# Jump to bookmark
function g {
  source ~ / .sdirs
  cd $ (eval $ (echo echo $ (echo \ $ DIR_ $ 1)))

# List bookmarks with dirnam
function l {
  source ~ / .sdirs
  env | grep "^ DIR_" | cut -c5- | grep "^ * =."
# List bookmarks without dirname
function _l {
  source ~ / .sdirs
  env | grep "^ DIR_" | cut -c5- | grep "^ * =." | cut -f1 -d "="

# Completion command for g
function _gcomp {
    local curw
    COMPREPLY = ()
    curw = $ {COMP_WORDS [COMP_CWORD]}
    COMPREPLY = ($ (compgen -W ' `_l`' - $ curw))
    return 0

# Bind completion command for g to _gcomp
complete -F _gcomp g

Tip 7, extract a (awk technique I use most often) from formatted output in

I will use it almost every day. Really. Often there will be some output, I only need one second row or third column, the following command will be able to do this:

#Sample Output of git status -s command:

$ Git status -s

M .bashrc
?? .vim / Bundle / extempore /

# Remove status code from git status and just get the file names
$ Git status -s | awk '{print $ 2}'

.vim / bundle / extempore /
Why do not you write a function, we can always use it?

function col {
  awk -v col = $ 1 '{print $ col}'
This makes it very easy to extract column, for example, you do not want the first column? simple:

$ Git status -s | col 2

.vim / bundle / extempore /

8 Tips to ignore the first x words

I was fascinated to xargs, I feel it's like a sharp knife. But sometimes it used the results obtained need to be adjusted, may need to get some values. For example, the image file you want to get rid of some of the information in the following:

function skip {
    n = $ (($ 1 + 1))
    cut -d '' -f $ n-
Here is how to use it:

Use docker images get the following output:
$ Docker images

65a9e3ef7171 3 weeks ago 1.592 GB
7c01ca6c30f2 3 weeks ago 11.1 MB
9518620e6a0e 3 weeks ago 7.426 MB
430707ee7fe8 3 weeks ago 7.426 MB
boot2docker / boot2docker latest 1dbd7ebffe31 3 weeks ago 1.592 GB
spaceghost / tinycore-x86_64 5.4 f47686df00df 7 weeks ago 11.1 MB
durdn / bithub latest df1e39df8dbf 8 weeks ago 100.9 MB
c5e6cf38d985 8 weeks ago 100.9 MB
nginx latest e426f6ef897e 12 weeks ago 100.2 MB
zoobab / tinycore-x64 latest 8cdd417ec611 8 months ago 7.426 MB
scratch latest 511136ea3c5a 20 months ago 0 B
Using the above function, you can get all IDs:
$ Docker images | col 3

Further processing:
docker images | col 3 | xargs

IMAGE 65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a
But in front of the "IMAGE" I also want to get rid of characters:
docker images | col 3 | xargs | skip 1

65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a
Complete write down is this:
docker rmi $ (docker images | col 3 | xargs | skip 1)

9 tips to create your own command packet

In bash, you can easily create your own command component, you can look at what I wrote the following:

function dur {
  case $ 1 in
  clone | cl)
    git clone git@bitbucket.org: nicolapaolucci / $ 2.git
  move | mv)
    git remote add bitbucket git@bitbucket.org: nicolapaolucci / $ (basename $ (pwd)) git.
    git push --all bitbucket
  trackall | tr)
    #track all remote branches of a project
    for remote in $ (git branch -r | grep -v master); do git checkout --track $ remote; done
  key | k)
    #track all remote branches of a project
    ssh $ 2 'mkdir -p .ssh && cat >> .ssh / authorized_keys' <~ / .ssh / id_rsa.pub
  fun | f)
    #list all custom bash functions defined
    typeset -F | col 3 | grep -v _ | xargs | fold -sw 60
  def | d)
    #show definition of function $ 1
    typeset -f $ 2
  help | h | *)
    echo "[dur] dn shell automation tools"
    echo "commands available:"
    echo "[cl] one, [mv | move]"
    echo "[f] fun lists all bash functions defined in .bashrc"
    echo "[def] < fun > lists definition of function defined in .bashrc"
    echo "[k] ey < host > copies ssh key to target host"
    echo "[tr] ackall], [h] elp"
Through the above script, I can ssh key copied to any server - simply type dur key
user @ somehost.
- Use PuTTY key authentication mechanism for remote login Linux (Linux)
- Some security configuration of Linux systems (Linux)
- Calculate CPU utilization (Linux)
- Oracle 11gr2 new APPEND_VALUES tips (Database)
- Intrusion prevention network server security maintenance tips (Linux)
- Analysis RabbitMQ cluster (Server)
- Linux hard drive failure Case Studies (Linux)
- Vim useful plugin: vundle (Linux)
- Source MongoDB 3.2.1 installed on CentOS6.5 (Database)
- Linux source code to install Apache and solutions to common errors (Server)
- Linux, Google Test (GTest) test environment to build step (Linux)
- sudo command scenario analysis (Linux)
- How to install new fonts on Ubuntu 14.04 and 14.10 (Linux)
- Revive Adserver ad server installation on Ubuntu 15.04 / CentOS7 (Server)
- Linux system security reinforcement (Linux)
- Modify MySQL character encoding under Linux (Database)
- Caffe install under Ubuntu 14.04 64bit (Linux)
- Linux Crontab Timing task command Detailed (Linux)
- When Linux Detailed time zone and common function of time (Linux)
- Ubuntu 14.04 Docker installation (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.