Outpout formatting…

The following are the some basic utilities we use daily during Linux system administration and    manipulation.

sort -nr  [sort line of a file]

n-numeric, r-reverse

uniq -c  [print and count unique line on a file]

Efficient use of ‘find’ command

Find core files in this directory tree and remove them

find . -name “core” -exec rm -f {} \;

Find a pattern in a file using the recursive grep (ignore case)
find . -type f | xargs grep -i MYPATTERN
find . -name ‘*.sh’ -exec grep -li MYPATTERN {} \;

Find all my pipe files and change their permissions to all writable
find . -name “pipe*” -exec chmod 666 {} \;

Find files that were modified 7 days ago and archive
find . -type f -mtime 7 | xargs tar -cvf `date ‘+%d%m%Y’_archive.tar`

Find .txt files in this directory and move them as .xml
for i in `ls *.txt`; do mv $i ${i%%.txt}.xml; done

More on sed

Delete leading whitespace (spaces, tabs) from front of each line
aligns all text flush left
sed ‘s/^[ \t]*//’

Delete trailing whitespace (spaces, tabs) from end of each line
sed ‘s/[ \t]*$//’

Delete BOTH leading and trailing whitespace from each line
sed ‘s/^[ \t]*//;s/[ \t]*$//’

Count lines (emulates “wc -l”)
sed -n ‘$=’

To replace a pattern in the middle of a line
sed ‘s/.time/-/’ (will replace time with – where match from starting of the line)

To replace a range of matched pattern with a new one
sed ‘s/tty.*time=/-/’ (will replace the pattern tty-to-time with a -)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: