User Tools

Site Tools


linux:ubuntu:useful-commands

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:ubuntu:useful-commands [2024/02/08 02:10] odeftalinux:ubuntu:useful-commands [2024/02/08 17:12] (current) – [[user] Change mode] odefta
Line 4: Line 4:
 <code> <code>
 file file.txt file file.txt
 +</code>
 +
 +===== [cat] Add line numbers to a file =====
 +<code>
 +cat -n file.txt
 +nl file.txt
 </code> </code>
  
Line 260: Line 266:
 <code> <code>
 sudo ip link show sudo ip link show
 +</code>
 +
 +===== [ip] UP / DOWN states of network interfaces =====
 +
 +Display only the names and states (UP / DOWN) of the network interfaces on the system, using the format interface_name:state.
 +
 +<code>
 +ip link show | awk '/^[0-9]+:/ {print $2 " " $9}' | sed 's/://g' | awk '{print $1 ":" ($2=="UP"?"UP":"DOWN")}'
 +</code>
 +Output:
 +<code>
 +lo:DOWN
 +ens33:UP
 +ens34:UP
 +docker0:DOWN
 +br1:DOWN
 +br2:DOWN
 </code> </code>
  
Line 285: Line 308:
 ping -c 4 10.125.50.50 ping -c 4 10.125.50.50
 </code> </code>
-c - number of packets to send (by default in infinite)+c - number of packets to send (by default is infinite)
  
 ===== [ip] Show routing table / find default gateway ===== ===== [ip] Show routing table / find default gateway =====
Line 297: Line 320:
 {{:linux:ubuntu:pasted:20240206-175930.png}} {{:linux:ubuntu:pasted:20240206-175930.png}}
  
 +To extract just the default gateway ip:
 +<code>
 +ip route show | awk 'NR==1 {print $3}'
 +</code>
 +{{:linux:ubuntu:pasted:20240208-024419.png}}
 ===== [ip] [nmcli] Find DNS Servers ===== ===== [ip] [nmcli] Find DNS Servers =====
 <code> <code>
Line 477: Line 505:
 grep -o 'searched_string' file.txt | wc -l grep -o 'searched_string' file.txt | wc -l
 </code> </code>
-o - print each occurrence on a separate line+o - print each occurrence on a separate line \\ 
 l (from wc) - count lines l (from wc) - count lines
  
Line 490: Line 518:
 grep -nri search workspace/C/ | less grep -nri search workspace/C/ | less
 </code> </code>
-n - show line number +n - show line number \\  
-r - recursive+r - recursive \\ 
 i - case insensitive i - case insensitive
  
Line 731: Line 759:
 ls [abc]*.txt ls [abc]*.txt
 </code> </code>
-[abc] - characters a, b or c +[abc] - characters a, b or c \\  
-* - any number of characters (including 0)+* - any number of characters (including 0) \\ 
 .txt - ends with txt .txt - ends with txt
  
Line 1052: Line 1080:
 echo -e "5\n3\n8\n1" | sort -n echo -e "5\n3\n8\n1" | sort -n
 </code> </code>
-e - interpret escape sequences+e - interpret escape sequences \\ 
 n - numbers (sort as numbers) n - numbers (sort as numbers)
  
Line 1167: Line 1195:
 <code> <code>
 history | awk '{$1=""; print $0}' | sort | uniq -c | sort -nr -k1 | head -10 | awk '{$1=""; print $0}' history | awk '{$1=""; print $0}' | sort | uniq -c | sort -nr -k1 | head -10 | awk '{$1=""; print $0}'
 +</code>
 +
 +===== [for] Start 10 processes in background then kill them =====
 +
 +<code>
 +for i in {1..10}; do sleep 3000 & done; pkill sleep
 +</code>
 +
 +===== [for] Create 15 files - their names contains numbers =====
 +
 +Using a one-liner, create 15 files with names ranging from final-exam-00.txt to final-exam-15.txt.
 +
 +<code>
 +for i in {0..15}; do touch final-exam-$(printf "%02d" $i).txt; done
 </code> </code>
  
Line 1449: Line 1491:
  
 {{:linux:ubuntu:pasted:20240208-012914.png}} {{:linux:ubuntu:pasted:20240208-012914.png}}
 +
 +===== [bash] [sec] [hash] Script to hash two arguments - string =====
 +
 +Create a script that takes two arguments (strings). The script hash_it.sh concatenates the two arguments and displays their SHA256 hash sum.
 +
 +<code bash hash_it.sh>
 +#!/bin/bash
 +
 +nr_args=$#
 + 
 +if [ "$nr_args" -ne 2 ]; then
 +    echo "Error: two args should be present. Usage: $0 <arg1> <arg2>"
 +    exit 1
 +fi
 +
 +result="$1$2"
 +echo -n $result | openssl dgst -sha256
 +</code>
 +{{:linux:ubuntu:pasted:20240208-022830.png}}
 +
 +===== [bash] [sec] [hash] Script to read all lines from a file and print the SHA256 sum of each =====
 +
 +Suppose the input file is called parole.txt.
 +
 +<code bash hash_it_all.sh>
 +#!/bin/bash
 +
 +IFS=$'\n'
 +while read password; do
 +    echo -n $password | openssl dgst -sha256 | cut -d '=' -f2 | awk '{print $1}'
 +done < parole.txt
 +</code>
 +{{:linux:ubuntu:pasted:20240208-023728.png}}
 +
 +===== [bash] [ps] Script to display every second the process with highest CPU usage =====
 +
 +<code bash top.sh>
 +#!/bin/bash
 +while true; do
 +    clear
 +    #echo "Update on: $(date)"
 +    echo "Process with highest CPU usage:"
 +    ps -eo %cpu,pid,command --sort=-%cpu | head -n 2
 +    sleep 1
 +done
 +</code>
 +{{:linux:ubuntu:pasted:20240208-030524.png}}
  
 ===== [git] Configure initial settings ===== ===== [git] Configure initial settings =====
Line 1686: Line 1775:
 <code> <code>
 w | awk 'FNR>2 {print $1}' w | awk 'FNR>2 {print $1}'
 +</code>
 +
 +===== [user] Display the users who have opened a terminal on the host system =====
 +
 +<code>
 +who | awk '$2 ~ /^tty/ {print $1}'
 +</code>
 +
 +<code>
 +~ - equals for regular expressions
 +/ / - delimitates a regular expression
 +^tty - starts with tty.
 </code> </code>
  
Line 1855: Line 1956:
 </code> </code>
  
-Eliminare read permission for group:+Eliminate read permission for group:
 <code> <code>
 chmod 601 file.txt chmod 601 file.txt
Line 1861: Line 1962:
 </code> </code>
  
-Eliminare write permission for all:+Eliminate write permission for all:
 <code> <code>
 chmod ugo-w file.txt chmod ugo-w file.txt
linux/ubuntu/useful-commands.1707351011.txt.gz · Last modified: 2024/02/08 02:10 by odefta