[Bash shell] Find something in some files

Command “find” is one of the most powerful command in linux, here is an exmaple that allows list the files which contain something.

find . -name \*.[type of your files] -exec grep -q "[text that you want to search]" "{}" \; -print

For example, I have two shell files in my folder :

 
$ ll
drwxrwxr-x 3 bo bo 4096 juin 13 14:56 ./
drwxr-xr-x 16 bo bo 4096 juin 13 14:04 ../
-rwxrwxrwx 1 bo bo 2554 juin 12 18:05 first.sh*
-rwxrwxrwx 1 bo bo 1283 juin 12 16:10 second.sh*

And I want to find the string “spark” in these files

$ find . -name \*.sh -exec grep -q "spark" "{}" \; -print

And I got :

./second.sh
Advertisements

[Bash shell] Display the output with color

In bash shell, we have multiple ways to display the output, but do u know how to do this with color ?
Like this :
Image 8

It’s not so hard to do, the only thing we need to understand is  : ANSI escape code

An example of correspondence between the code and the color :

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

The example in bash shell

#!/bin/bash

NC='\033[0m' 		# No color
RED='\033[0;31m'	# red
PURPLE='\033[0;35m'	# purple

printf "My ${PURPLE}name${NC} is ${RED}BO${NC} \n"

Do this in shell, and launch your script, you will get the same from the image in the beginning of this article.

[Bash] Custom bash prompt — PS1

System : Ubuntu

Tool : Terminal (bash)

Prompt : PS1

Note :  By default, we have PS1, PS2, PS3 and PS4.

  • PS1 – The value of this parameter is expanded and used as the primary prompt string. The default value is \s-\v\$ .
  • PS2 – The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is >
  • PS3 – The value of this parameter is used as the prompt for the select command
  • PS4 – The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is +

To change the value of PS1, you have all regular expression below :

  • \a : an ASCII bell character (07)
  • \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”)
  • \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
  • \e : an ASCII escape character (033)
  • \h : the hostname up to the first ‘.’
  • \H : the hostname
  • \j : the number of jobs currently managed by the shell
  • \l : the basename of the shell’s terminal device name
  • \n : newline
  • \r : carriage return
  • \s : the name of the shell, the basename of $0 (the portion following the final slash)
  • \t : the current time in 24-hour HH:MM:SS format
  • \T : the current time in 12-hour HH:MM:SS format
  • \@ : the current time in 12-hour am/pm format
  • \A : the current time in 24-hour HH:MM format
  • \u : the username of the current user
  • \v : the version of bash (e.g., 2.00)
  • \V : the release of bash, version + patch level (e.g., 2.00.0)
  • \w : the current working directory, with $HOME abbreviated with a tilde
  • \W : the basename of the current working directory, with $HOME abbreviated with a tilde
  • \! : the history number of this command
  • \# : the command number of this command
  • \$ : if the effective UID is 0, a #, otherwise a $
  • \nnn : the character corresponding to the octal number nnn
  • \\ : a backslash
  • \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • \] : end a sequence of non-printing characters

Example :

echo $PS1

Output :
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

bo@bo-OptiPlex-740-Enhanced:~$ PS1='\u@\w\a$ '
bo@~$