Simple usage of xargs, tr, head, tail commands

Last Updated on

1 xargs

Similar to -exec

Delete .txt files from current folder

find . -name "*.txt" | xargs rm

Count line numbers in all .txt files

find . -name "*.txt" | xargs wc -l

2 tr

Replace

Replace capital with small case

echo "THIS IS A TEST" | tr "A-Z" "a-z"
 
#Result this is a test

ROT13 Encrypt

echo "Test" | tr "a-zA-Z" "n-za-mN-ZA-m"
 
# Result: Grfg

ROT13 Decrypt

echo "Grfg" | tr "a-zA-Z" "n-za-mN-ZA-m"
 
# Result: Test

Remove digits from the string

echo "This 3 is 1831 a tes2t" | tr -d "0-9"
 
# Result: This  is  a test

Remove redundant space

echo "This is  a       test message     " | tr -s ' '
 
# Result: This is a test message

Remove redundant empty lines form text file

cat text.txt | tr -s '\n'

3 head

Print first 10 lines

head my.log -n 10

4 tail

Print last 10 lines

tail my.log -n 10

Leave a Reply

Your email address will not be published. Required fields are marked *