Skip to content

files and directories ​

grep in compressed files ​

bash
# -A lines to show after match
# -B lines to show before match
zgrep -A 50 -B 50 'command_status: 1281' files-*

# -E use regular expression in search
zgrep -E 'command_status: [1-9]' files-*

who's writing to a file ​

bash
fatrace | grep file.log

list file extensions, recursively ​

bash
find . -type f -printf "%f\n" | awk -F. 'NF > 1 && $NF != "" {print $NF}' | sort -u

create temporary file ​

bash
$ mktemp
/tmp/tmp.UHHyLzjCB0
bash
$ tempfile
/tmp/fileR5dt6r

remove windows line breaks ​

bash
sudo dnf install -y dos2unix
dos2unix original.csv
bash
sudo apt-get install -y dos2unix
dos2unix original.csv

split a file in small pieces ​

bash
split --verbose --bytes=<size> -d <file> <prefix>

where:

  • --bytes=<size> is the maximum size of each piece, in bytes, or an interger folloed by K, M, G, T, P, E, Z, Y
  • -d adds a numeric suffix in the end of the file name
  • <prefix> the name of every piece

an example: an iso file can be sliced in several 1Gb files:

bash
$ split --verbose --bytes=1G -d imagefile.iso img
creating file  "img00"
creating file "img01"
creating file "img02"
creating file "img03"
creating file "img04"

to join these files back together, use

bash
cat img00 img01 img02 img03 img04 > imagem.iso

listing only one column of a file ​

a file with columns separated by : can have its data displayed like this

bash
cat file.log | cut -d":" -f4

showing a file content, removing duplicates ​

bash
cat file.log | awk '!a[$0]++'

list the recursive contents of a directory ​

bash
ls -R /my_path | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }'

besides showing the contents recursively, this command also shows the full path of the files, not just the names.

reference: AskUbuntu.com

get a file mime-type ​

bash
file --mime-type -b my_file

find file by content ​

bash
grep -lr "text to fine" *.txt