官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 363字
  • 2021-07-23 19:17:34

How to do it...

Using the data obtained by recursively crawling the Packt Publishing website, we can see that inside of www.packtpub.com the entire website is available. Wow! We also created some test data directories and files.

  1. Next, open up a terminal and create the following script:
#!/bin/bash

# Let's find all the files with the string "Packt"
DIRECTORY="www.packtpub.com/"
SEARCH_TERM="Packt"

# Can we use grep?
grep "${SEARCH_TERM}" ~/* > result1.txt 2&> /dev/null

# Recursive check
grep -r "${SEARCH_TERM}" "${DIRECTORY}" > result2.txt

# What if we want to check for multiple terms?
grep -r -e "${SEARCH_TERM}" -e "Publishing" "${DIRECTORY}" > result3.txt

# What about find?
find "${DIRECTORY}" -type f -print | xargs grep "${SEARCH_TERM}" > result4.txt

# What about find and looking for the string inside of a specific type of content?
find "${DIRECTORY}" -type f -name "*.xml" ! -name "*.css" -print | xargs grep "${SEARCH_TERM}" > result5.txt

# Can this also be achieved with wildcards and subshell?
grep "${SEARCH_TERM}" $(ls -R "${DIRECTORY}"*.{html,txt}) > result6.txt
RES=$?

if [ ${RES} -eq 0 ]; then
echo "We found results!"
else
echo "It broke - it shouldn't happen (Packt is everywhere)!"
fi

# Or for bonus points - a personal favorite
history | grep "ls" # This is really handy to find commands you ran yesterday!

# Aaaannnd the lesson is:
echo "We can do a lot with grep!"
exit 0
Notice in the script the use of ~/* ?. This refers to our home directory and introduces the * wildcard, which allows us to specify anything from that point on. There will be more on the concept of wildcards and regexes later in this chapter.
  1.  If you remain in your home directory (~/) and run the script, the output should be similar to the following:
$ bash search.sh; ls -lah result*.txt
We found results!
We can do a lot with grep!
-rw-rw-r-- 1 rbrash rbrash 0 Nov 14 14:33 result1.txt
-rw-rw-r-- 1 rbrash rbrash 1.2M Nov 14 14:33 result2.txt
-rw-rw-r-- 1 rbrash rbrash 1.2M Nov 14 14:33 result3.txt
-rw-rw-r-- 1 rbrash rbrash 1.2M Nov 14 14:33 result4.txt
-rw-rw-r-- 1 rbrash rbrash 33 Nov 14 14:33 result5.txt
-rw-rw-r-- 1 rbrash rbrash 14K Nov 14 14:33 result6.txt
主站蜘蛛池模板: 古交市| 扎赉特旗| 长泰县| 云浮市| 右玉县| 沙河市| 沙田区| 宕昌县| 高雄县| 新源县| 汕头市| 县级市| 南木林县| 大邑县| 宿松县| 新闻| 安化县| 温州市| 南岸区| 措美县| 繁峙县| 临颍县| 新乐市| 双流县| 东乡族自治县| 常宁市| 甘肃省| 英超| 田林县| 安达市| 定南县| 双辽市| 彩票| 漳浦县| 荔波县| 池州市| 伊川县| 香格里拉县| 平罗县| 驻马店市| 花莲县|