[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

[Git] Git diff without comparaison file mode

Issue when execute git diff command on a file and got :

diff --git a/your_file b/your_file
old mode 100644
new mode 100755

If you see this, it is because the permissions of your_file has changed.
For information, the unix file permission mode(644=rw_r__r__ which means read and write for user, only read for group and only read for others, the same for 755=rwxrw_rw_, x for executable).

To disable the comparaison of file mode, the command to execute :

git config core.filemode false

[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.

[Linux command] Tar – Extract only the contents of an archived directory

 

As said the subject, we want to extract only the contents of a tar file to a specific folder.

I have my tar : myTarFolder.tar, my destination folder : destFolder

The structure of my tar file :

Image 10

If we use only the tar command, we will get :

tar -xf myTarFolder.tar -C destFolder 

Image 11

If we want to get something like this :

The option that we have to use is :

  1. –strip-components=NUMBER : strip NUMBER leading components from file names on extraction

An example of the command :

tar -xf myTarFolder.tar --strip-components=1 -C destFolder

The result :

Image 12

[ubuntu] WARNING gnome-keyring in lubuntu

If you have met the same problem, this maybe help.

Background :

I installed phpunit in my ubuntu 12.04(the os is lubuntu), the installation passed quite well, but when I launch the command in termianl, I got always this warning :

$ phpunit --version
WARNING: gnome-keyring:: couldn't connect to: /tmp/keyring-xAGrk8/pkcs11: No such file or directory
PHPUnit 4.4.5 by Sebastian Bergmann.

Continue reading