— devops, scripting101 — 1 min read
tee
: read from stdin and write to stdout as well as file(s)
df -h | tee disk_usage.txt
1Filesystem Size Used Avail Use% Mounted on2dev 7.8G 0 7.8G 0% /dev3run 7.9G 1.8M 7.9G 1% /run4/dev/nvme0n1p3 212G 159G 43G 79% /5tmpfs 7.9G 357M 7.5G 5% /dev/shm6tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup7tmpfs 7.9G 15M 7.9G 1% /tmp8/dev/nvme0n1p1 511M 107M 405M 21% /boot9/dev/sda1 459G 165G 271G 38% /data10tmpfs 1.6G 16K 1.6G 1% /run/user/120
Ouput also saved to the disk_usage.txt
file
awk
: manipulate text dataThe file contents could look something like this:
1fristName lastName age city ID2
3Thomas Shelby 30 Rio 4004Omega Night 45 Ontario 6005Wood Tinker 54 Lisbon N/A6Giorgos Georgiou 35 London 3007Timmy Turner 32 Berlin N/A
Print all
1awk '{print $0}' information.txt
Ouptut:
1fristName lastName age city ID2
3Thomas Shelby 30 Rio 4004Omega Night 45 Ontario 6005Wood Tinker 54 Lisbon N/A6Giorgos Georgiou 35 London 3007Timmy Turner 32 Berlin N/A
Print first column
1awk '{print $1}' information.txt
Ouput:
1Thomas2Omega3Wood4Giorgos5Timmy
Print line start with O
(regex)
1awk '/^O/' information.txt
Output:
1Omega Night 45 Ontario 600
head
: print top N line from filesprint lines between 10 and 20
1head -n 20 state.txt | tail -10
tail
: print last N line from filestail -f
following logfile
less
: showing part of file without loading entire file to memory, reduce using memory when accessing large text fileless -p "failure"
: start at first occurrence of pattern “failure” in the file.
less -N
: show content with line number
touch
: update modification time of file to current time, create file if not existcut
: cutting out the sections from each line of files and writing the result to standard outputlsof
: Listof Open Fileprovides a list of files that are openedlsof -c Mysql
: list all files opened by Mysql
lsof -u root
: list all files opened by user root
lsof -u ^root
: list all files opened by everyone except root
lsof -p PID
: list all files opened by PID
ps
: Process Status is used to list the currently running processes and their PIDs along with some other information depends on different options. It reads the process information from the virtual files in /proc file-system. /proc contains virtual files, this is the reason it’s referred as a virtual file systemps -aux
: view processes owned by a user named "x"
netstat
: Network Statistic
&``disown``nohup
:
&
puts the job in the background, that is, makes it block on attempting to read input, and makes the shell not wait for its completion.
disown
removes the process from the shell's job control, but it still leaves it connected to the terminal. One of the results is that the shell won't send it a SIGHUP
. Obviously, it can only be applied to background jobs, because you cannot enter it when a foreground job is running.
nohup
disconnects the process from the terminal, redirects its output to nohup.out
and shields it from SIGHUP
. One of the effects (the naming one) is that the process won't receive any sent SIGHUP
. It is completely independent from job control and could in principle be used also for foreground jobs (although that's not very useful).