Skip to content

files and directories

create temporary file

almalinux
$ mktemp
/tmp/tmp.UHHyLzjCB0
ubuntu linux
$ tempfile
/tmp/fileR5dt6r

remove windows line breaks

almalinux
sudo dnf install -y dos2unix
dos2unix original.csv
ubuntu linux
sudo apt-get install -y dos2unix
dos2unix original.csv

split a file in small pieces

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:

$ 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

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

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

showing a file content, removing duplicates

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

list the recursive contents of a directory

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

file --mime-type -b my_file

find file by content

grep -lr "text to fine" *.txt