Saturday, February 14, 2015

How to cut, grep, remove, print characters or files in linux

awk

Print characters less than 3
................
$ ls 
Applications    Network        Users        bin        dev        home        net        private        tmp        var
Library        System        Volumes        cores        etc        mach_kernel    opt        sbin        usr

$ ls | awk 'length($0) < 4'
bin
dev
etc
net
opt
tmp
usr
var
................

Print or cut only selected column

$ ls -l
total 16
-rw-r--r--+ 1 sasi  staff  39 Feb 18  2014 index.html.en
-rw-r--r--+ 1 sasi  staff  40 Feb 18  2014 ip.php

$ ls -l | awk '{print $2}'
16
1
1

$ ls -l | awk '{print $2, $4}'
16
1 staff
1 staff

Xargs:

Find and remove / copy / move 

$ find /path -iname 'search string' | xargs 'action'

i- ignore case

Example:
$ find /root -iname 'asterisk' | xargs rm -rf 

This will find and remove all the files and dir matching with asterisk




 

No comments:

Post a Comment

CSS tricks

Mixed paint in background: background: linear-gradient(to right, #b6e358, #38b143) Grid view: display: grid; grid-template-columns: a...