Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 52.74 KB | None | 0 0
  1. Note for L3 Training
  2.  
  3. The following commands and associated best practices here: echo, cd, pwd, date, type, file, ls, stat, lsattr, du, sort, head, tail, cat, grep, less, vim, chmod, touch, ln, mkdir, rmdir, cp, mv, rm, rmdir, tar, and mysqldump.
  4.  
  5. View Manual: man bash
  6.  
  7. man -k allows you search all manual pages for a keyword string
  8.  
  9. Bash can be invoked as an interactive login shell or as a script. When invoked as an interactive login shell, Bash reads executes the commands from the /etc/profile file, after which, ~/.bash_profile. ~/.bash_login, and ~/.profile are located in that order.
  10.  
  11. When Bash is invoked as script, /bin/bash is called by the kernel as the interpreter directive succeeding the shebang (#!) as such:
  12.  
  13. #!/bin/bash
  14.  
  15. OPTIONS
  16. All of the single-character shell options documented in the description of the set builtin command can be used as options when the shell is invoked. In addition, bash interprets the following options when it is invoked:
  17.  
  18. -c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with
  19. $0.
  20. -i If the -i option is present, the shell is interactive.
  21. -l Make bash act as if it had been invoked as a login shell (see INVOCATION below).
  22. -r If the -r option is present, the shell becomes restricted (see RESTRICTED SHELL below).
  23. -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive
  24. shell.
  25. -D A list of all double-quoted strings preceded by $ is printed on the standard output. These are the strings that are subject to language translation when the current locale is not C or POSIX. This implies the -n
  26. option; no commands will be executed.
  27. [-+]O [shopt_option]
  28. shopt_option is one of the shell options accepted by the shopt builtin (see SHELL BUILTIN COMMANDS below). If shopt_option is present, -O sets the value of that option; +O unsets it. If shopt_option is not sup‐
  29. plied, the names and values of the shell options accepted by shopt are printed on the standard output. If the invocation option is +O, the output is displayed in a format that may be reused as input.
  30. -- A -- signals the end of options and disables further option processing. Any arguments after the -- are treated as filenames and arguments. An argument of - is equivalent to --.
  31.  
  32.  
  33. Commonly used built-ins include echo, help, cd, declare, logout, export, read, unset, type, ulimit, unalias, source, bg, fg, jobs, disown, history, kill, pwd, shopt, trap, unset. We will cover some of these as they become important; however, you are welcome to review each function and test them on your accounts.
  34.  
  35. ========================================================
  36. bang bang (!!)
  37. bang caret (!^)
  38. bang star (!*)
  39. bang dollar (!$)
  40. ========================================================
  41.  
  42. printenv can be used to show the current global variables
  43.  
  44. export is used to set a global variable, whereas the built-in set is used to set a local variable. We can remove a global or local variable using unset.
  45. ========================================================
  46. peachlet@peachlet.info [~]# export FIRST_NAME=William
  47. peachlet@peachlet.info [~]# export LAST_NAME=Lam
  48. peachlet@peachlet.info [~]# echo ${FIRST_NAME} ${LAST_NAME}
  49. William Lam
  50. ========================================================
  51.  
  52. unset is to remove variable
  53. ========================================================
  54. peachlet@peachlet.info [~]# unset FIRST_NAME
  55. peachlet@peachlet.info [~]# unset LAST_NAME
  56. peachlet@peachlet.info [~]# echo ${FIRST_NAME} ${LAST_NAME}
  57. ========================================================
  58.  
  59. SET is for setting up LOCAL variable
  60.  
  61. NO SET is need to set local variable
  62. ========================================================
  63. peachlet@peachlet.info [~]# bash
  64. peachlet@peachlet.info [~]# RELEASE=`rpm --query centos-release`
  65. peachlet@peachlet.info [~]# echo ${RELEASE}
  66. centos-release-6-8.el6.centos.12.3.x86_64
  67. peachlet@peachlet.info [~]# uname
  68. Linux
  69. peachlet@peachlet.info [~]# bash
  70. peachlet@peachlet.info [~]# KERNEL=`uname`
  71. peachlet@peachlet.info [~]# echo ${KERNEL}
  72. Linux
  73. ========================================================
  74.  
  75. To run command set in as variable use ` (backtic)
  76. To print literally use "
  77.  
  78. A safer way instead of backticks is to use $() .
  79.  
  80. RELEASE=$(rpm --query centos-release)
  81.  
  82. What is head?
  83. outputting the result on a file
  84.  
  85. ========================================================
  86. peachlet@peachlet.info [~]# head testing.txt
  87. Testing for bash command
  88. William Lam
  89. Today is a good day
  90. peachlet@peachlet.info [~]# head -n testing.txt
  91. head: testing.txt: invalid number of lines
  92. peachlet@peachlet.info [~]# head -n 2 testing.txt
  93. Testing for bash command
  94. William Lam
  95. peachlet@peachlet.info [~]# head -n 3 testing.txt
  96. Testing for bash command
  97. William Lam
  98. Today is a good day
  99.  
  100. ========================================================
  101.  
  102. If two files have same ending, or beginning , you can use { and } to specify for it
  103.  
  104. ========================================================
  105. -rw-r----- 1 root mail 24 Dec 26 02:34 trueuserdomains
  106. -rw-r--r-- 1 root mail 34 Dec 26 02:34 trueuserowners
  107.  
  108.  
  109.  
  110. peachlet@peachlet.info [/etc]# head -n 2 trueuser{domains,owners}
  111. head: cannot open `trueuserdomains' for reading: Permission denied
  112. ==> trueuserowners <==
  113. #userowners v1
  114. peachlet: peachlet
  115. peachlet@peachlet.info [/etc]#
  116.  
  117.  
  118. ========================================================
  119.  
  120. Sequences are also permitted within the braces {string...string2} provided that the range is actually sequential (e.g. a through z or 1 through 100).
  121.  
  122. ========================================================
  123. root@rstraining.lantstic.com [~]# echo ab{{c..h},{1..3}}d
  124. abcd abdd abed abfd abgd abhd ab1d ab2d ab3d
  125. ========================================================
  126.  
  127. In above example, we see our echo command print the strings 'a' 'b' and 'd' with the range of 'c' through 'h' or '1' through '3' between the 'b' and 'd'. Recall that with this form of expansion we are able to create backup file names without duplicating the constant prefix string:
  128.  
  129. ========================================================
  130. root@rstraining.lantstic.com [~]# echo ./.bashrc{,.backup}
  131. ./.bashrc ./.bashrc.backup
  132. ========================================================
  133.  
  134. Tilde Expansion
  135. As covered in Bash Basics, ~ will expand to the $HOME variable within the current $USER environment. Thus, be aware of the user for which you are logged in as. For example, note the different expansions of ~ below for certain users:
  136. ========================================================
  137. root@rstraining.lantstic.com [~]# echo $USER
  138. root
  139. root@rstraining.lantstic.com [~]# echo ~
  140. /root
  141. lantstic@lantstic.com [~]# echo $USER
  142. lantstic
  143. lantstic@lantstic.com [~]# echo ~
  144. /home/lantstic
  145. ========================================================
  146.  
  147. Parameter/Variable Expansion
  148.  
  149. Variables (or parameters) that are set in our bash sessions can be called using $VARIABLE where we replace 'VARIABLE' with the actual name of the pointer. While $VARIABLE is acceptable, when using this form of expansion we recommend the following form (including double quotes): "${VARIABLE}". Additional features can be utilized as documented in man bash. Here are some examples:
  150.  
  151. lantstic@lantstic.com [~]# echo ${USER}
  152. lantstic
  153.  
  154. Setting an error message for variables that are not set or are null strings:
  155.  
  156. lantstic@lantstic.com [~]# echo ${NOTSET}
  157.  
  158. lantstic@lantstic.com [~]# echo ${NOTSET:?unset}
  159. -bash: NOTSET: unset
  160. lantstic@lantstic.com [~]# echo ${IFS:?unset}
  161.  
  162. lantstic@lantstic.com [~]#
  163.  
  164. Removing suffixes from the expanded string:
  165.  
  166. lantstic@lantstic.com [~]# echo ${FILENAME}
  167. index.php
  168. lantstic@lantstic.com [~]# echo ${FILENAME%%.php}
  169. index
  170. lantstic@lantstic.com [~]# echo ${FILENAME%%.php}.html
  171. index.html
  172.  
  173. More examples are covered in man bash. I encourage you as always to review them, as you may find the features interesting. In addition to the standard parameters we've seen here, I would like to introduce the concept of arrays.
  174. Arrays
  175.  
  176. Bash allows one-dimensional and associative array parameters to be set. These are similar to normal parameters; however, they allow for positional reference to their components. That sounds like a mouthful, so let's use a bit of code. There are a few methods to set an array, I will show both for thoroughness.
  177.  
  178. lantstic@lantstic.com [~/globbing]# declare -a omg=(red blue yellow)
  179.  
  180. Or
  181.  
  182. lantstic@lantstic.com [~/globbing]# omg=(red blue yellow)
  183.  
  184. As you can see, this is very similar to setting local parameters; however, where this diverges from basic parameter expansion is in the means to print the member components of the array. Code is worth a thousand paragraphs, so let's look:
  185.  
  186. lantstic@lantstic.com [~/globbing]# declare -a omg=(red blue yellow)
  187. lantstic@lantstic.com [~/globbing]# echo ${omg}
  188. red
  189.  
  190. Not exactly what we we wanted considering we set the array to red, blue, and yellow. So we'll have to use a new feature:
  191.  
  192. lantstic@lantstic.com [~/globbing]# echo ${omg[@]}
  193. red blue yellow
  194.  
  195. That will get me the whole line; however, as these are positional parameters, I can reference each separate value. The parameters are assigned a numerical value starting with 0.
  196.  
  197. lantstic@lantstic.com [~/globbing]# echo ${omg[0]}
  198. red
  199. lantstic@lantstic.com [~/globbing]# echo ${omg[1]}
  200. blue
  201. lantstic@lantstic.com [~/globbing]# echo ${omg[2]}
  202. yellow
  203.  
  204. Lastly, much like with other parameters, we can remove the array using unset.
  205. Try it yourself!
  206.  
  207. Go ahead and set an array to a variety of strings. Call each one using echo.
  208. Command Substitution
  209.  
  210. Command substitution allows the output of a command to replace the command itself. The preferred syntax is as follows:
  211.  
  212. $(command)
  213.  
  214. This expansion also has a deprecated syntax you will occasionally see in older scripts and code using back-ticks:
  215.  
  216. `command`
  217.  
  218. This form does not nest well. The preferred syntax is $(command) as previously shown in Bash Basics. Here are some examples combined with brace and parameter expansion.
  219.  
  220. lantstic@lantstic.com [~]# echo $(date +%s).backup
  221. 1481233947.backup
  222. lantstic@lantstic.com [~]# echo ${FILENAME%%php}$(date +%s).backup
  223. index.1481233992.backup
  224. lantstic@lantstic.com [~]# cp -v ${FILENAME}{,.$(date +%s).backup}
  225. `index.php' -> `index.php.1481234103.backup'
  226.  
  227.  
  228. $() is for STORING it
  229. ${} is for DECLARING it
  230.  
  231.  
  232. $((expression))
  233.  
  234. Arithmetic Expansion and Word Splitting
  235. Arithmetic Expansion
  236.  
  237. Arithmetic expansion allows the evaluation of an arithmetic expression and substitutes the result to the command-line. The form is as follows:
  238.  
  239. $((expression))
  240.  
  241. Appropriate expressions are found in the section 'ARITHMETIC EVALUATION' in man bash. All tokens in the expansion undergo parameter expansion, command substitution, and quote removal.
  242.  
  243. lantstic@lantstic.com [~]# n=1;((n++));echo $n
  244. 2
  245.  
  246. The $ is left off as we are not assigning a parameter to the expression. Note the difference:
  247.  
  248. lantstic@lantstic.com [~]# n=1;x=((n+=1));echo $x
  249. -bash: syntax error near unexpected token `('
  250. lantstic@lantstic.com [~]# n=1;x=$((n+=1));echo $x
  251. 2
  252.  
  253. Additional operators can be found here.
  254. Word Splitting
  255.  
  256. Whenever parameter expansion, command substitution, arithmetic expansion are invoked, bash performs word splitting to separate the words. Splitting is only performed on strings that are not double quoted. Words are split by the value of $IFS or the 'internal field separator'. This is usually a tab, space or newline. If $IFS is unset, word splitting does not occur. Each token separated by $IFS not in double quotes is treated as a new word or token; consider the difference here:
  257.  
  258. lantstic@lantstic.com [~/globbing/test]# for i in * ; do ls $i ; done
  259. /bin/ls: cannot access test: No such file or directory
  260. /bin/ls: cannot access file: No such file or directory
  261. /bin/ls: cannot access 1.txt: No such file or directory
  262. /bin/ls: cannot access test: No such file or directory
  263. /bin/ls: cannot access file: No such file or directory
  264. /bin/ls: cannot access 2.txt: No such file or directory
  265. /bin/ls: cannot access test: No such file or directory
  266. /bin/ls: cannot access file: No such file or directory
  267. /bin/ls: cannot access 3.txt: No such file or directory
  268. /bin/ls: cannot access test: No such file or directory
  269. /bin/ls: cannot access file: No such file or directory
  270. /bin/ls: cannot access 4.txt: No such file or directory
  271. /bin/ls: cannot access test: No such file or directory
  272. /bin/ls: cannot access file: No such file or directory
  273. /bin/ls: cannot access 5.txt: No such file or directory
  274. /bin/ls: cannot access this: No such file or directory
  275. /bin/ls: cannot access file: No such file or directory
  276. /bin/ls: cannot access has: No such file or directory
  277. /bin/ls: cannot access spaces: No such file or directory
  278.  
  279. We guard against this unintentional word splitting by using double quotes to preserve the parameter expanded:
  280.  
  281. lantstic@lantstic.com [~/globbing/test]# for i in * ; do ls "$i" ; done
  282. test\ file\ 1.txt
  283. test\ file\ 2.txt
  284. test\ file\ 3.txt
  285. test\ file\ 4.txt
  286. test\ file\ 5.txt
  287. this\ file\ has\ spaces
  288.  
  289. This leads us to the final form of expansion we will cover here: pathname expansion.
  290. Activity
  291.  
  292. Take a moment to reviewing the QUOTING section in man bash. Why are double quotes necessary for protection against word splitting and not single quotes or the back slash?
  293.  
  294. Pathanme Expansion, Globstar, and Optional Shell Behavior
  295. Pathname Expansion
  296.  
  297. Pathname expansion was covered extensively in Bash Basics. The main meta-characters and their meaning should already be familiar to you; however, we should cover it some more so that we understand exactly what it is doing. This form of expansion is how the bash matches patterns. Unless the -f option has been set, bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option nullglob is disabled, the word is left unchanged. If the nullglob option is set, and no matches are found, the word is removed. If the failglob shell option is set, and no matches are found, an error message is printed and the command is not executed. If the shell option nocaseglob is enabled, the match is performed without regard to the case of alphabetic characters. We've already shown in Bash Basics the meaning of each meta-character. I recommend reviewing man bash for a brief review. However, there is one meta-character that deserves special attention.
  298. Globstar
  299.  
  300. While we have seen these meta-characters in action in previous courses and likely on the floor, we should really understand what * or globstar represents, what it doesn't, and what it is not. First, globstar in the context of pathname expansion is not a 'wildcard': never was and never will be. If we only look at the explanation of the meta-character and nothing else, we will think that * is a wildcard, when in fact it is not:
  301.  
  302. * Matches any string, including the null string. When the globstar shell option is enabled, and * is used in a pathname expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.
  303.  
  304. However, please note the following from man bash:
  305.  
  306. When a pattern is used for pathname expansion, the character ``.`` at the start of a name or immediately following a slash must be matched explicitly...The file names ``.`` and ``..`` are always ignored when GLOBIGNORE is set and not null
  307.  
  308. Note that GLOBIGNORE is a bash-specific feature and shells generally ignore ``.`` by default, as not to expand ``.`` and ``..`` unintentionally.
  309.  
  310. Thus, '*' will not match ``.`` or filenames starting with ``.`` unless specifically set to (which can be dangerous). This functionality actually was adopted by Unix shells from the original behavior of ls.
  311.  
  312. We can modify the expansion of globstar using shopt. With the built-in shopt, we can actually modify several shell options.
  313. Optional Shell Settings
  314.  
  315. In addition to the default features and loaded environment in bash, there are optional shell behaviors that govern functionality of cd, window-size of your terminal, version control, job management, pathname and globstar expansion, alias expansion, bash history, and source control. You will not be modifying the optional shell behaviors much beyond enabling checkwinsize to ensure text wrapping after screen resizing.
  316.  
  317. We can see the list of shell options and their status using shopt:
  318.  
  319. lantstic@lantstic.com [~/globbing/test]# shopt
  320. autocd off
  321. cdable_vars off
  322. cdspell off
  323. checkhash off
  324. checkjobs off
  325. checkwinsize off
  326. cmdhist on
  327. compat31 off
  328. compat32 off
  329. compat40 off
  330. dirspell off
  331. dotglob off
  332. execfail off
  333. expand_aliases on
  334. extdebug off
  335. extglob off
  336. extquote on
  337. failglob off
  338. force_fignore on
  339. globstar off
  340. gnu_errfmt off
  341. histappend off
  342. histreedit off
  343. histverify off
  344. hostcomplete on
  345. huponexit off
  346. interactive_comments on
  347. lithist off
  348. login_shell on
  349. mailwarn off
  350. no_empty_cmd_completion off
  351. nocaseglob off
  352. nocasematch off
  353. nullglob off
  354. progcomp on
  355. promptvars on
  356. restricted_shell off
  357. shift_verbose off
  358. sourcepath on
  359. xpg_echo off
  360.  
  361. Options can be enabled using the -s option and disabled using the -u option.
  362. Activity
  363.  
  364. Using shopt, ensure that your dotglob is disabled. Run the following commands, determine what pathname is being matched by these patterns:
  365.  
  366. *
  367. .*
  368.  
  369. What is the difference between the two?
  370.  
  371. Consider the following different output:
  372.  
  373. lantstic@lantstic.com [~/globbing/hidden]# /bin/ls *
  374. /bin/ls: cannot access *: No such file or directory
  375. lantstic@lantstic.com [~/globbing/hidden]# shopt -s dotglob
  376. lantstic@lantstic.com [~/globbing/hidden]# /bin/ls *
  377. .these files are hidden10.txt .these files are hidden2.txt .these files are hidden4.txt .these files are hidden6.txt .these files are hidden8.txt
  378. .these files are hidden1.txt .these files are hidden3.txt .these files are hidden5.txt .these files are hidden7.txt .these files are hidden9.txt
  379.  
  380.  
  381.  
  382. the $PATH variable stores the locations of several folders, bash will look for a file with the name you are using from the command line in these folders when you are trying to run a command
  383.  
  384. Program execution path and location
  385. Locating a program and its information
  386.  
  387. When speaking about a ‘program’, I am referring specifically to a binary. A binary is program written and compiled in machine code to execute a particular function within a system. These programs will have the binary file, the source, and the manual page stored on the system. To locate these files, we would use the whereis command. whereis has a hard coded path and will look for these binaries, sources, or manual pages in standard Linux locations and, if set, the directories listed in $PATH and $MANPATH. However, you can specify a directory for whereis to check.
  388.  
  389. lantstic@lantstic.com [~]# whereis php
  390. php: /usr/bin/php /usr/bin/php.working /usr/lib/php /usr/lib/php.ini /usr/local/bin/php /usr/local/lib/php /usr/local/lib/php.ini,v /usr/local/lib/php.ini /usr/include/php /usr/local/php
  391.  
  392. If instead of listing all binary, source, or manual page files that may match our provided program in the particular directory, we want to know exactly which binary would be executed within our current session, we can use the command which:
  393.  
  394. lantstic@lantstic.com [~]# which rm
  395. /bin/rm
  396.  
  397. Scanning ${PATH}, which will print out the absolute path of the executable that would be launch on the command-line. This is all very useful if you wanted to determine the exact php-cli binary that would execute within your current bash session:
  398.  
  399. lantstic@lantstic.com [~]# which php
  400. /usr/local/bin/php
  401.  
  402. Activity
  403.  
  404. Go ahead and run which and whereis on various commands we've previously covered. If you do not get any output, try type to see the difference.
  405.  
  406. ========================================================
  407. 1
  408.  
  409. = stdout
  410. 2
  411.  
  412. = stderr
  413. 0
  414.  
  415. = stdin
  416.  
  417. ========================================================
  418.  
  419. stdout = standard output
  420. stdin = standard input
  421. stderr = standard error
  422.  
  423. STDIN == standard input; this is received from files, command-line input from peripherals, or other programs.
  424. STDOUT == standard output; this is the non-error output for programs. To see this in action, you can simply run ls. The list of files printed to terminal represents the standard output.
  425. STDERR == standard error; the error or diagnostic messages.
  426.  
  427. The right chevron > is used to redirect either stdout, stderr, or both. This feature is commonly used to write data to a file such as command > file. If the file did not exist, the > would create a new file. If the file did exist, the > would truncate the data in the file, overwriting it with the stdout from the command. We can instead append our output using double chevrons: >>.
  428.  
  429. stdout and stderr can be redirected simultaneously using several methods. Some of them include the following:
  430.  
  431. command &> word
  432. command > word 2>&1
  433.  
  434. Test
  435.  
  436. root@lam [~]# cat > whatevertest.txt << "eof"
  437. > This is for testing purpose
  438. > I love hostgator
  439. > learning this is confusing but yet very interesting
  440. > eof
  441. root@lam [~]# cat whatevertest.txt
  442. This is for testing purpose
  443. I love hostgator
  444. learning this is confusing but yet very interesting
  445. root@lam [~]#
  446.  
  447.  
  448. ========================================================
  449.  
  450. A pipeline is a sequence of one or more commands separated by the operators | or |&. The more common operator you will see is the pipe ‘|’. The stdout of one command is sent as the stdin of the succeeding command: command1 | command2.
  451.  
  452. Recall that all programs have at least three standard streams at execution: stdin, stdout, and stderr. These are file handles that are created for every program, every time. When using process substitution we are not redirecting a stream but instead taking the sum of the file handles, treating them as a single file descriptor and executing a program as if its input was the file descriptor. Effectively, cat file and cat <(list) are syntactically the exact same; however, <(list) is not a file on the file system in the same manner as file. When <(list) is performed, stdin or stdout is connected to a file in /dev/fd (other systems use named pipes, but ours do not). That file in /dev/fd is read as part of the expansion. It is important to note the subtle distinctions between process substitution, command substitution, and pipelines. Again, code is worth a thousand paragraphs.
  453.  
  454. ========================================================
  455. lantstic@lantstic.com [~/globbing]# echo date +%s
  456. date +%s
  457. lantstic@lantstic.com [~/globbing]# echo $(date +%s)
  458. 1478548741
  459. lantstic@lantstic.com [~/globbing]# echo <(date +%s)
  460. /dev/fd/63
  461. lantstic@lantstic.com [~/globbing]# date +%s|echo
  462.  
  463. lantstic@lantstic.com [~/globbing]#
  464. ========================================================
  465. Here we compare running echo against a string, command substitution, process substitution, and a pipeline. Note the difference. Specifically as you can see, echo <(list) will merely print the pathname of the file created in /dev/fd when process substitution is invoked.
  466.  
  467.  
  468. When using redirection, do not truncate client files; there should not be an instance in which you are truncating client files, unless specifically asked. But a good rule of thumb: > should not be used on existing files. If you are going to write to an already existing file using tee -a for append or >> to append the stdout redirection are preferred.
  469.  
  470.  
  471.  
  472. Wordpress database name / username
  473. peachlet_wordpress
  474. peachlet_wrdhg
  475. Wave123###
  476. ========================================================
  477. hgadmin
  478. Wave#@!dud342
  479. ========================================================
  480.  
  481. Joomla
  482. peachlet_joomla
  483. peachlet_joom
  484.  
  485.  
  486. Finding Files
  487. A Brief on I/O
  488. Before we begin, I would like to briefly cover what we mean by I/O. In the previous lessons we mentioned the input and output streams stdin, stdout, and stderr. All programs have these streams. In short, I/O is the transfer of any data from one program or device to another program or device. Commands read input from the command-line, executing against files on the disk. These files and/or the corresponding inode are read from disk as input and written to our output.
  489.  
  490. lantstic@lantstic.com [~]# stat ./public_html/
  491. File: `./public_html/'
  492. Size: 4096 Blocks: 8 IO Block: 4096 directory
  493. Device: fc01h/64513d Inode: 4853159 Links: 9
  494. Access: (0750/drwxr-x---) Uid: ( 541/lantstic) Gid: ( 99/ nobody)
  495. Access: 2016-12-09 02:03:29.645531559 -0600
  496. Modify: 2016-11-28 12:48:35.348868800 -0600
  497. Change: 2016-11-28 12:48:35.348868800 -0600
  498. The input file here is ./public_html; however, note that stat is a program that must (in this case) execute from /usr/bin/stat. The ELF is first read, producing I/O, then executed. During its execution, necessary files called 'libraries' are accessed, opened, and read to complete the execution. This produces in increase of the input stream on the system as a whole. Once the data is read from the input stream, additional calls are executed and sent to the output stream. In short, the execution of stat alone produces an I/O overhead, and once the program completes its load, its input argument (a filename, in this case public_html) produces an additional I/O overhead.
  499.  
  500. Here stat is dealing with a single file; however, when running a command across an undetermined, large number of files, system I/O will increase potentially causing performance degradation. Please keep this concept in mind in both this lesson and your work on the floor.
  501.  
  502. With that out of the way, let's look at find.
  503.  
  504.  
  505. find is a simple command — at first. find will search and print all (by default) files within a directory hierarchy. If no pathname is provided, the present working directory is used. With no tests or actions, find will print all file system segments (read: files) recursively from the provided directory: this list includes regular files, links, directories, sockets, devices, block devices, and so on.
  506.  
  507. lantstic@lantstic.com [~/globbing]# find .
  508.  
  509. lantstic@lantstic.com [~/globbing]# find ~+
  510.  
  511. Options
  512.  
  513. Of course, we usually don't want everything from 'find .'. We can trim this with tests; however, before we begin, we want to be aware of useful options in find. The following is a non-exhaustive list of options to use with find, these are common options to help your commands. Please review the man page for find for more on these options:
  514.  
  515. maxdepth
  516. mindepth
  517. mount
  518. daystart (should be used in conjunction with [acm]time)
  519. regextype
  520. depth
  521.  
  522.  
  523. Tests and more tests
  524.  
  525. The tests that you pass to find will constitute your ‘bread and butter’ for your find commands. We will not cover an extensive list of tests here — that’s what the man page is for. However, below are tests that you should be familiar with and tests that you will use frequently.
  526.  
  527. type
  528. regex
  529. size
  530. name
  531. perm
  532. user
  533. inum
  534. wholename
  535. [acm]time (we prefer ctime as other values can be spoofed using touch)
  536. These tests are also “introductory”: more complex tests may arise that will aid in your abilities to search.
  537.  
  538. There are several operators that can be found in man find section 'OPERATORS'. Important operators to keep in mind are -o, !, and the form expr1 expr2 or expr1 -a expr2. The last example is the default behavior of find. find defaults to a logical ‘AND’ syntax. Each test is considered and compounded to the last. This can be confirmed under the section ‘Operators’ in the man page. You can force an operator such as -o to set an OR: expr1 -o expr2. This expression means 'expr2 if expr1 returned false'. Use of parenthetical statements can be used to ensure that the files filtered from the first expressions are further examined:
  539.  
  540.  
  541. ========================================================
  542. expr1 -o \(...expr2 \)
  543. ========================================================
  544.  
  545. In the above, note the escape character \ used to escape the parenthesis such that find expands the ( and not bash. Consider this example with the negation operator (!) in place:
  546.  
  547. ========================================================
  548. lantstic@lantstic.com [~/globbing]# find . -type f ! -name '*.jpg' -o ! -name '*.php' -o ! -name '*.js'
  549. .
  550. ./this file has spaces.css
  551. ./lorem
  552. ./ex1.6
  553. ./ex1-donotmatch.7
  554. ./ex1.5
  555. ./echo
  556. ./this file has spaces.js
  557. ./this file has spaces.jpg
  558. ./.thisfileishidden
  559. ./ex1.2
  560. ./ex1.4
  561. ./ex1.1
  562. ./this file has spaces.php
  563. ./this file has spaces.txt
  564. ./this file has spaces.exe
  565. lantstic@lantstic.com [~/globbing]# find . -type f ! \( -name '*.jpg' -o -name '*.php' -o -name '*.js' \)
  566. ./this file has spaces.css
  567. ./lorem
  568. ./ex1.6
  569. ./ex1-donotmatch.7
  570. ./ex1.5
  571. ./echo
  572. ./.thisfileishidden
  573. ./ex1.2
  574. ./ex1.4
  575. ./ex1.1
  576. ./this file has spaces.txt
  577. ./this file has spaces.exe
  578. ========================================================
  579.  
  580. In the first example, it would seem that we are trying to find files that do not end with the list of extensions, separated by -o (OR); however, as this is a logical OR, all tests of -name [string] return true if false per the operator '!' as we have files matching the requirements for that test and negation. Thus false positives are evaluated. To correct this, we would need to separate the OR-statement of the -name tests from the negation operator. The second example shows this. In the second example, the OR-statement is evaluated within the parenthetical; then, the whole statement or expression is negated; thus, returning the files that we want: those without the provided extension.
  581. ========================================================
  582.  
  583.  
  584. Finding Files
  585. Actions and Find
  586. Once we have our tests and a filtered list of file system segments that we need, we can perform a number of actions. Actions should follow after all tests have completed. find will perform in the order of the tests and actions from left to right. If an action precedes all tests, the file list will not be trimmed prior to the action, which is very dangerous.
  587.  
  588. There are several actions detailed in the ‘Action’ section of the man page. We will list the less concerning actions here for you.
  589.  
  590. print
  591. printf
  592. ls
  593. prune (note: -depth is implied and as such a more dangerous action — -delete — cannot be used)
  594. Activity
  595.  
  596. Please review and take notes on the above actions before we proceed. You can find an explanation of these actions in the man page.
  597.  
  598. I sense great danger here, young Jedi
  599.  
  600. If you reviewed the other actions listed in the section ‘ACTIONS’ in the man page, you likely noticed some other actions that are obviously problematic — or at least dangerous. We will cover these here with caveats and notes on best practices:
  601.  
  602. -delete
  603. -exec (-execdir)
  604. -ok (same as exec but with prompt)
  605. Any use of the -delete options should be tempered with care. Our recommendation when using this action with find is to (obviously) have -delete be the last action added to the find command. It should be terminal string in the command line. Additionally, your find statement should first be ran to ensure that the list does not contain any false positives. Review this list in full. You may need to further sanitize the list such that changes to said list do not result in more files removed than intended. For this, we would recommend using tee to send your find output to a file for easy review. Additionally, you can also send find's output to less.
  606.  
  607. The action -exec allows you call any command to act upon all segments matched against your tests. The form is generally, -exec [command] {} \; . The {} represent the segments that were filtered by your test, being replaced by the current file name being processed. The command will continue until a ; is encountered and as such will need to be escaped with \ to signify a termination of the command and begin the next form of processing.
  608. ========================================================
  609.  
  610. lantstic@lantstic.com [~/globbing]# find . -type f -exec grep -q 'lorem' {} \; -ls
  611. 2228307 4 -rw-rw-r-- 1 lantstic lantstic 66 Oct 7 11:23 ./lorem
  612.  
  613.  
  614. -exec handles the above construction, handling each segment or file, one at a time. In the format -exec [command] {} + the command line is appended with the selected file name such that -exec will process as many files against the command provided as possible. This behavior is not recommended for use on our client’s accounts or our production servers.
  615.  
  616. -exec will execute the command provided from the starting directory of the find command. This can introduce security issues that while often not observed on our farm are potential risks. Full explanation of this security risk can be provided in the workshops or in correspondence with the admin trainers, as it is out of the scope of this document (not enough space to explain race conditions, unfortunately). However, find offers a mitigation for the behavior of -exec in the form of -execdir. This allows for the command to be executed in the directory containing the matched file. Considerations for $PATH must be made, however. These are detailed in the man page.
  617.  
  618. ========================================================
  619.  
  620. With all of the considerations and warnings presented so far, lets get into some of the usefulness of find. Below are examples and explanations of what find is accomplishing in that scenario. Following this section is an activity for you to complete.
  621.  
  622. lantstic@lantstic.com [~]# find -type l -size +10c -ls
  623. 2238813 0 lrwxrwxrwx 1 lantstic lantstic 34 Dec 8 2015 ./access-logs -> /usr/local/apache/domlogs/lantstic
  624. 5384103 0 lrwxrwxrwx 1 lantstic lantstic 22 Dec 8 2015 ./mail/.webmaster@lantstic_com -> lantstic.com/webmaster
  625. 5384104 0 lrwxrwxrwx 1 lantstic lantstic 18 Dec 8 2015 ./mail/.admin@lantstic_com -> lantstic.com/admin
  626. 3933078 0 lrwxrwxrwx 1 lantstic lantstic 58 Feb 2 2016 ./var/cpanel/styled/current_style -> /usr/local/cpanel/base/frontend/paper_lantern/styled/retro
  627. 2237329 0 lrwxrwxrwx 1 lantstic lantstic 11 Dec 8 2015 ./www -> public_html
  628. 6825002 0 lrwxrwxrwx 1 lantstic lantstic 25 Aug 9 00:19 ./.cphorde/meta/latest -> horde.backup.sql.20160809
  629. ========================================================
  630.  
  631. In the above example, we see that no pathing is explicitly provided, thus we use relative pathing with the '.' is assumed. My first test is -type l specifying that I am looking for a symbolic link. The second test -size +10c indicates to match any member of the first test that is 10 bytes or larger in size. Once the list is trimmed, we use the action -ls which will list in ls -dils format.
  632.  
  633. lantstic@lantstic.com [~]# find ~+ ! -user lantstic -perm 0644 -ls
  634. 2230477 0 -rw-r--r-- 1 root root 0 Jul 22 18:31 /home/lantstic/this\ file\ has\ spaces
  635. 2237291 4 -rw-r--r-- 1 root root 29 Jul 25 10:49 /home/lantstic/LIST
  636. 6828474 0 -rw-r--r-- 1 root root 0 Jul 28 17:47 /home/lantstic/public_html/this\ file\ has\ spaces
  637. 6824825 0 -rw-r--r-- 1 root root 0 May 12 11:20 /home/lantstic/public_html/......5D.....
  638. 6828464 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test1
  639. 6828468 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test5
  640. 6828467 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test4
  641. 6828472 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test9
  642. 6828466 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test3
  643. 6828469 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test6
  644. 6828470 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test7
  645. 6828465 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test2
  646. 6828473 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test10
  647. 6828471 0 -rw-r--r-- 1 root root 0 Jul 28 15:45 /home/lantstic/public_html/cache/test8
  648. 2237297 236 -rw-r--r-- 1 root root 238302 Jun 6 11:05 /home/lantstic/strace.16546
  649. 2228278 7776 -rw-r--r-- 1 root root 7961036 Sep 7 09:59 /home/lantstic/latest.tar.gz
  650. 2237292 40 -rw-r--r-- 1 root root 38569 Jul 28 11:25 /home/lantstic/php.ini
  651. ========================================================
  652. In the above example, we use tilde expansion to set our pathing to absolute. Our first test is for -user lantstic to match those files that are owned by lantstic; however, this is preceded by the operator '!' which negates the matches. Then that list is further trimmed by the test -perm 0644 to match those segments that have 0644 permissions. Lastly, we perform the -ls action.
  653.  
  654. lantstic@lantstic.com [~]# find -maxdepth 2 -type f ! \( -name '*.php' -o -name 'ex1*' \) -size 0c -ctime -5 -exec stat -c%i" "%n {} \;
  655. 2228356 ./newfile.txt
  656. 2228574 ./globbing/this file has spaces.css
  657. 2228575 ./globbing/this file has spaces.js
  658. 2228576 ./globbing/this file has spaces.jpg
  659. 2228577 ./globbing/.thisfileishidden
  660. 2228572 ./globbing/this file has spaces.txt
  661. 2228573 ./globbing/this file has spaces.exe
  662.  
  663. The above example may appear complex; however, we'll take it a step at a time. First, we specify our depth of the current directory and one sub-directory down. Next, we use -type f to match only regular files. The operator '!' is used to the negate the following set of tests. We escape the parenthetical using \ and set up our OR-statement for the tests -name '*.php' and -name 'ex1*'. Once that list is trimmed, we want only those files that are zero-byte files, whose change time occurred within the last 5 24 hour cycles (without the - preceding the '5', -ctime would only match a file changed exactly 5 24 hours cycles ago). Once we have that list, the action -exec stat -c%i" "%n {} \; will print the inode number and the pathname. The " " are necessary to print those values with a space between them.
  664. ========================================================
  665.  
  666.  
  667. G/re/p 2.0
  668. G/re/p Control
  669. grep has several controls: match control, output control, output prefix, context line control. In addition to this, grep allows the use of basic, extended, and perl regular expressions. We will be covering particular options in depth throughout this lesson. For this lesson, using your shell shell account on training-ce.internal.hostgator.com using port 2222. We will be using sample files primarily from the ./regex directory therein. If you do not have that directory, please let an admin trainer know.
  670.  
  671. Let's start our lesson with match control.
  672.  
  673. Match Control
  674.  
  675. The following flags will be the most common pertaining to match control. Please review man grep for a full explanation of these. I recommend taking detailed notes explaining these actions in your notebooks.
  676.  
  677. -e
  678. -i
  679. -v
  680. The -e option specifies the PATTERN for which we are searching. The default structure of grep is as follows:
  681.  
  682. grep PATTERN [FILE]
  683. The -e PATTERN form mirrors the above behavior; however, when explicitly provided as an option, repeated use allows multiple patterns to be search from a file:
  684.  
  685. [root@training-ce regex]# grep -e 'kiwi' -e 'apple' fruit.txt
  686. apple
  687. pineapple
  688. kiwi
  689. Of course, multiple search strings can be implemented using alternation with grep, escaping the pipe character in the search string:
  690.  
  691. [root@training-ce regex]# grep 'kiwi\|apple' fruit.txt
  692. apple
  693. pineapple
  694. kiwi
  695. Option -i toggles case insensitivity:
  696.  
  697. [root@training-ce regex]# grep -i 'jeff' names.txt
  698. Jeffrey
  699. Jeffery
  700. This is useful for instances when case is either not controlled or unknown.
  701.  
  702. Next, have the -v option. This option will invert the match. By default grep will print the line that contains a match of the pattern; -v inverts this such that lines that do not match are sent to stdout:
  703.  
  704. [root@training-ce regex]# cat repeat_example.txt
  705. This sentence has a repeated repeated word.
  706. This sentence does not.
  707. [root@training-ce regex]# grep -v repeat repeat_example.txt
  708. This sentence does not.
  709. These options are rather straight-forward. You can practice a few of these options on your account.
  710.  
  711. ========================================================
  712. Output Control of GREP
  713.  
  714. The following options will cover our output control:
  715.  
  716. -c
  717. -L
  718. -o
  719. -s
  720.  
  721. [root@training-ce regex]# grep -c apple fruit.txt
  722. 2
  723. [root@training-ce regex]# grep -vc apple fruit.txt
  724. 14
  725.  
  726. When used in conjunction with multiple files, -L will print only those files that do not have a corresponding match to the provided pattern:
  727. [root@training-ce regex]# grep -L 'apple' *.txt
  728. backreference.txt
  729. correct_emails.txt
  730. correct_names.txt
  731. correct_servers2.txt
  732. correct_servers.txt
  733. dot_example.txt
  734. ips.txt
  735. names.txt
  736. repeat_example.txt
  737. servers.txt
  738. times.txt
  739.  
  740. -l will merely print the filenames that match, if multiple have been provided. This is useful with find for example:
  741. [root@training-ce regex]# grep -l 'nn' *.txt
  742. ./correct_names.txt
  743. ./names.txt
  744. ./correct_errorlog.txt
  745. ./test2.txt
  746. ./correct_emails.txt
  747. ./test1.txt
  748.  
  749. -o will format the output to show only the matched string or pattern. Consider the difference here:
  750.  
  751. [root@training-ce regex]# grep Zoidberg foo
  752. what, you didn't know that Zoidberg likes sandwiches?
  753. [root@training-ce regex]# grep -o Zoidberg foo
  754. Zoidberg
  755. This can be very useful in providing us only the information that we want from a log file without the full output. Consider the following (this example basic regex which we will cover more in-depth later):
  756.  
  757. blackest@blackest.info [~]# grep -o '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' ./access-logs/blackest.info |sort -u
  758. 157.55.39.125
  759. 167.114.118.4
  760. 180.76.15.138
  761. 180.76.15.147
  762. 192.185.1.20
  763. 216.172.180.21
  764. 220.181.108.180
  765. 91.210.146.52
  766. In the above example, with just a bit of simple regex, I am able to print only the unique IPs that have hit the site 'blackest.info' from the access logs.
  767.  
  768. Lastly, -s will suppress error messages about nonexistent files or unreadable files. There are portability concerns for shell scripting, which can be reviewed in man grep.
  769.  
  770. [root@training-ce regex]# grep 'apple' fruit.txt doesnotexist.txt
  771. fruit.txt:apple
  772. fruit.txt:pineapple
  773. grep: doesnotexist.txt: No such file or directory
  774. [root@training-ce regex]# grep -s 'apple' fruit.txt doesnotexist.txt
  775. fruit.txt:apple
  776. fruit.txt:pineapple
  777. The output prefixing showing in the above examples leads us to our next topic on output prefix control.
  778.  
  779. Output Prefix Control
  780.  
  781. These control mechanisms allow us to manage the prefixing for our output:
  782.  
  783. -H
  784. -h
  785. -n
  786. -H will provide us with both the match line, prefixed with the pathname of the file containing that match:
  787.  
  788. [root@training-ce regex]# grep '10:' *.txt
  789. times.txt:10:42 PM
  790. times.txt:10:24 AM
  791. times.txt:10:19 PM
  792. -h will do the opposite of the above. Filename prefixing will be suppressed, providing us only with the line matching the string:
  793.  
  794. [root@training-ce regex]# grep -h '10:' *.txt
  795. 10:42 PM
  796. 10:24 AM
  797. 10:19 PM
  798. -n will prefix the line number of the match. This can be used in conjunction to -H for greater utility when working with multiple files. Let's use this feature with -exec in a find command:
  799.  
  800. lantstic@lantstic.com [~]# find . -type f -name ".htaccess" -exec grep -Hn "AddHandler" {} \;
  801. ./public_html/.htaccess:1:#AddHandler application/x-http-php53 .php
  802. Next we will move on to context line control.
  803.  
  804. Context Line Control and File Selection
  805. Our last options concerning grep control are context line control and file selection.
  806.  
  807. Context Line Control
  808.  
  809. Context Line Control allows us to gather information about the lines surrounding our initial matches. The options for these are as follows:
  810.  
  811. -A
  812. -B
  813. -C
  814. All of these options take NUM as any whole integer. This specifies the number of lines we would like as context. -A will specify "lines after context" or lines after the string we have matched, printing the provided number of lines after matching lines:
  815.  
  816. lantstic@lantstic.com [~/public_html/blog]# grep -A 2 'BEGIN WordPress' ./.htaccess
  817. # BEGIN WordPress
  818. <IfModule mod_rewrite.c>
  819. RewriteEngine On
  820. With -B we are able to match an arbitrary number of lines before the context or match string:
  821.  
  822. lantstic@lantstic.com [~/public_html/blog]# grep -B 2 'END WordPress' ./.htaccess
  823. RewriteRule . /blog/index.php [L]
  824. </IfModule>
  825. # END WordPress
  826. Lastly, we have -C. When provided with an integer, we get the full context with matching lines before and after according to the number provided:
  827.  
  828. lantstic@lantstic.com [~/public_html/blog]# grep -C1 'special' this-is-a-list.txt
  829. This is a file.
  830. Each line is special.
  831. kthx.
  832. File Selection
  833.  
  834. grep is able to search multiple files and exclude certain file from being searched. These options are as follows:
  835.  
  836. -R
  837. --exclude
  838. -R will search recursively through a directory or pathname provided, searching each file for the provided string. Here is an example in conjunction with -l:
  839.  
  840. lantstic@lantstic.com [~/public_html/blog]# grep -l -R 'lantstic'
  841. error_log
  842. file-list.txt
  843. grep
  844. wp-config.php
  845. .qidb/Dirnames
  846. .qidb/__db.003
  847. .qidb/Packages
  848. wp-admin/error_log
  849. --exclude allows us to remove certain GLOB matches from our searches. Specifying --exlcude-dir will also allow us to exclude a directory entirely. Here are a few examples:
  850.  
  851. lantstic@lantstic.com [~/public_html/blog]# grep -l -R 'lantstic' --exclude=__*
  852. error_log
  853. file-list.txt
  854. grep
  855. wp-config.php
  856. .qidb/Dirnames
  857. .qidb/Packages
  858. wp-admin/error_log
  859. lantstic@lantstic.com [~/public_html/blog]# grep -l -R 'lantstic' --exclude-dir=.qidb
  860. error_log
  861. file-list.txt
  862. grep
  863. wp-config.php
  864. wp-admin/error_log
  865. With these options and features covered, we can move on to basic regular expressions. For this next lesson, we will focus primarily on grep for these examples.
  866.  
  867.  
  868.  
  869. ========================================================
  870. print only matching string
  871.  
  872. = -o
  873. counts number of line matches
  874.  
  875. = -c
  876. Provides n number of lines before context
  877. = -B
  878. Prints only the matching file name.
  879.  
  880. = -l
  881. That's correct!
  882. ========================================================
  883. regex = special characters that have special meaning beyond their literal meaning
  884.  
  885. ^
  886. $
  887. .
  888. [
  889. [^
  890. ?
  891. *
  892. +
  893. \
  894.  
  895. ========================================================
  896. For
  897. Anachors = To search either from the BEGINNING or the END of a line, using ^ (cadet) we can match for BEGINNING (The start) of the line
  898.  
  899. Ex:
  900.  
  901. grep '^#' ./.htaccess
  902.  
  903. This will output the lines starting with #
  904.  
  905. Output:
  906. lantstic@lantstic.com [~/public_html/blog]# grep '^#' ./.htaccess
  907. # BEGIN WordPress
  908. # END WordPress
  909.  
  910. To Search for ENDING with certain string, you will need to use $ to call the string
  911.  
  912. EX
  913.  
  914. grep 'Wordpress$' .htaccess
  915. Output:
  916. lantstic@lantstic.com [~/public_html/blog]# grep '^WordPress#' ./.htaccess
  917. # BEGIN WordPress
  918. # END WordPress
  919.  
  920. Same output, but because it is ending with Wordpress, so that's where the string output the ENTIRE line with WordPress
  921.  
  922. ========================================================
  923.  
  924. The dot (.) can standss for any one, single character. The dot is usually use with other meta-characters and quantiflier. But it is going to be an issue in search for when we would like to match a literal dot and not other character. We can use \ to escape the the dot.
  925.  
  926. Use \ to escape a character so the character can be read as a LITERAL character, so ^ will be reading as ^ instead of a search string starting with ^
  927.  
  928.  
  929. The [...] would search for anything you put in there
  930.  
  931. so if you put grep 'DB_[UNH]' ./wp-config.php
  932. It would search for
  933.  
  934. DB_anything containing U, N, H inside wp-config.php
  935.  
  936. Output
  937. define('DB_NAME', 'lantstic_wrdp1');
  938. define('DB_USER', 'lantstic_wrdp1');
  939. define('DB_HOST', 'localhost');
  940.  
  941. You can also use [...] to declare ranges
  942. [0-9]
  943. [a-z]
  944. [A-Z]
  945.  
  946. lantstic@lantstic.com [~/public_html/blog]# grep '^[a-d]' ./lulzstext.txt
  947. dollar sign $, but I don't want to match
  948. any of these anchors. Like here
  949.  
  950. Grep all the line START with a-d within the file called lulzstext.txt
  951.  
  952.  
  953. ========================================================
  954. We can negate a character or sets of characters by including the caret (^) within our brackets as such: [^...]. Any characters or range specified in the character class will be negated:
  955.  
  956. lantstic@lantstic.com [~/public_html/blog]# grep 'DB_[^PC]' ./wp-config.php
  957. define('DB_NAME', 'lantstic_wrdp1');
  958. define('DB_USER', 'lantstic_wrdp1');
  959. define('DB_HOST', 'localhost');
  960. ========================================================
  961.  
  962. * represents any number of matches to the prior item, including zero.
  963.  
  964. ? repeats the prior match at most once. + repeats the prior match at least once, but multiple matches are possible.
  965.  
  966. [root@training-ce regex]# grep '^gator[789]*\..*' ./servers.txt
  967. gator798.hostgator.com
  968. gator888.hostgator.com
  969. gator978.hostgator.com
  970. gator9.hostgator.com
  971.  
  972. [root@training-ce regex]# grep -E '^gator[789]?\.' ./servers.txt
  973. gator9.hostgator.com
  974.  
  975. [root@training-ce regex]# grep -E '^gator[789]+\.' ./servers.txt
  976. gator798.hostgator.com
  977. gator888.hostgator.com
  978. gator978.hostgator.com
  979. gator9.hostgator.com
  980.  
  981.  
  982. negate means it will effectively cancel
  983. so
  984. for example
  985. if you had the line
  986. william lam is a faggot, and you used [^lam], it would just do william is a faggot.
  987. since [^lam] removes lam from the line
  988.  
  989.  
  990. To grep between TWO condition
  991.  
  992. For example
  993.  
  994. Find all the words STARTING with E and ending with N, in the names.txt file would be
  995.  
  996. grep -i '^[e]'.*'[n$]' names.txt
  997.  
  998. whenever you see a ! in a FIND command, meaning NOT
  999.  
  1000. lantstic@lantstic.com [~]# find . -maxdepth 2 -type f ! -name ‘*.txt’ -size +1G -perm 0444 ! -user ${USER}
  1001.  
  1002. This find commands is locating starting from
  1003.  
  1004. current directory and the sub folder and the sub folder of the subfolder for all files NOT ending with .txt and have more than 1G disk space with the permission 444 and not user
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032. Which basic regex pattern will match only a literal dot.
  1033. c. \.
  1034. ========================================================
  1035.  
  1036. Select all commands that will provide you with the exact binary path of php as it will execute from your command-line.
  1037. The correct answer is: which php, type php
  1038. ========================================================
  1039. find ./mail -maxdepth 2 -delete -type f -size +0c -name "1*" -exec grep -il 'SPAM:' {} \;
  1040. The above will only remove the files larger than 0 bytes, matching the filename pattern "1*" that contains the string 'spam'.
  1041. FALSE
  1042. ========================================================
  1043.  
  1044. In as much detail as possible, explain what this command will do:
  1045. lantstic@lantstic.com [~]# find . -maxdepth 2 -type f ! -name ‘*.txt’ -size +1G -perm 0444 ! -user ${USER}
  1046. This find commands is locating starting from
  1047. current directory and the sub folder and the sub folder of the subfolder for all files NOT ending with .txt and not have more than 1G disk space, and not have the permission 444 and not user
  1048. Incorrect. The size must be greater than 1gb
  1049. ========================================================
  1050.  
  1051. Output designated as errors. stderr
  1052.  
  1053. Non-error output. stdout
  1054.  
  1055. Input received from hardware interrupts (peripherals) or other programs. stdin
  1056. ========================================================
  1057. The correct answer is:
  1058.  
  1059. Redirecting only stderr to /dev/null –
  1060. 2>/dev/null
  1061.  
  1062. Appending stdout and stderr to a log file –
  1063. &>> output.txt,
  1064.  
  1065. Truncating stdout and stderr to a log file –
  1066. &> output.txt,
  1067.  
  1068. Redirecting only stdout to /dev/null –
  1069. 1>/dev/null
  1070. ========================================================
  1071. Command executed in a pipeline are executed in the parent PID and environment.
  1072. FALSE
  1073. ========================================================
  1074.  
  1075. Explain why there is a difference in output of these two commands:
  1076. root@rstraining.lantstic.com [~]# lynx --dump http://galtse.cx|grep -co '[\.?,\-]$'
  1077. 496
  1078. root@rstraining.lantstic.com [~]# lynx --dump http://galtse.cx|grep -co '[\.?,\-].$'
  1079. 15
  1080.  
  1081.  
  1082. The first one is just grepping for anything that matches with all the special characters specified.
  1083. The second one is grepping for anything that have to match with TWO special characters specified together
  1084. ========================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement