sudo lsof -i :80 | awk 'NR>1 {print $2}' | xargs sudo kill
Liste les processus écoutant sur le port 80, récupéré le "pid" avec awk, il prend toutes les lignes sauf la première, et récupéré la colonne après 2 tabulations. Puis kill les "pid" listés.
lsof -i :80
# List every process listening on port 80
lsof -i :http
# Do the same
awk 'NR>1 {print $2}'
# Takes every lines except the first one.
# print the tabulation number 2 (second column)
[...] | xargs [...]
# xargs applies the following command to the arguments, which is `kill` in this case.
grep -lR "/var/www/html" | xargs sed -i -e "s#/var/www/html#/var/sites#g"
Replace "var/www/ in every files containing "/var/www/html"
grep -lR "/var/www/html"
# grep -lR for recursevely searching & gives the files name.
[...] | xargs [...]
# xargs applies the following command to the arguments
sed -i -e "s#/var/www/html#/var/sites#g"
# -i stands for apply the modification
# -e pass command/script/element to the sed command.
# s replace
# g global, apply the modification several times to the same file if the matching string is detected several times.
# Adding the `#` permits to do not escape the `/` character.
find . -name "*.sh" | cpio -o --format=tar > compil.tar
find . -name "*.sh"
# Search from the current directory all .sh files
cpio -o --format=tar > compil.tar
# Deal with archives (Give some files to eat to cpio, it will archive it in the compil.tar file)
tar -tf compil.tar
# -tf get the archive file and read/list files into it.
python3 -m http.server 9999
# open web server on the 9999 port
curl http://example.com:9999/.passwd
wget http://example.com:9999/.passwd
Useful to know when to use alias with most used commands
cat ~/.bash_history | awk `{a[$1" "$2]++}END{for(i in a){print a[i] " " i}}` | sort -nr | head
# {a[$1]++} create a table key-value with the string of the 1st column as key and the number of the string as value.
# in a list with 503 times the command "ls", it would have been "ls-503 times"
# {a[$1" "$2]++} does the same with the value of the column 1 & 2
#
# for(i in a) For each entry in the table, do:
# {print a[i] " " i} Print ls - 504
#
# sort -nr : sort number and reverse
#
# head : takes first 10 entries
Or
cat ~/.bash_history | sort | uniq -c | sort -nr | head
Which works differently and gives different results
sudo apt-get install cloc
install-cloc.sh
> cloc archive.tar.gz
ps aux | sort -nrk 3,3 | head -n 10
verbose way
ps -eo pcpu,pid,user,args --no-headers | \
sort -t. -nk1,2 -k4,4 -r | \
head -n 10 | \
awk '{print "\t"$1"\t"$2"\t"$3"\t "$4}'
# ps
# -e : to have the whole processes
# -o : to filter some columns (pcpu,pid,user,args)
Cleaner way
TODO : explications
smth.... | awk `{print $1}`
# Print first column of "smth"
smth.... | awk `{print $1" "$2}`
# Print first & second column of "smth"
smth.... | awk `NR>1 {print $1}`
# NR>1 select every line with id more than 1 (which means, not the 1st one).
# and print the first tab.
grep ...
-n : Show the matching number line
--color: highlight the expression
-i : Performing case-insensitive grep searches
-l : list files
grep "something" *.html
Search "something" in all html files
grep -r "something" *
Search recursively in all files
Using -R will search though directories symbolically linked
tree
show directories and files as a tree.
type [...]
type ls
# indicate if the command is intern or not.
eog [path image]
Open image with image viewer lsof : liste les processus écoutant sur un port ou un service
Xavki sysad