- Bash
reference
manual (GNU)
- Lists of commands (wp:
List
of UNIX commands):
- asyncronous: &
- sequential: ;
- AND: &&
- OR: ||
- Functions
- Job control:
- Arrays
- 6.7
Arrays
-
type
|
declaration
|
creation of elements
|
remove all elements
|
remove single element
|
length
|
print keys
|
print all values
|
print keys and values
|
print single value
|
print range of values
|
indexed
|
declare -a my_array
local
-a my_array
readonly -a my_array
my_array=()
|
my_array[0]=tata
my_array[1]=tete
my_array=(tata tete)
my_array+=(titi)
|
unset
my_array
|
unset
my_array[1] |
${#my_array[1]}
${#my_array[@]}
${#my_array[*]}
|
${!my_array[@]}
${!my_array[*]}
|
${my_array[@]}
${my_array[*]}
printf "%s\n" "${my_array[@]}"
for value in "${my_array[@]}"
do
echo $value
done
- all values separated by specified char (e.g. a
comma):
- echo
$(IFS=, ; echo "${my_array[*]}")
|
|
${my_array}
${my_array[0]}
${my_array[1]}
|
- first element:
- last element:
- from second element to the end:
- 3 elements, starting from the second:
- last 2 elements
- all elements but the last 2:
|
associative
|
declare -A my_array
local -A my_array
readonly -a my_array
|
my_array[primer]=titi
my_array[segon]=toto
|
|
|
for key in
"${!my_array[@]}"
do
echo "${key}:
${my_array[$key]}"
done
|
${my_array[primer]}
${my_array[segon]}
|
|
- Append
my_array=()
my_array+=('tata')
my_array+=('tete')
- Remove
- Sorting
- Split string:
- Join array with dots:
echo $(IFS='.';echo "${my_array[*]}";IFS=$'')
- Intersection
- Unique
- CPU/memory activity
ps
- fields:
- VSZ: virtual_size = physical_memory + swap_memory
- RSS: physical_memory
ps --help
ps -edalf
- show threads (like CTRL-H in htop):
- by command (http), and sort by RSS
ps -yl -C httpd - -sort:rss
- sum the memory ()
ps aux | awk '{sum+=$4}; END {print sum}'
pgrep
- Instal·lació / Installation
- look for a regular expression
- list full command line
- processes with specified parent:
- kill
pgrep "my_process.sh" | xargs kill -9
pgrep -f "some expression" | xargs kill -9
- processes=$(pgrep
-f ...)
ps ${processes}
kill -15 ${processes}
pstree
- show also pid:
- show ancestors
- highlight ancestors
- ...
top
atop
- One-stop
performance analysis using atop
- logs of usage (historic)
- /etc/sysconfig/atop
# interval (default 10
minutes)
INTERVAL=600
- /usr/share/atop/atop.daily
# delete logfiles
older than four
weeks
# start a child shell that activates another
child shell in
# the background to avoid a zombie
#
( (sleep 3; find $LOGPATH -name 'atop_*'
-mtime +28
-exec rm {} \;)& )
sudo systemctl enable atop
sudo systemctl start atop
atopsar
atop -r /var/log/atop/atop_yyyymmdd
- search for peaks
- of memory (minimum free memory):
for f in /var/log/atop/atop_*; do
atopsar -r $f -m -R 1 | sort -b -h -k 3 |
head; done
- of cpu (maximum all cpu usage):
for f in /var/log/atop/atop_*; do
echo $f; atopsar -r $f -c -R 1 | grep all
| sort -r -b -h -k 3 | head; done
- prepend every line with analysis date
- graphs with
gnuplot
- cpu usage
atopsar -r atop_20210914 -c | grep
all >cpu.txt
- cpu.plot
set timefmt
"%H:%M:%S"
set xtics rotate
plot "cpu.txt" using 1:3 with lines
- free memory
- single day:
atopsar -r atop_20210914 -m |
awk 'BEGIN {date_stamp=""; }
/analysis date: /{date_stamp=$4;}
/^[0-9]/ {print date_stamp, $0;}'
>free_memory.txt
- several days:
for f in atop_*; do atopsar
-r $f -m | awk
'BEGIN {date_stamp=""; } /analysis
date: /{date_stamp=$4;} /^[0-9]/
{print date_stamp, $0;}'
>>free_memory.txt; done
- free_memory.plot
set timefmt " %Y/%m/%d
%H:%M:%S "
set xtics rotate
plot "free_memory.txt" using 1:4 with
lines
- ...
htop
vmstat 1
- System Activity (KDE)
- Definit a Arranjament del sistema -> Dreceres i
gestos -> Dreceres de teclat globals -> Interfície
d'execució d'ordres -> Mostra l'activitat del sistema
- Activitat de fitxers / File
activity
lsof (list open files)
iotop (access to disk)
- Monitoratge / Monitoring
- Activitat
de logs / Logs activity
- Xarxa
/ Network
- Network
activity
- ipcalc
- get CIDR from a given expression
eval $(ipcalc -np 172.31.37.147/20); echo
$NETWORK/$PREFIX
- get local CIDR from ip command (only first occurence)
cidr=$(ip -o address | awk
'$2 !~ /lo/ && $3 ~ /^inet$/ {print $4;
exit;}')
eval $(ipcalc -np $cidr); echo
$NETWORK/$PREFIX
- get ipv4 first address
ipv4_address =$(ip
-o -f inet address | awk '$2 !~ /lo/
{gsub(/\/24/,"",$4);print $4; exit;}')
- nc
- conversion int/ipv4
function ipv4_to_int
() {
# convert 1.2.3.4 to 1*256^3 +
2*256^2 + 3*256^1 + 4*256^0
local ipv4=$1
ipv4_array=(${ipv4//./ })
ipv4_hex_array=()
ipv4_without_dots=''
for value in ${ipv4_array[@]}
do
hex_value=$(printf '%02x' ${value})
ipv4_without_dots="${ipv4_without_dots}${hex_value}"
ipv4_hex_array+=(${hex_value})
done
# convert base 16 to base 10
ipv4_10=$((16#$ipv4_without_dots))
echo "$ipv4_10"
return 0
}
function int_to_ipv4
() {
# convert 1*256^3 + 2*256^2 +
3*256^1 + 4*256^0 to 1.2.3.4
value_10=$1
# convert base 10 to base 16
value_16=$(printf '%08x'
${value_10})
length=${#value_16}
if (( length > 8))
then
echo
"ERROR: provided value ($value_10, 0x$value_16) is too
large to be converted to ipv4"
exit 1
fi
local offset=2
local ipv4=""
while (( offset < length+2 ))
do
substring_16=${value_16: -${offset}:2}
substring_10=$((16#$substring_16))
if ((
${#ipv4} > 0 ))
then
ipv4="${substring_10}.${ipv4}"
else
ipv4=${substring_10}
fi
((
offset+=2 ))
done
echo "$ipv4"
return 0
}
address_start=224.0.0.1
address_start_int=$(ipv4_to_int ${address_start})
echo "${address_start} ${address_start_int}"
address_stop_int=$(( address_start_int + 2 ))
address_stop=$(int_to_ipv4 ${address_stop_int})
echo "${address_stop} ${address_stop_int}"
- Utilització de disc /
Disk usage
- Substitució
/
Replacement
- Elimina el CR
sed -e "s!\\r!!g" toto.txt
- Errors in English:
- File
descriptors:
stdin, stdout, stderr, ...
# lsof +f g -ap $BASHPID
-d 0,1,2
-
number
|
name
|
redirection
|
duplicate
|
0
|
stdin
|
0<from_file
<from_file
|
|
|
1
|
stdout
|
1>to_file
>to_file
|
|
|
2
|
stderr
|
2>to_file
|
2>&1
|
the descriptor 2 will
go where the descriptor 1 goes. Order
is
important (evaluated from left to right):
command >file 2>&1 will
redirect
- first: stdout/1 (implicitly specified) to
file
- second: stderr/2 to where stdout/1 is
pointing (file)
command 2>&1 >file will
first redirect
- first: stderr/2 where stdout/1 is pointing
(/dev/tty0)
- second: stdout/1 (implicitly specified) to
file (stderr will still point to /dev/tty0)
|
3
|
your_name
|
|
|
|
...
|
|
|
|
|
- Redireccionament
/ Redirection
- Pipes
-
type
|
direction
|
command
|
example
|
description
|
info
|
no named
|
unidirectional
|
|
|
proc1.sh |
proc2.sh
|
connects stdout
of proc1.sh to stdin of proc2.sh
|
|
named
|
bidirectional
|
mkfifo
|
mkfifo
my_fifo
proc1.sh 1>my_fifo &
proc2.sh 0<my_fifo
|
connects stdout
of proc1.sh to stdin of proc2.sh |
|
network
|
bidirectional
|
nc
|
|
|
|
- Pipeline
(Unix) (wp)
- Bash
pipe
handling
- Ring
buffers
with Bash script
- Make
a
pipe conditional on non-empty return
- closing
mkfifo
- exemple / example:
mkfifo toto
- on console 1 (it will wait until other process
read all the content of the pipe):
- on console 2 (it will wait until other process
writes to the pipe, and will end when EOF is
found):
cat <toto
- to avoid closing pipe, use one of the
following
- Power Up
Linux GUI Apps (Linux Magazine)
- Camins i noms de fitxers /
Paths and file names
- URL
parsing
- Python
paths
- Names:
my_path=./dir1/dir2/toto.mp4
#
./dir1/dir2/toto.mp4
my_dirname=$(dirname $my_path)
# ./dir1/dir2
my_rel_dirname=${my_dirname#*\./} # dir1/dir2
my_basename=$(basename $my_path) #
toto.mp4
my_name=${my_basename%.*}
#
toto
my_extension=${my_basename##*.} # mp4
my_rel_path=${my_path#*\./}
#
dir1/dir2/toto.mp4
cd /tmp; mkdir -p dir1/dir2; touch
dir1/dir2/toto.mp4
my_abs_path=$(realpath $my_path) #
/tmp/dir1/dir2/toto.mp4
my_abs_path=$(eval realpath $my_path) # (use eval
when variable contains quotes)
- Get the present
directory:
- Get the directory
from a filename:
- Get the abolute path
of a file:
- Get the name of
the file (without directory):
- Remove file extension:
- Remove file extension
and add a new one ".txt": (Shell
parameter
expansion)(FAQ)
- Get the file extension:
(Shell
parameter
expansion)(FAQ)
extension=${file##*.}
- to get an empty extension if the file has none:
extension_with_point=$([[ "$file" = *.* ]]
&& echo ".${file##*.}" || echo '')
- Recursively create the needed directories:
- fitxers ini / ini files
- Nginx
config
- augtool (Augeas)
- No tracta qualsevol fitxer: només gestiona els fitxers
inclosos per les lenses, sempre i quan siguin correctes
- Instal·lació / Installation
- Lenses (modules)
- Ús / Usage
export AUGEAS_ROOT=/tmp/augeas-sandbox
augtool -b
- Errors
- Tracking
down
errors
augtool errors (only from version
...?)
- augtool -b
- match /augeas//error
- ls /augeas/files/etc/nginx/nginx.conf/error
- ...
- crudini
crudini --get toto.in my_section my_key
crudini --set toto.in my_section my_key my_value
- Problems
- Whitespace
around
= #33
- Workaround
- put your_key=foo. E.g.:
sed -i -e '/^#host-name/ s/^#//'
avahi-daemon.conf
- crudini will preserve no spaces around
'=':
crudini --set avahi-daemon.conf
server host-name my_hostname
- Esborrar línies en blanc
/ Removing blank lines
(*):
sed '/^$/d' input.txt > output.txt
grep
-v '^$' input.txt > output.txt
- Gotchas
- Bash completion
urpmi bash-completion
- files:
- rules:
- /etc/sysconfig/bash-completion
- ~/.bash_completion (copied from
/etc/skel/.bash_completion)
/etc/ inputrc
- Variable settings:
set variable value
- Key bindings:
keyname: function-name or macro
"keyseq": function-name or macro
-
"keyseq" |
tecla |
|
|
|
|
"\e[5~" |
Re Pág / PgDown |
"\e[6~" |
Av Pág / PgUp |
|
|
"\e[A"
|
up arrow
|
"\e[B" |
down arrow |
- ...
- Història / History
- Per a poder fer anar RePag/AvPag (*)
(necessari per a Ubuntu)
- /etc/inputrc
"\e[5~":
history-search-backward
"\e[6~": history-search-forward
- Dates
- Comunicació
sèrie / Serial communication
- Linux
Serial
Console HOWTO
- 5
Linux / Unix Commands For Connecting To The Serial Console
- discover
-
|
lsusb
|
dmesg | egrep --color 'serial|tty'
|
SparkFun
FTDI Basic Breakout - 5V |
ID 0403:6001 Future Technology Devices
International, Ltd FT232 Serial (UART) IC |
usb 1-1.2: FTDI USB Serial Device
converter now attached to ttyUSB0
|
YaMoRC YD9100 |
ID 0403:6015 Future Technology Devices
International, Ltd Bridge(I2C/SPI/UART/FIFO)
|
FTDI USB Serial Device converter now
attached to ttyUSB0
|
... |
|
|
- configure
- print configuration
setserial -a /dev/ttyUSB0
setserial -G /dev/ttyUSB0
- communicate
-
command |
usage |
screen |
screen /dev/ttyUSB0 115200 |
cu |
cu -l /dev/ttyUSB0 -s 115200 |
minicom |
|
putty |
|
tip |
tip -115200 /dev/ttyUSB0 |
- aleatori / random
- alternatives
- at
- instal·lació / installation
- daemon
sudo systemctl enable atd
sudo systemctl start atd
- afegeix una tasca / add a job:
echo "ls -l >>/tmp/toto.txt" | at 00:12
2024-03-31
- timedate in [[CC]YY]MMDDhhmm[.ss] format:
echo "ls -l >>/tmp/toto.txt" | at -t
202403310012
- llista de totes les tasques de l'usuari / list of jobs for
present users (for all users if run as root)
- mostra els detalls d'una tasca
- esborra una tasca
- atop
- awk
- The
GNU
Awk User’s Guide
awk [options] 'pattern1 [&& pattern1b]
{action1a;action1b}[;] pattern2 {action2}[;]
pattern_start,pattern_stop {action3} ...'
- options:
-
option
|
description
|
-f
<filename>
|
read program
from file
|
|
|
- pattern (Escape
sequences):
-
pattern
|
description
|
1
|
(default)
true |
0
|
false
|
/regular_expression/ |
matching
regular expression (whole line)
|
$0 ~ /regular_expression/ |
$3 ~ /regular_expression/ |
third field
matching regular expression
|
!~
/regular_expression/ |
not matching
regular expression |
$3 !~ /regular_expression/ |
third field
not matching regular expression |
BEGIN |
at the
beginning of the file
|
END |
at the end of
the file
|
...
|
|
- actions (7.3
Actions):
- parameters
- RS: register separator
- FS: field separator (what a field is not)
- separator is "\ "
awk -F'\\\\ '
awk 'BEGIN {FS=" \\\\
" } ... '
- FPAT: (what a field is)
- ORS: output register separator
- OFS: output field separator
- you may need to force
{$1=$1}
awk 'BEGIN {OFS=" --" }
{$1=$1} {print
$0} '
- pattern combination:
&&, ||
$2 !~ /lo/ &&
$3 ~ /^inet$/
- example:
ip -o address | awk '$2 !~ /lo/
&& $3 ~ /^inet$/ {print $4}'
- pass shell variables
awk -v pat="abc${my_var}" '$0 ~ pat {print
$2}'
boundary=--
echo "$input" | awk -v b=$boundary
'BEGIN{RS=b"\n";ORS="...\n";FS="\n"} {print $1}'
awk -v pattern="abc${my_var}" '$1 ~ pattern
{print $2}'
- Using shell
variables:
- 7.2
Using
Shell Variables in Programs
- Option 1:
IMAGE_NAME=toto
IMAGE_ID=$(glance image-list | awk
"/$IMAGE_NAME/"'{ print $2 }')
- Option 2:
IMAGE_NAME=toto
IMAGE_ID=$(glance image-list | awk -v
pat="$IMAGE_NAME" '$0 ~ pat { print $2 }')
- Rang / Range:
- Large number of input files
- ...
- Gawk:
Effective AWK Programming ()
- Awk - A
tutorial and introduction (UNIX
Grymoire)
- awk (Eric
Pement)
- Add
a
column and match two files
- Reimplementation
of
cut in AWK
- Exemples / Examples
- sdp_split.sh
- nginx
logs
- AWS ALB
logs
- Exemples
amb dvbsnoop / dvbsnoop examples
- Exemples
amb OpenSSL / OpenSSL examples
- els espais dins de les cometes dobles
no delimiten camps:
gawk ... FPAT
...
- exemples
- AWS
S3 logs
- first="first value" second="second value"
'BEGIN {FPAT="([^=]+=\"[^\"]+\")"};
{print $2}'
- first,"se cond",third
'BEGIN
{FPAT="([^,]+)|(\"[^\"]+\")"}; {print $2}'
- first "se cond" third
'BEGIN {FPAT="([^
]+)|(\"[^\"]+\")"}; {print $2}'
- [fi rst] "se cond" third
'BEGIN {FPAT="([^
]+)|(\"[^\"]+\")|(\\[[^\\[\\]]+\\])"};
{print $2}'
- Escriu les línies numerades del primer fitxer (
NR==FNR )
i després les del segon:
awk 'NR==FNR{print "first "FNR": "
$0;next}{print "second "FNR": " $0}' file1 file2
- Obté l'enèsima línia
(p.ex. 15a) d'un fitxer / Get the nth (e.g. 15th) line
of a file
awk 'NR==15' toto.txt
sed -n '15p' toto.txt
- Escriu els valors de la segona columna
del fitxer, on les columnes estan separades pel caràcter
'=':
awk -F= '{print $2}' toto.txt
- Mostra el segon camp
($2) ("xx") de la línia on el primer camp ($1) contingui
(~) l'expressió
regular "ID_LENGTH" del fitxer .toto.txt
(ID_LENGTH=xx)
awk -F= '$1 ~ /ID_LENGTH/ {print $2}'
toto.txt
- ...
awk -F ":" '{print $1 | "sort" }'
/etc/passwd
- Fes un grep en un altre fitxer de cadascuna de les
cadenes trobades:
awk -F, '{print $2}' toto.csv | xargs -n 1 -I % grep %
tata.txt
- De la línia que conté la paraula "aquesta", mostra la
tercera columna, però sense cap parèntesis / Replace
parenthesis to nothing:
awk '/aquesta/{gsub(/[()]/,"",$3);print
$3}}' toto.txt
- De la línia (on els
camps estan separats pel caràcter =) que conté la
paraula aws_access_key_id (aws_secret_access_key),
elimina els espais del segon camp i el mostra:
awk -F= '/aws_access_key_id/{gsub(/
/,"",$2);print $2}' /etc/aws/credentials
awk -F= '/aws_secret_access_key/{gsub(/
/,"",$2);print $2}' /etc/aws/credentials
- Elimina les cometes simples / Remove
sinlge quotes:
awk '/.../{gsub(/['\'']/,"",$2);print
$2}'
awk '/.../{gsub(/[\x27]/,"",$2);print
$2}'
- Elimina les cometes dobles / Remove
double quotes:
- Elimina la primera i segona columnes / Remove first
and second columns:
awk '!($1="") !($2="")' toto.txt
awk '!($1="") !($2=""){print}' toto.txt
- Elimina les línies que comencen per "/dev"
awk '!/^\/dev/' toto_fstab.txt
- Extreu el text pla d'un fitxer de subtítols
vtt:
awk -F'>' '/^<c/
{gsub("</c","",$2);print $2}' toto.vtt
>toto.txt
- Blocs separats per una línia en concret
- Línies al voltant:
- Print
current,next,previous
line using awk,sed - BASH
- Print
next
few lines after pattern - awk
- Print the line immediately before a regex, but not
the line containing the regex:
awk '/regex/{print x};{x=$0}'
- Print the line immediately after a regex, but not
the line containing the regex (only the first
occurrence):
awk '/regex/{f=1;next}f{print;exit}'
toto.txt
- Print the line immediately after a regex, but not
the line containing the regex (all the occurrences):
awk '/regex/{f=1;next}f{print;f=0}'
toto.txt
- Awk:
print
all other columns but not $1, $2 and $3
- Print directory of files with name ending with
index.m3u8 or mp4 (split acts like basename; $(NF-2)
takes the third last column)
fragment_list=$(awk '/(index.m3u8|mp4)$/ {n=split($(NF-2),a,"/");
print a[n-1]}' $log_path)
- Esborra línies duplicades / Remove
duplicated lines
- Obté la suma de la segona columna:
awk '{sum+=$2}; END {print sum}'
- Find the funcion in fabfile.py
that contains a put of file mcd.service:
awk -v pat="put.*mcd.service" '/^def/
{function_name = $0;} $0 ~ pat {print
function_name $0}' fabfile.py
- HLS
- mostra les durades dels segments:
awk -F: '/^#EXTINF/ {gsub(/,/,"",$2);
print $2}' monovariant.m3u8
- suma les durades de tots els segments:
awk -F: '/^#EXTINF/ {gsub(/,/,"",$2);
sum += $2} END {print sum}' monovariant.m3u8
- mostra la suma parcial i el nom de cada segment:
- awk -F:
'/^#EXTINF/ {gsub(/,/,"",$2); sum += $2; print
$2 " " sum} /.ts/ {print $1} END {print sum}'
monovariant.m3u8
- troba el segment que conté el punt temporal
especificat:
awk -F: -v limit=3600 '/^#EXTINF/
{gsub(/,/,"",$2); sum += $2; if (sum>limit)
f=1;next} f {print $0; exit}' monovariant.m3u8
- Get listening port for process snowmix
netstat
-pnl --ip | awk '$7 ~ /snowmix$/
{gsub(/*.:/,"",$4);print $4}'
- get disk and mountpoint with a usage greater than a
threshold (adding 0 to $2 is necessary to convert it to
a number, to make sure that 17>2):
usage_threshold=12
df --local --output=source,ipcent,target | tail -n
+2 | awk -v usage_threshold=${usage_threshold}
'{gsub(/%/,"",$2)} $2+0 >= usage_threshold
{print $1 " " $3}'
- base64
bc
- case
- cat /
EOF
- to standard output:
- to a variable
my_var=$(cat <<EOF
...
EOF
)
- to a file
cat >file_name <<EOF
...
EOF
- cd
- va al directori anterior ($OLDPWD) / go to previous
directory:
- chmod
chmod -R +X dir_name (-R: recursive; +X:
executable/browseable only for directories)
- change mod of directories:
find /var/lib/mysql -type d | xargs chmod 700
- change mod of files:
find /var/lib/mysql -type f | xargs chmod 660
- recursive sticky
group / setgid (only directories)
find
/absolute/path/to/dir -type d -exec chmod 2770 {} +
- comm
- see also: uniq, join
- print 3 columns
- first column: only in first file
- second column: only in second file
- third column: in both files
- specified files must be sorted
comm f1_sorted,txt f2_sorted.txt
comm <(sort f1_unsorted.txt) <(sort f2 _unsorted .txt)
- only entries in f1 (remove columns 2 and 3):
comm -23 <(sort f1_unsorted.txt) <(sort f2 _unsorted .txt)
- only entries in f2 (remove columns 1 and 3):
comm -13 <(sort f1_unsorted.txt) <(sort f2 _unsorted .txt)
- only common entries (remove columns 1 and 2):
comm -12 <(sort f1_unsorted.txt) <(sort f2 _unsorted .txt)
- from variables instead of files:
- cp
- fes una còpia de seguretat / backup:
- filename expansion
- còpia amb excepcions / copy with exceptions:
rsync -a
--exclude='dir_to_exclude' source_path
destination_path
- crontab
- Cron
and crontab usage and example
- crontab guru
- camps / fields
-
m h dom mon dow command
-
- m: minut / minute (0-59)
- h: hora / hour (0-23)
- dom: dia del mes / day of month (1-31)
- mon: mes / month (1-12) (Jan-Dec)
- dow: dia de la setmana / day of week (0-6)
(Sun-Sat)
- Note: implicit AND, except
dom-dow ,
combined with implicit OR
- exemples / examples
-
- cada divendres a les 20:00 UTC / every Friday at
20:00 UTC
-
- cada dues hores, a dos quarts / every 2 hours, at
half past
-
- a les 12 del migdia i a les dotze de la nit / at
midnight and noon
-
- a les / at 1:30, 2:30, 3:30
-
- llistat / list
-
- edició / edition
-
[export
EDITOR=emacs]
crontab -e
- logs
-
/var/log/cron
- check mail
of the owner of the crontab
- Problemes / Problems
-
sudo: sorry, you must have a tty to run sudo
-
- afegir línia des de shell / add line from shell:
-
- How to create cronjob using
bash
/var/spool/cron/crontabs/$USER
crontab -l | { cat; echo "0 0 * * * some entry"; } | sort -u | crontab
-
- root
crontab:
sudo sh -c "crontab -l | {
cat; echo '0 4 * * * some entry'; } | sort -u | crontab
-"
- Linux
Execute
Cron Job After System Reboot
-
- Crontab
with
Fabric
- cu
- curl
- cut
(esborra parts d'una línia / remove parts of a line)
- get the second field when delimiter character is '_':
echo 'someletters_12345_moreleters.ext' | cut
-d'_' -f 2
12345
- date
-
command
|
output
|
notes
|
|
LANG=en_GB
|
LANG=en_US
|
LANG=ca_ES.UTF-8
|
|
date
|
Mon 20 Feb 12:07:54
CET 2017
|
Mon Feb 20 12:08:14
CET 2017
|
dl feb 20 12:08:27
CET 2017
|
see languages ()
|
date '+%c'
|
Mon 20 Feb 2017
12:06:06 CET
|
Mon 20 Feb 2017
12:06:18 PM CET
|
dl 20 feb 2017
12:00:12 CET |
see:
|
date '+%x'
|
20/02/17
|
02/20/2017
|
20/02/17
|
|
date -Iseconds
-u
date --iso-8601=seconds --utc
date '+%Y-%m-%dT%H:%M:%S%z' --utc |
2014-10-31T11:03:02+0000
|
|
date -Ins --utc
|
2016-06-30T11:51:20,083994321+0000
|
|
date
'+%Y-%m-%dT%H:%M:%S.%N%z' --utc |
2016-06-30T11:51:20.083994321+0000 |
NOT valid for default
date
format in DRF |
date
'+%Y-%m-%dT%H:%M:%S.%06NZ' --utc
|
2016-06-30T13:57:26.562720Z
|
valid for default date
format in DRF
|
date
'+%Y-%m-%dT%H:%M:%SZ' --utc
|
2015-11-10T16:28:56Z
|
|
date
'+%Y-%m-%dT%H%M%SZ' --utc
|
2015-11-10T162945Z
|
|
d ate
'+%Y%m%d_%H%M' |
20141031_1203
|
|
date +%s |
1414753430
|
seconds
|
date +%s%03N |
1414753430123 |
milliseconds
|
date +%s%06N |
1414753430123456 |
microseonds
|
date +%s%N |
1414753430123456789 |
nanoseconds
|
date -d
'2012-11-20 19:19:00' +%s
|
1353435540
|
seconds
|
date
--date=@1530780235
|
|
|
dj jul 5
10:43:55 CEST 2018
|
seconds from
01/01/1970
|
date --rfc-2822
date -R
|
Fri,
16 Sep 2016 09:34:17 +0200
|
used by curl -z ... / -H
'If-Modified-Since: ...'
|
- present date in ISO 8601 format (-I or --iso-8601), up to
seconds precision (-Iseconds), UTC (-u)
- present date in ISO 8601 format (-I or --iso-8601), up to
nanoseconds precision (-Ins), UTC (-u)
- present date in a given format:
- seconds between 1970-01-01 00:00:00 UTC and now (%s)
- milliseconds between 1970-01-01 00:00:00 UTC and now
- seconds between a given date and 1970-01-01 00:00:00 UTC:
date -d '2012-11-20 19:19:00' +%s
- Re:
find
out UTC in seconds since Jan 01 1900
date -d '2012-11-20 19:19:00' +2208988800+%s | bc
- dateutils
- timedate addition
now=$( date -Iseconds -u )
duration_seconds=10
end_date=$(date '+%Y-%m-%dT%H:%M:%SZ' --date="$now +
$duration_seconds seconds" -u)
now=$( date -Ins -u )
duration_seconds=10.5
end_date=$(date '+%Y-%m-%dT%H:%M:%S.%06NZ'
--date="$now + $duration_seconds seconds" -u)
now_ms=$( date +%s%03N
-u )
duration_ms=300
end_date_ms=$(( now_ms + duration_ms ) )
- time addition
- milliseconds
initial_time="00:01:00.100"
delta_seconds="45.678"
date -u -d "0 $(date -u -d "${initial_time}"
+"%s.%03N") sec + $(date -u -d @"${delta_seconds}"
+"%s.%03N") sec" +"%H:%M:%S.%03N"
- time difference
max_time= "09:22:01"
min_time="08:44:55"
date -u -d "0 $(date -u -d "$max_time" +"%s") sec
- $(date -u -d "$min_time" +"%s") sec" +"%H:%M:%S"
- microseconds precision:
max_time= "09:22:01.123456"
min_time="08:44:55.654321"
date -u -d "0 $(date -u -d "$max_time"
+"%s.%06N") sec - $(date -u -d "$min_time"
+"%s.%06N") sec" +"%H:%M:%S.%06N"
date -u -d @$(( $(date -u -d "$max_time" +"%s")
- $(date -u -d "$min_time" +"%s") )) +"%H:%M:%S"
- datetime difference
- How
to
calculate time difference in bash script?
- Find
difference
between two dates in bash
formatted output (integer precision)
min_date="2016-03-03T22:00:00Z"
max_date="2016-03-04T22:30:00Z"
date -d @$(( $(date -d "$max_date" +%s) - $(date
-d "$min_date" +%s) )) -u +'%H:%M:%S'
- output in seconds (integer precision)
min_date="2016-03-03T22:00:00Z"
max_date="2016-03-04T22:30:00Z"
#date -d @$(( $(date -d "$max_date" +%s) - $(date
-d "$min_date" +%s) )) -u +'%s'
delta= $(( $(date -d "$max_date" +%s)
- $(date -d "$min_date" +%s) ))
echo $delta
- output in seconds (nanosecond precision)
min_date="2016-03-03T22:00:00.123456789Z"
max_date="2016-03-04T22:30:00.987654321Z"
delta=$( echo $(date -d "$max_date" +%s.%N) -
$(date -d "$min_date" +%s.%N) | bc -l )
echo $delta
- time comparison
time_1="13:58:08.200"
time_2="15:58:08.200"
if [[ ${time_1} < ${time_2} ]]
then
echo "${time_1} <
${time_2}"
else
echo "${time_1}
>= ${time_2}"
fi
- date comparison
date_1="2017-11-01T13:58:08.200Z"
date_2="2017-11-01T14:58:08.000Z"
if [[ $date_1 < $date_2 ]]
then
echo "$date_1 < $date_2"
else
echo "$date_1 >= $date_2"
fi
- convert seconds to time
time_s=10.5
time=$(date -d@${time_s} -u +%H:%M:%S.%06N)
- convert time (hh:mm:ss.dd) to seconds:
time="00:01:02.30"
IFS=: read -r h m s <<<"$time"
time_s=$( echo "($h * 60 + $m) * 60 + $s" | bc -l)
- dd
- Tuning
dd block size
- remove the first 35 bytes from a file:
dd bs=35 skip=1 if=full_file.bin
of=stripped_file.bin
- from a raw file (e.g. yuv)
with lines of 1440 bytes, take the 100th and 400th lines:
dd bs=1440 skip=100 count=1 if=test.uyvy | od -v
-t u1
dd bs=1440 skip=400 count=1
if=test.uyvy | od -v -t u1
- image from an sd card to a file
(e.g. from a Raspberry Pi)
dd bs=4M if=/dev/mmcblk0 | gzip >
/tmp/raspbian.bin.gz
- create a file of 1MB containing zeros:
dd if=/dev/zero of=1M_zeros.dat bs=1M count=1
- create a file of 1MB containing random values:
dd if=/dev/urandom
of=1M_random.dat bs=1M count=1
- diff
- dig
- dmesg
- dmidecode
- Informació sobre el maquinari / Hardware information
- du
- Utilització de disc
/ Disk usage
- dump
od (octal dump)
xxd (hexadecimal dump)
- echo
- print variable content with a final \n:
- print variable content (preserve inner \n), with a final
\n:
- do not print final \n
- eval escaped chars
echo -e ...
- print character specified by its ascii value specified
in octal:
- old bash (<3.2):
- new bash (>=3.2)
- eval
- exec
- expect
- file
- more info
- mime type
file -b --mime-type toto.file
- text
coding
- find
- find
- Find
(UNIX Grymoire)
- sintaxi / syntax:
- opcions / options
- proves / tests
-mtime +7 (més d'una setmana de temps
de modificació / more than a week of modification
time)
-newerXY
-type
f (només fitxers / only files)
-perm +6000
-name
...
- combinacions
- accions / actions
- -name
- globular
expressions (wildcard)
- to use regular expressions instead of globular
expressions, see regextype
- regular
expression
- get all txt files, but exclude
*bak*.txt
find . -name "*.txt" ! -name "*bak*"
- remove all ts files or jpeg files older than 7 days
find . -type f \( -name "*.ts" -or -name
"*.jpeg" \) -mtime +7 -delete
- -regextype posix-extended
-regex ...
- How
to
use regex in file find
- find all directories with name a-toto_..., b-toto_...,
toto_... (but not c-toto_...)
find . -maxdepth 1 -type d -regextype
posix-extended -regex '\./(a-|b-|)toto_.*'
- -type
- find regular files
- find
symbolic links (-type l )
- find broken (
-L ) symbolic links (-type
l )
- find subdirectories
find . -maxdepth 1 -type d
- -delete
- delete files older than 5 days (print the list first):
find . -type f -mtime +5 -print0 | xargs -0
ls -ltr
find . -type f -mtime +5 -delete
- delete all *.ts files (not beginning with a dot),
without entering into the subdirectories (maxdepth 1),
with less than 1k:
find . -maxdepth 1 -size -1k -name
"[^\.]*.ts" -delete
- recursively delete all symbolic links
- delete broken (
-L ) symbolic links (-type
l )
find -L . -type l -maxdepth 1 -delete
- verbose (
-print )
find -L . -type l -maxdepth 1 -print
-delete
- recursively delete directories older than 150 days:
find . -maxdepth 1 -type d -mtime +150 -exec
rm -rf {} \;
- -exec
executes command for each output item; a pipe to xargs
takes the output as a single string
- list (detailed) all files with extension m2v:
find . -name "*.m2v" -exec ls -l '{}' \;
find . -name "*.m2v" -ls
- Copy all the files (-type f) in
/var/export/backup_zeus3/ modified in the latest 24h
(-mtime -1) to /tmp:
find /var/export/backup_zeus3/ -mtime -1
-type f -exec cp -pf '{}' /tmp \;
- Recursively delete all directories named ".svn":
find . -name ".svn" -type d -exec rm -rf {}
\;
- Add ".bak" suffix to all txt files:
find . -name '*.txt' -exec mv {} {}.bak \;
- | xargs
- xargs
by
examples
- list of found files (one line per file):
find . -name "*uyvy" -exec echo '{}' \;
- list of found files (one line with all the files):
find . -name "*uyvy" -exec echo '{}' +
- Calculates the total size of files modified today
(-exec produces another behaviour):
find . -mtime -1 | xargs du -c
- Fa un ls de cadascun dels fitxers (el nom pot incloure
espais) / ls of each of the files (the file name can
contain spaces):
find $PWD -name "*.html" -print0 | xargs -0 ls -l
- Fa un zip dels fitxers */$trimestre/*.pdf
find . -name "*.pdf" -print0
| grep -FzZ ${trimestre} | xargs -0
zip ${trimestre}.zip
- amb grep / with grep
grep toto $(find . -type f -newermt 2022-09-28)
find . -type f -newermt 2022-09-28 -exec grep toto '{}' \;
find . -type f -newermt 2022-09-28 | xargs grep tot
- -print0
- la separació dels resultats es fa amb '\0' en lloc
d'espais / the separation of the results is made with
'\0' instead of spaces
- si va seguit per xargs,
cal posar l'opció '-0' / if followed by xargs, xargs must have the
option '-0'
- -printf (%p: path
and file name; %h: only path; %f: only file name)
find . -type f -name "*txt" -printf "fitxer: %h
%f\n"
find . -type f -name "*txt" -printf "fitxer:
%p\n"
- How
can
I escape white space in a bash loop list?
- Esborra amb ex...
- Ignora alguns directoris / Ignore certain dirs
find . -name toto -not -path './not_here*'
- dates
- rsync with date filter
- Unix/Linux find and sort by
date modified
- fitxers del 31 de març de 2013 / files modified on
31st March 2013:
find . -newermt 2013-03-31 ! -newermt
2013-04-1
- fitxers entre el 9 d'agost de 2013 i l'11 d'agost de
2013 (ambdós inclosos), ordenats per temps:
find . -newermt 2013-08-9 ! -newermt
2013-08-12 -exec ls -ltr '{}' +;
find . -newermt 2013-08-9 ! -newermt
2013-08-12 -printf "%T@ %Tc %p\n" | sort -n
find . -newermt 2013-08-9 ! -newermt
2013-08-12 -printf "%T@ %p\n" | sort -n | awk
'{print $2}'
find . -newermt 2013-08-9 ! -newermt
2013-08-12 -printf "%T@ %p\n" | sort -n | awk
'{print $2}' | paste
-s -d ' '
- for
- getent
getent [ahosts ahostsv4 ahostsv6 aliases ethers
group gshadow hosts netgroup networks passwd protocols rpc
services shadow]
- grep
- Recursiu
grep toto -R
.
- exclou directoris
grep toto -R . --exclude-dir
dir_name_to_exclude
- grep "toto
tata" -R .
--exclude-dir={'env*','.tox*','.git','htmlcov*'}
- Cerca caràcters menys / Search for minus characters:
- Regular
expressions
- Print all file, just colour the matches:
- pdfgrep
find /path -iname '*.pdf' -exec pdfgrep pattern
{} +
- groups
- head
- see also tail
- print first 2 lines
- htop
- iconv
- id
- if
- ip
- list all
- get ip address
- Consider:
hostname --ip-address
- route
ip route list
ip route replace default via 192.168.43.1 dev
wlan0 metric 101
ip route del default via 192.168.43.1 dev
wlan0 metric 303
...
- ipcalc
- join
- journalctl
- kill
kill -<signal_code>
<process_pid>
- kill amb pgrep
- pkill
- kill recursiu de tots els descendents / descendant
recursive kill
- konsole
- locate
- ln
- ls (*) (*)
ls -ltr (reverse time sorted)
ls -l 0225[1-5,7-9].MTS ("[]" means one
character)
- precise local time: (see also rsync)
ls --full-time
- files
- /etc/localtime
- /usr/share/zoneinfo/Europe/Andorra
- /usr/share/zoneinfo/Etc/UTC
- precise UTC:
- one file per line (note that argument is number one)
- horizontal
- only dirs
ls -d */ | cut
-f1 -d'/'
for i in $(ls -d ${root_dir}/*/); do echo
$(basename $i); done
- filename expansion
- sort numbers as humans (1, 2, 10, 20):
- count number of files
- ...
- lsblk
- llistat de les particions muntades
- Examples
- minicom
- mkdir
- mktemp
- create a temporary file:
- create a temporary dir
- mount
- mount
NFS
- mount some partition from an image file (e.g. an image file from an entire
SD card)
- Mounting
a
hard disk image including partitions using Linux
- Mounting
a
raw partition file made with dd or dd_rescue in Linux
fdisk -l raspbian_20150731.bin
Disk raspbian_20150731.bin: 14,9 GiB,
15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512
bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x307439bc
Device
Boot
Start End
Sectors Size Id Type
raspbian_20150731.bin1
2048
1683593 1681546 821,1M e W95 FAT16
(LBA)
raspbian_20150731.bin2
1687552 31050751 29363200 14G 85
Linux extended
raspbian_20150731.bin3
31050752 31116287
65536 32M 83 Linux
raspbian_20150731.bin5
1695744
1818623 122880
60M c W95 FAT32 (LBA)
raspbian_20150731.bin6
1826816 31049727 29222912 14G 83
Linux
parted raspbian_20150731.bin
GNU Parted 3.2
Utilitzant /disc/rpi/raspbian_20150731.bin
Welcome to GNU Parted! Type 'help' to view a list
of commands.
(parted)
unit
Unit? [compact]?
B
(parted)
print
Model: (file)
Disk /disc/rpi/raspbian_20150731.bin: 15931539456B
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number
Start
End
Size
Type
File system Flags
1
1048576B
862000127B
860951552B primary
fat32
lba
2
864026624B 15897985023B
15033958400B extended
5
868220928B
931135487B
62914560B
logical
fat16
lba
6
935329792B
15897460735B 14962130944B
logical ext4
3
15897985024B 15931539455B
33554432B
primary ext4
- Mount first ext4 partition (6)
mkdir /mnt/toto
mount -o loop,ro,offset= 935329792
raspbian_20150731.bin
/mnt/toto
- resize partition in an image file
- How
To
: Resize Partitions in an Image File
- resize2fs
- create a device (/dev/loop0) for all partitions:
losetup -f --show raspbian_20150731.bin
parted /dev/loop0
- unit
- print
Number
Start
End
Size
Type
File system Flags
1
1048576B
862000127B
860951552B
primary
fat32
lba
2
864026624B
15897985023B 15033958400B
extended
5
868220928B
931135487B
62914560B
logical
fat16
lba
6
935329792B
15897460735B
14962130944B logical
ext4
3
15897985024B 15931539455B
33554432B
primary ext4
- create a device (/dev/loop1) for partition 6:
losetup -f --show -o 935329792
raspbian_20150731.bin
parted /dev/loop1
- unit
- print
Number Start
End
Size
File system Flags
1
0B
14996209663B 14996209664B
ext4
- check partition 6
- get the minimum size of partition 6
- resize2fs -P /dev/loop1
- resize2fs 1.42.12 (29-Aug-2014)
Estimated minimum size of the filesystem:
1712943
- as units are 4k, this means: 1712943*4096
= 7016214528 (~7GB)
- resize partition 6
- remove loops
losetup -d /dev/loop0 /dev/loop1
- Problemes / Problems
- SELinux
- /var/log/messages:
systemd: Unit mnt-vol1.mount
is bound to inactive unit dev-xvdh.device. Stopping,
too.
- nc / ncat / netcat
- netstat
- nmap
- nslookup
- paste
(posa línies en una sola / put lines into one single line)
- put all ts files into one single line, where filenames are
separated by a '|' (useful when concatenating
files):
find . -name "*.ts" | paste -s -d '|'
- put three consecutive lines into three columns:
paste -s -d '\t\t\n' input.dat > output.dat
- other examples:
- patch
- pgrep
- pkill
- kill (SIGKILL) all child processes
- printf
- The
printf command
- integer format with zeros:
- thousand separator (according to locale)
- string and numbers with padding spaces (minus sign for
left-aligned)
printf "%-40s%20d\n" ${my_string} ${my_int}
- quote special characters
- Colors / Colours
- Standard
ECMA-48:
Control Functions for Coded Character Sets
- C
- start colouring
"\e[<attributes>m"
- Python:
"\033[<attributes>m"
- attributes are ";" separated
-
0
|
all
attributes off
|
|
1
|
bold on
|
|
2 |
dark on |
|
4
|
underscore
|
|
5
|
blink on
|
|
54 |
upperscore |
|
7
|
reverse video
on
|
|
8
|
concealed on
|
|
foreground |
colour
|
background |
30
|
black |
40
|
31
|
red |
41
|
32
|
green |
42
|
33
|
yellow |
43
|
34
|
blue |
44
|
35
|
magenta |
45
|
36
|
cyan |
46
|
37
|
light grey |
47
|
90 |
dark grey |
100 |
91 |
light red |
101 |
92 |
light green |
102 |
93 |
light yellow |
103 |
94 |
light blue |
104 |
95 |
light magenta |
105 |
96 |
light cyan |
106 |
97 |
white |
107 |
- end colouring
- Exemples / Examples
-
|
shell |
Python |
print "Title" in green foreground (32) |
"\e[32mTitle\e[0m" |
"\33[32mTitle\33[0m" |
print "my_command" in blue foreground (34) |
"\e[34mmy_command\e[0m" |
"\33[34mmy_command\33[0m" |
print "my_outout" in yellow foreground
(33) |
"\e[33mmy_output\e[0m" |
"\33[33mmy_output\33[0m" |
Print "Hello", in blue foreground (34),
yellow background (43),
blinking (5) |
"\e[5;34;43mHello\e[0m\n" |
|
- emacs
- ps
- pstree
- pv
- read
- read one line from stdin and populate specified variables
read <options> <list of vars>
- options
-
option
|
description
|
examples
|
-r
|
The backslash is
considered to be part of the line, and not
interpreted as an escape character
|
|
-s
|
silent
|
|
-t <secs>
|
timeout
|
|
|
|
|
- used variables
-
variable
|
description
|
examples
|
info
|
IFS
|
Internal Field
Separator
|
IFS=
(or IFS='' ) prevents
leading/trailing whitespace from being trimmed
|
|
- line by line from a file
- read from a file, containing three fields (separated by
space) per line
while read -r field1 field2 field3
do
echo $field1 $field2 $field3
done < $filename
- Bucle sobre
json amb jq / Loop over json from jq
- iterate over lines that can contain spaces (a simple for
would only work if there were no spaces in lines) from a
variable:
#! /bin/bash
fitxers_html=$(find $PWD/ -name "*.html")
while IFS= read -r linia
do
echo "${linia}"
sed -i
's#src="http://www.w3.org/Icons/valid-html401"#src="//www.w3.org/Icons/valid-html401"#g'
"${linia}"
done < <(echo "$fitxers_html")
exit 0
#!/bin/bash
tags=$(cat <<EOF
TAG:major_brand=mp42
TAG:minor_version=0
TAG:compatible_brands=isommp42
TAG:creation_time=2016-10-27 14:41:05
EOF
)
echo "tags: $tags"
while IFS= read -r
do
tag=$REPLY
echo "tag: $tag"
pair=${tag#TAG:}
echo "pair: $pair"
done < <(echo
"$tags")
- recode
- rm
- esborra el fitxer "-nom_fitxer":
- esborra recursivament amb excepcions / recursively remove
with exceptions:
find
dir -maxdepth
1 -mindepth 1 ! -name "name_to_avoid"
-exec rm -rf {} \;
- esborra els fitxers el nom dels
quals està especificat en un fitxer:
ls *.mp4 -1 > mp4_files_to_be_deleted.txt
xargs rm
< mp4_files_to_be_deleted.txt
- esborra els fitxers apuntats per
enllaços simbòlics
- rsync
- screen
- serial
- Screen
Command:
Set Baud Rate [ Terminal Communication ]
- Commands
- Common
screen
commands
screen /dev/ttySX
baud_rate,cs8|cs7,ixon|-ixon,ixoff|-ixoff,istrip|-istrip
baud_rate
cs7|cs8 : 7|8 bits per byte
ixon|-ixon : enable|disable software
flow-control (CTRL-S/CTRL-Q) for sending data
ixoff|-ixoff : enable|disable
flow-control (CTRL-S/CTRL-Q) for receiving data
istrip|-istrip : clear|keep the eight
bit in each received byte
-
CTRL+a ?
|
help
|
CTRL+a i
|
info
|
CTRL+a K
|
kill session
|
...
|
|
- sed
- Sed - An
Introduction and Tutorial by Bruce Barnett
- Unix
Sed
Tutorial: Append, Insert, Replace, and Count File Lines
sed [-n] [-e] '[/begin_pattern/[,/end_pattern/]]
[!]command'
[-e 'command'] ... input_filename
sed [-n] [-e] '[/begin_pattern/[,/end_pattern/]] [!]{command}'
[-e
'command'] ... input_filename
- Command
line options
-
-f filename
|
execute script in
a file (several commands can be specified, each
in one line)
|
-e
script
|
execute script
(no need to specify -e for the first script)
|
-n |
only print the
matching pattern
|
-i |
replace input file |
...
|
|
- Commands
- Inserció de línies / Line
insertion
- before
sed '/This is an existing line/ i
This is a new line' toto.txt
- spaces has to be escaped:
sed -i '/<link rel="stylesheet"
href="css\/fpm.css"/ i\
<meta
name="viewport"
content="width=device-width">' toto.html
- after
sed '/This is an existing line/ a
This is a new line\n and a second one' toto.txt
- Substitució
/ Replacement
- Per a substituir
totes (g) les cadenes inici (pot ser
una expressió
regular) en el fitxer fitxer_original
per final, en fitxer_final:
sed 's/inici/final/g'
fitxer_original > fitxer_final
- per a sobreescriure el fitxer inicial: "-i" / to
overwrite original file
sed -i
's/inici/final/g' fitxer_original
- create a backup (
fitxer_original.bak )
sed -i.bak
's/inici/final/g'
fitxer_original
- el separador no cal que sigui '/'. Pot ser
qualsevol altre caràcter.
- si a dins de l'ordre volem fer servir variables,
cal posar-la entre cometes dobles:
baseurl=$1 sed -i
"s#<BaseURL>.*</BaseURL>#<BaseURL>${baseurl}</BaseURL>#g"
toto.html
- newline
- sed: How can I replace
a newline (\n)?
- remove line feed (^M) (see also dos2unix):
sed -i 's/\r//g' toto.txt
- replace all "/n" (2 chars) by real newlines in a
variable:
echo "$toto" | sed 's/\\n/\n/g'
- Replace
' -> '\'' (e.g.
for curl POST
with a specified json)
echo "${input}" | sed "s/'/'\\\''/g"
- Estableix BaseURL en un fitxer mpd
(MPEG DASH):
#! /bin/bash
baseurl=$1
fitxers_mpd=$(find $PWD -name "*.mpd")
for fitxer in $fitxers_mpd; do
echo "${fitxer}: setting
BaseURL to ${baseurl}"
sed -i
"s#<BaseURL>.*</BaseURL>#<BaseURL>${baseurl}</BaseURL>#g"
$fitxer
done
- En tots els fitxers del directori public_html:
#! /bin/bash
for f in public_html/*
do
echo $f;
sed -i.bak 's*<P
ALIGN=center>*<P
STYLE="text-align:center">*g' $f;
done
#! /bin/bash
for f in public_html/*;
do
echo $f;
sed 's*<P
ALIGN=center>*<P
STYLE="text-align:center">*g' $f > ${f}#;
rm -f $f;
mv ${f}# $f;
done
- Canvia la mida de la lletra dels textos en Android /
Change the font size in Android texts:
find .
-name *.xml -exec sed -i.bak
's/android:textSize="10sp"/android:textSize="12sp"/g'
{} \;
- Afegeix un meta refresh a tots els fitxers html:
#! /bin/bash
fitxers_html=$(find $PWD -name "*.html")
for fitxer in $fitxers_html;
do
echo "${fitxer}"
fitxer_nom=$(basename
${fitxer})
inicial="<head>"
final="<head>\n
<meta http-equiv=\"refresh\" content=\"5;
url=http://www.francescpinyol.cat/${fitxer_nom}\">"
sed -i
"s#${inicial}#${final}#g" ${fitxer}
done
- Comenta totes les línies que comencin per
"net.ipv4.ip_forward" :
- Uncomment all lines that have an "=":
sed -e '/=/ s/^#//' -i
manila.rpm.uncommented.conf
- Uncomment lines that start with #submission and
subsequent lines with options, unil the next line that
is not an option (in the example, line starting with
smtps):
- /etc/postfix/master.cf
...
#submission inet
n
-
n
-
- smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
# -o
smtpd_client_restrictions=$mua_client_restrictions
# -o
smtpd_helo_restrictions=$mua_helo_restrictions
# -o
smtpd_sender_restrictions=$mua_sender_restrictions
# -o
smtpd_recipient_restrictions=permit_sasl_authenticated,reject
# -o
milter_macro_daemon_name=ORIGINATING
#smtps inet
n
-
n
-
- smtpd
...
- activate_submission.sed
/^#submission/,/^#\w/
{
/^#submission/ s/^#//
/^# / s/^#//
}
sed -f activate_submission.sed master.cf
...
submission inet
n
-
n
-
- smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
-o
smtpd_client_restrictions=$mua_client_restrictions
-o
smtpd_helo_restrictions=$mua_helo_restrictions
-o
smtpd_sender_restrictions=$mua_sender_restrictions
-o
smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
#smtps inet
n
-
n
-
- smtpd
...
- Substitueix tota la
línia / Replace all line:
sed -e "/^.\?admin_token/
c\admin_token=$ADMIN_TOKEN" -i
/etc/keystone/keystone.conf
sed -e "/^host
all
all
127.0.0.1/
c\host
all
all
127.0.0.1/32
md5"
/var/lib/pgsql/data/pg_hba.conf
- canvia el valor de la variable toto_var:
sed '/var toto_var*/ c\var toto_var =
"nou_valor"' toto.js
- grep-like
sed -n /pattern/p file.txt
- print only lines starting with "admin_token" or
one single character followed by "admin_token":
sed -n '/^.\?admin_token/p'
/etc/keystone/keystone.conf
- print only line with "duration=" in it, and
replace it by nothing (i.e. get the value) (e.g. to
parse result given by ffprobe):
- printing all lines except (
! ) those that
match the pattern (grep -v ):
sed -n -e '/pattern/ !p' file.txt
- Ranges
by patterns
sed '/start/,/stop/ s/#.*//'
- fer una substitució després de trobar una
coincidència:
- elimina totes les línies que comencin per «abc»:
- seq
- similar to
{10..20} , but with seq one of the
limits can be a variable
END=20
for i in $(seq 10 $END)
do
echo $i
done
- set
- activate debug
- set positional parameters
set -- param1 param2
- Exemple / Example
#!/bin/bash
echo "supplied $# parameters: $@"
# add a parameter
set -- "new_param1" "new_param2"
echo "new $# parameters: $@"
exit 0
- shopt
- sleep
- sort
- How
to sort on Multiple Fields with different field separator
- sort by months in English:
LC_ALL=en_US.utf8 sort ... -k2M ...
cat logs_per_ordenar.txt | awk -F '[ /]' '{print
$3"\t"$2"\t"$1"\t"$0}' | LC_ALL=en_US.utf8 sort -k1n
-k2M | cut -f4-
- input:
[15/Feb/2024 09:13:04] ...
[17/Jan/2024 09:13:04] ...
[17/Jan/2024 09:58:13] ...
[17/Jan/2024 10:36:23] ...
[16/Feb/2024 09:13:04] ...
[17/Jan/2024 10:36:23] ...
[17/Jan/2024 11:04:25] ...
[02/Jan/2024 11:16:45] ...
[17/Jan/2024 11:16:50] ...
[17/Jan/2024 11:24:56] ...
[30/Nov/2023 08:53:01] ...
[30/Nov/2023 08:54:13] ...
[02/Jan/2023 08:54:13] ...
[14/Feb/2024 09:13:04] ...
- output:
[02/Jan/2023 08:54:13] ...
[30/Nov/2023 08:53:01] ...
[30/Nov/2023 08:54:13] ...
[02/Jan/2024 11:16:45] ...
[17/Jan/2024 09:13:04] ...
[17/Jan/2024 09:58:13] ...
[17/Jan/2024 10:36:23] ...
[17/Jan/2024 10:36:23] ...
[17/Jan/2024 11:04:25] ...
[17/Jan/2024 11:16:50] ...
[17/Jan/2024 11:24:56] ...
[14/Feb/2024 09:13:04] ...
[15/Feb/2024 09:13:04] ...
[16/Feb/2024 09:13:04] ...
- sponge
- Instal·lació / Installation
- Alma 8
sudo dnf install moreutils
- Ús / Usage
sudo sh -c 'sort -u /etc/exports | sponge
/etc/exports'
- ...
- ssh
- su / sudo
- RootSudo
(Ubuntu)
- sudoers
file
su root
adduser mynewuser
passwd mynewuser
[su]
- option 1: add files to /etc/sudoers.d (because they
are included by the last line in /etc/sudoers file)
su
echo " mynewuser ALL=(ALL)
NOPASSWD: ALL" > /etc/sudoers.d/mynewuser
- option 2: directly edit sudoers file
visudo (don't use regular editor, as
you can end up with a non-working system)
- got to after line with "
root ALL=(ALL)
ALL "
- i
mynewuser ALL=(ALL) ALL
- to avoid password request every time you
execute sudo, append the following line at
the end of the file (/etc/sudoers):
mynewuser ALL=(ALL) NOPASSWD:
ALL
- ESC
- :wq
- Problemes / Problems
- How
to
Add a User and Grant Root Privileges on CentOS 7
- give a user admin privileges:
sudo adduser <username> admin
- redirect
sudo sh -c 'echo "nameserver 192.168.0.1"
>> /etc/resolv.conf'
- execute as another user (which has home)
su my_user
-c " mkdir /mnt/nfs/my_dir"
sudo su
- my_user -c "mkdir /mnt/nfs/my_dir"
- execute as another
user (which has no home; created as
--no-create-home )
su nginx -s /bin/bash -c " mkdir
/mnt/nfs/my_dir"
sudo -u nginx mkdir /mnt/nfs/my_dir
sudo su - nginx -s /bin/bash -c "export"
- mode interactiu / interactive mode
- mode gràfic / graphical mode
- Gnome
- KDE
kdesudo kate /etc/X11/xorg.conf
- LXDE
gksudo leafpad /etc/fstab (text
editor)
gksudo pcmanfm /etc (file browser)
- sudo in ssh,
crontab
- to avoid having to specify -t option (to avoid error:
sudo: sorry, you must have a tty to run sudo ):
- /etc/sudoers.d/80-my_user
myusername ALL=(ALL)
NOPASSWD: ALL
# no need to specify "-t" when doing sudo via
ssh for this user
Defaults:myusername !requiretty
- for all users: /etc/sudoers
- while in ssh
ssh -q remote_user@remote_server -t
"sudo bash -c \"while ! [ -e /dev/xvdf ]; do sleep 1;
done\""
- sysctl
- tail
- see also: head
- get latest 5 lines
- tar
- root
tarball
needs to be extracted with --numeric-owner
- ignore git dirs:
tar --exclude=.git --exclude=.gitignore -cvzf
toto.tgz my_dir
- follow symbolic links (-h)
tar chvf documents.tgz Documents
- uncompress
-
extension
|
tar
|
.tgz, .tar.gz
|
-xzf
|
.bz2
|
-xjf
|
.xz
|
-xJf
|
- partially remove path when creating (/mnt/vol1 will not
appear in paths for files inside the tar file):
tar cvzf toto.tgz --directory /mnt/vol1
letsencrypt
- tee
- telnet
- test
- timeout
- tip
- top
- tr
- substitució
de caràcters individuals / replacement of individual
characters:
tr '<input_characters>'
'<output_characters>'
- elimina les comes i cometes / delete comma and quotes:
- elimina les cometes simples / delete single quotes
- remove line feed (
^M ):
- majúscules i minúscules / upper case and lower case
- url safe
- trap
- indica una funció que cal executar quan es rep el senyal, excepte KILL (9), en
lloc del comportament habitual
- How
"Exit Traps" Can Make Your Bash Scripts Way More Robust
And Reliable
- Capping Expensive Resources: AWS AMI after updating an
instance
- Sending
and Trapping Signals
- Identifying
received
signal name in Bash
- Examples
trap <code or function>
<list of signals>
#!/bin/bash
trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM
...
#!/bin/bash
function on_exit {
# variable to know whether we
arrive here from an "exit 0" or "exit 1"
rv=$?
# do something before exiting (e.g.
remove temporary files)
...
}
# when EXIT signal is sent to this script (either from
an exit command or from a CTRL-C), go to finish
function
trap on_exit EXIT
...
if ...
then
exit 1
fi
exit 0
#!/bin/bash
function on_exit {
# variable to know whether we
arrive here from an "exit 0" or "exit 1"
rv=$?
# do something before exiting (e.g.
kill all children processes)
own_pid=$$
echo "sending TERM to all children
processes (whose parent id is ${own_pid}):"
# only informative:
# install pstree with: sudo yum
install psmisc
command -v pstree >/dev/null
2>&1 && pstree -p ${own_pid}
# install pgrep with: sudo yum
install procps
command -v pgrep >/dev/null
2>&1 && pgrep -a -P ${own_pid}
pkill -TERM -P $$
}
# when EXIT signal is sent to this script (either from
an exit command or from a CTRL-C), go to finish
function
trap on_exit EXIT
...
if ...
then
exit 1
fi
exit 0
- tree
- umask
- useradd
- with specified password:
useradd --password $(openssl passwd -1
${new_password}) ${new_user}
- usermod
- wait
- which
- while
- xargs
- takes input and passes it as arguments to the specified
command. Input can be separated by whitespaces (default) or
by
'\0' (option: -0 ).
- find
and xargs
- remove files specified
in a file as one file per line
- list files specified in a file as one file per line
xargs ls -l < list_files.txt
cat list_files.txt | grep something | xargs ls
-s
- zip / unzip
- compress
- recursive
- encrypt with a password (prompt):
zip -e
toto.zip -r my_dir
- with find
#!/bin/bash
EXPECTED_ARGS=1
if [ $# -ne $EXPECTED_ARGS ]
then
cat <<EOF
Usage: `basename $0` yyyy_t
EOF
exit 1
fi
PREFIX=my_prefix
trimestre=$1
find . -name "*.pdf" -print0 | grep -FzZ
${trimestre} | xargs -0 zip
${PREFIX}_${trimestre}.zip
- uncompress
unzip -d /destination/dir toto.zip
- password protection
- remove known password from file
|