Skip to content

Latest commit

 

History

History
118 lines (78 loc) · 1.84 KB

bash.md

File metadata and controls

118 lines (78 loc) · 1.84 KB

Bash and other Unix tools

Bash

Converting

Decimal to hex

$ printf "%x" 1234

Hex to binary:

  • With xxd -p -r:
$ echo -n "deadbeef" | xxd -p -r | hexdump -C
00000000  de ad be ef                                       |....|

To write hex in a binary at a given position:

echo "00000088: 0000 4000" | xxd -r - modified.elf

To read it back: xxd -s 0x88 -l modified.elf

  • With perl pack:
$ echo -n "deadbeef" | perl -ne 'print pack "H*", $_' | hexdump -C
00000000  de ad be ef   

Note that hexdump -C prints exactly as it is in the file, but that hexdump swaps bytes:

$ echo -n "deadbeef" | xxd -p -r | hexdump
0000000 adde efbe                              
0000004

Instead of hexdump -C you can also use od -t x1:

$ echo -n "deadbeef" | xxd -p -r | od -tx1
0000000 de ad be ef
0000004

Date

  • Epoch to date: date -d @1234567890
  • Get epoch: date "+%s"
  • Given date to epoch: date -d '2024-03-03T06:00' +%s

Find

To search for something excluding some paths:

$ find . -type d -name "blah" ! -path "./softs/*' ! -path "./bin/*"

Do not forget the * in the path...

Find files bigger than 10M

find . -type f -size +10M

Sed

To remove lines 1-n of a file:

$ sed '1,nd' file

Awk

Swapping 2 columns in a file:

 awk -F, '{print $2,$1}' OFS=, file

Convert

Creating an image with text "Blah"

convert -background white -fill dodgerblue  -font "FreeMono" -strokewidth 2  -stroke blue   -undercolor lightblue -size 165x70 -gravity center label:Blah image.png

Cut

Get given characters: cut -c15-22

Renaming files

Renaming .txt extension files to .text:

for f in *.txt; do 
    mv -- "$f" "${f%.txt}.text"
done

Tar

Untar selectively:

tar -xzf lotsofdata.tar.gz --wildcards --no-anchored '*contract*'