find MyFolder
find . -name "myFile.txt"
find
* - any number of characters
? - one character
[] - any of the characters inside the brackets
find . -name "*.html" # anything that ends with .html
find . -name "*.???" # anything that ends with a three letter file extension like .txt or .css
find . -name "[fts]*" # anything that starts with the letter f t or s
find . -name "*main*" # anything that has the text main somewhere in the filename
sudo apt install mlocate
echo "Lisa
Mark
Elie
Beth
Tim
Elizabeth
Tom
Matt
Liza
Janey
Jane
Shana" >> people.txt
cat people.txt | grep Tim
grep -i "elie" people.txt # case insensitive
grep -iw "beth" people.txt # case insensitive + full world
grep -v "Beth" people.txt # everything but
grep -c "Jane" people.txt # count
grep -ni "Jane" people.txt # print with line numbers
grep -wc "...." people.txt
grep -wc "T.*" people.txt
grep -wc "[LME].*" people.txt
grep -wc "[^T].*" people.txt
The real power of grep shines when using regular expressions
echo "Lisa
Mark
Elie
Beth
Tim
Elizabeth
Tom
Matt
Liza
Janey
Jane
Shana" >> people.txt
grep "T" people.txt
grep "....." people.txt
grep -v "e" people.txt
# Thanks @delhaj ❤️
echo "Issam,Pokemon, 5
Marta,Extreme sports, 2
Dhirar,SublimeText, 1
JC,Stargate, 7
Wenting,Disney world, 6
Shuting,Bag Raiders, 3
Stephanie,Wolves, 4
Sebastien,Naruto, 8
Trung,CSGO, 10
Rahul,GOT, 9" > best_people_ever.txt
cat best_people_ever.txt | cut -d ',' -f 1 # Issam, Marta...
cat best_people_ever.txt | cut -d ',' -f 2 # Pokemon, Extreme sports...
Used to cut lines based on a delimiter or a number of characters
echo "Issam,Pokemon, 5
Marta,Extreme sports, 2
Dhirar,SublimeText, 1
JC,Stargate, 7
Wenting,Disney world, 6
Shuting,Bag Raiders, 3
Stephanie,Wolves, 4
Sebastien,Naruto, 8
Trung,CSGO, 10
Rahul,GOT, 9" > best_people_ever.txt
cat best_people_ever.txt | sort -t ',' -k 1 # sort names
cat best_people_ever.txt | sort -t ',' -k 3 # sort by number ⚠️
Used to sort lines based
echo "Issam,Pokemon, 5
Marta,Extreme sports, 2
Dhirar,SublimeText, 1
JC,Stargate, 7
Wenting,Disney world, 6
Shuting,Bag Raiders, 3
Stephanie,Wolves, 4
Sebastien,Naruto, 8
Trung,CSGO, 10
Rahul,GOT, 9" > best_people_ever.txt
# are these two lines below equivalent ?
cat best_people_ever.txt | sort -t ',' -k 3 -g | head -n 2
cat best_people_ever.txt | sort -t ',' -k 3 -g -r | tail -n 2
head -> output first lines of a file
tail -> output last lines of a file
echo "Issam,Pokemon, 5
Marta,Extreme sports, 2
Dhirar,SublimeText, 1
JC,Stargate, 7
Wenting,Disney world, 6
Shuting,Bag Raiders, 3
Stephanie,Wolves, 4
Sebastien,Naruto, 8
Trung,CSGO, 10
Rahul,GOT, 9" > best_people_ever.txt
Stream EDitor or Sed works as follows:
Often used to search and replace or substitute, the basic form is the following
sed -e [line number or regular expression]s/regular-expression/replacement/[flags] text
echo "I have three dogs and two cats" > animals.txt
sed -e 's/dog/cat/g' -e 's/cat/elephant/g' animals.txt --debug
sed -e 's@dog@cat@g' -e 's@cat@elephant@g' animals.txt # equivalent
n replace nth instance of pattern with replacement
g replace all instances of pattern with replacement
echo "line 1 (one)
line 2 (two)
line 3 (three)" > file.txt
sed -e '1,2d' file.txt
sed -e '3d' file.txt
sed -e '1,2s/line/LINE/' -e '/^line/'d file.txt
sed -e '1,2s/line/LINE/' -e '/^line/!'d file.txt
sed -e [line number or regular expression]d text
echo "hello
this text is wiped out
Wiped out
hello (also wiped out)
WiPed out TOO!
goodbye
(1) This text is not deleted
(2) neither is this ... ( goodbye )
(3) neither is this
hello
but this is
and so is this
and unless we find another g**dbye
every line to the end of the file gets deleted" > text.txt
sed -e '/hello/,/goodbye/d' text.txt
sed -e [address1, address2]d text
#!/bin/sh
X='word1\|word2\|word3\|word4\|word5'
sed -e "
/$X/!d
/$X/{
s/\($X\).*/&/
s/.*\($X\)/&/
q
}" $1
Sed can do much more with the use of subroutines
& refers to the first match (sometimes people use \1)
q forces the end of the evaluation
echo "Pour jouer au tennis de tableen 2020, il faut:
1 table de tennis de table
2 raquettes
3 balles de ping pong
et de la bonne motivation !" > ping_pong.txt
-> 1 table de tennis de table !!!
-> 2 raquettes !!!
-> 3 balles de ping pong !!!
I want this !
sed -e '/^[0-9].*/!d' -e 's/^[0-9].*/-> & !!!/g' ping_pong.txt
And you were thinking sed was powerful ?
Awk is almost a programming language in itself. It can do any kind of text manipulation you can think of.
echo "hey, how are you ?" | awk 'BEGIN{printf"---|Start|--\n"} {print} END{printf"---|End|---\n"}'
echo "hey, how are you ?
good and you" | awk 'BEGIN{printf"---|Start|--\n"} {sub(/you/ ,"we"); print} END{printf"---|End|---\n"}'
xargs command is used to build an execution pipeline from standard input.
echo 'one two three' | xargs mkdir
ls # one two three
find *.txt | xargs grep '1'
echo "Issam-Hammi-pizza
Rahul-Gakhar-vegetables
Wenting-Bao-steaks
Sebastien-Dubuc-budda bowl
Dhirar-Elhaj-tacos" > food.txt
echo "1>>>>2
2>>>>3
3>>>>4
4>>>>5" > numbers.txt