Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# File Printing and Dumping

Type a file:
cat PATH/FILE
cat -n PATH/FILE # show a text file with line numbers
cat PATH/FILE | tr [:upper:] [:lower:] # show a text all in lower case
cat PATH/FILE | tr [:lower:] [:upper:] # show a text all in Upper case
tac PATH/FILE # show a text from the last line to the first (thus listing lines backwards), useful to ordering elements in a last-in, first-out (LIFO) way
fold -w COLOUMN PATH/FILE # wrap a text file, cutting lines at the given coloumn. Useful to format data as input for a program
fmt -u -w80 file.txt # format a text file output: 1 space between words, 2 spaces between sentences, 80 coloumns
nl PATH/FILE  # show line numbers for every line (empty lines are not counted by default)
nl -ba PATH/FILE # show line numbers for every line and count all lines, including empty ones
nl -s": " PATH/FILE # show line numbers for every line and separate line numbers from line text using the specified separator (": " in this case)
pr -l 50 test.txt # show a text as pages, each page is 50 lines long (minimum 11, default is 66)
pr -l 50 --column 2 test.txt # show a text as pages, each page is 50 lines long and divided into 2 columns
more PATH/FILE  # type a file page by page
less PATH/FILE  # show a file page by page, and let scroll through pages. It shows escaped characters as plain text (get it with apt-get install less)
less -F PATH/FILE  # show a file page by page, and let scroll through pages (get it with apt-get install less). If the file output fits a single screen it quits without having to press "q"
grep "TEXT" PATH/FILE  # show only lines containing "TEXT"
grep -v "TEXT" PATH/FILE  # show only lines NOT containing "TEXT"
grep -i "TEXT" PATH/FILE  # show only lines containing "TEXT" (case insensitive)
grep -vi "TEXT" PATH/FILE  # show only lines NOT containing "TEXT" (case insensitive)
grep -m 1 "TEXT" PATH/FILE  # show only the first matching line containing "TEXT"
grep -v -m 1"TEXT" PATH/FILE  # show only the first matching line NOT containing "TEXT"
grep -i -m 1"TEXT" PATH/FILE  # show only the first matching line containing "TEXT" (case insensitive)
grep -vi -m 1"TEXT" PATH/FILE  # show only the first matching line NOT containing "TEXT" (case insensitive)
grep -B 1 -A 1 "TEXT" PATH/FILE  # show lines containing "TEXT" and show 1 line Before and 1 line After each match
grep -B 2 -A 2 -n "TEXT" PATH/FILE  # show lines containing "TEXT" and show 2 lines Before and 2 lines After each match, together with line numbers
head PATH/FILE  # show the head (first 10 lines) of FILE
head --lines=20 PATH/FILE  # show the first 20 lines of FILE
head --bytes=1024 PATH/FILE  # show the first 1024 bytes of FILE
head --lines=-50 PATH/FILE  # show all the FILE but the last 20 lines
head --bytes=-2048 PATH/FILE  # show all the FILE but the first 2048  bytes
tail PATH/FILE  # show the tail (last 10 lines) of FILE
tail --lines=20 -F PATH/FILE  # show the last 20 lines of FILE, and new lines as the file grows. If the files doesn't exist yet, it waits for it (useful for log files)
tail --lines=+20 -F PATH/FILE  # show the last lines of FILE starting from line 20
tail --bytes=1024 PATH/FILE  # show the last 1024 bytes of FILE
tail -f PATH/FILE # keep showing the last line of FILE whenever there are added new ones (useful for log files) until stopped
tail -f PATH/FILE | sed '/^ENDSTRING$/ q' # keep showing the last line of FILE whenever there are added new ones (useful for log files) until a given string appears
sed -n "3p" PATH/FILE  # show line number 3 of FILE
sed -n "2,4p" PATH/FILE # show lines 2 to 4 of FILE
sed -n "1,5p" PATH/FILE # show lines 1 (beginning) to 5 of FILE
sed -n "5p,$" PATH/FILE # show lines 5 to the end of FILE
sed -n -e '3,5p' -e '9,15p' PATH/FILE # show lines 3 to 5 and 9 to 15 of FILE
sed -e "5d" PATH/FILE # show all lines of FILE except for the ones from 2 to 30 included, and except for line 5
sed -e "2,30d" PATH/FILE # show all lines of FILE except for the ones from 2 to 30 included
sed -e "5d;2,30d" PATH/FILE # show all lines of FILE except for line 5, and except for the ones from 2 to 30 included
awk 'NR==2 {print;exit}' SOURCE # Print line number 2 from SOURCE file
awk 'NR==2,NR==4' SOURCE # Print line numbers 2 to 4 (inclusive) from SOURCE file
awk '{print $1}' PATH/FILE # print the first word of every line in FILE
awk '{print substr ($1,0,1)}' PATH/FILE # print the first character of every line in FILE
awk 'NF>=1{print $NF}' PATH/FILE # print the last word of every line in FILE
grep -o '.$' PATH/FILE # print the last character of every line in FILE
cat PATH/FILE | awk 'length($0) < 5' # show lines longer less than 5 characters in FILE
cat PATH/FILE | awk 'length($0) > 5' # show lines longer more than 5 characters in FILE
cat PATH/FILE | awk 'length($0) == 5' # show lines longer with exactly 5 characters in FILE
cat PATH/FILE | awk 'length($0) >= 5' # show lines with at least 5 characters in FILE
cat PATH/FILE | awk 'length($0) >= 5 { print NR ":" $0 }' # show lines with at least 5 characters in FILE, preceded by line numbers of matching lines
grep '.\{5\}' PATH/FILE # show lines with at least 5 characters in FILE

Binary dump of a file:
xxd -b PATH/FILE # binary dump of FILE
xxd -b PATH/FILE | less # navigate the binary dump of FILE

Hexadecimal dump of a file:
xxd PATH/FILE # hex dump of FILE
xxd PATH/FILE | less # navigate the hex dump of FILE
xxd -u PATH/FILE # hex dump of FILE. Show hex numbers in Upper case
xxd -a PATH/FILE # hex dump of FILE. Replace multiple NUL (00) lines with an asterisk ( * )
od -Ax -tx1z -v PATH/FILE # hex dump of FILE
hexdump -C PATH/FILE # hex dump of FILE
hexdump PATH/FILE # hex dump of FILE without the ASCII equivalent

File printout on a printer

Print a file on a printer: lpr -Pprinter PATH/FILE lpr -Pprinter -#N PATH/FILE # print N copies of FILE on printer List print queue: lpq Remove a job from the print queue: lprm JOB_ID lprm # remove all jobs from the print queue




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 06:00 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer