Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Wednesday, January 14, 2015

Bash - Find substring in a string

Very simple solution to find substring in a string, make script sub.sh:
#!/bin/bash
txt="This is a text"
substr="text"
if [[ $txt == *$substr* ]]
then
echo "String [$txt] contains substring [$substr]"
else
echo "String [$txt] doesn't contains substring [$substr]"
fi

Variable txt is a string which should (or not) contains string from variable substr.

Output:
$ ./sub.sh
String [This is a text] contains substring [text]

Change variable substr:
substr="t1xt"

Output:
$ ./sub.sh
String [This is a text] doesn't contains substring [t1xt]

Tuesday, January 13, 2015

Linux - Search for empty directories and files

To search for empty directories type:
$ find . -empty -type d
To delete empty directories:
$ find . -empty -type d -delete

If enter -type f instead of -type d, this command will search for empty files:
$ find . -empty -type f
to delete empty files:
$ find . -empty -type f -delete