Tuesday, June 2, 2015

Some cool tricks with AWK and CUT

This following script prints out the sum of the disk utilization of total directories

[root@Arch-DOC ~]# du -sh /home/wiz/
184G    /home/wiz

[root@Arch-DOC ~]# du -sh /home/wiz/* | egrep  '[0-9]G'
2.5G    /home/wiz/Docs
6.9G    /home/wiz/Documents
19G    /home/wiz/Downloads
19G    /home/wiz/ISO IMAGES
5.3G    /home/wiz/Music
59G    /home/wiz/r-drive
45G    /home/wiz/Videos
18G    /home/wiz/VirtualBox VMs

[root@Arch-DOC ~]# du -sh /home/wiz/* | egrep  '[0-9]G'  | awk '{print $1}' | cut -d'G' -f1| awk '{sum+=$1} END {print sum}'
174.7

Thus we can find the sum of the spaces of each individual directory utilization.

Renaming multiple files with extensions

Simple for loop is described as follows to bulk renaming files


[root@Arch-DOC test]# touch {a..z}

[root@Arch-DOC test]# for i in *; do mv $i $i.txt; done

a.txt  c.txt  e.txt  g.txt  i.txt  k.txt  m.txt  o.txt  q.txt  s.txt  u.txt  w.txt  y.txt
b.txt  d.txt  f.txt  h.txt  j.txt  l.txt  n.txt  p.txt  r.txt  t.txt  v.txt  x.txt  z.txt


[root@Arch-DOC test]# for i in *.txt; do sh -c "mv $i `echo $i| cut -d'.' -f1 `" ; done

[root@Arch-DOC test]# ls
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z


[root@Arch-DOC test]# for i in *; do sh -c "mv $i `echo $i| cut -d'.' -f1`.pdf" ; done

[root@Arch-DOC test]# ls
a.pdf  c.pdf  e.pdf  g.pdf  i.pdf  k.pdf  m.pdf  o.pdf  q.pdf  s.pdf  u.pdf  w.pdf  y.pdf
b.pdf  d.pdf  f.pdf  h.pdf  j.pdf  l.pdf  n.pdf  p.pdf  r.pdf  t.pdf  v.pdf  x.pdf  z.pdf



Finding the list of Directories Recursively on your Linux Machine.

First of Lets create some directories..

[root@Arch-DOC test]# mkdir Dir{1..10}

[root@Arch-DOC test]# mkdir Dir1/dir1{a..z}
[root@Arch-DOC test]# mkdir Dir2/dir2{a..z}

Now we shall also create some more sub-directories under dir2.
[root@Arch-DOC test]# mkdir Dir2/dir2a/sub-sub-dir2a{1..10}

and also lets create some sample files..

[root@Arch-DOC test]# touch files{1..10}

Lets see what we have got..

[root@Arch-DOC test]# ls
Dir1  Dir10  Dir2  Dir3  Dir4  Dir5  Dir6  Dir7  Dir8  Dir9  files1  files10  files2  files3  files4  files5  files6  files7  files8  files9

Now that the directories and Sub-directories have been created. Lets run some commands to test it out.

[root@Arch-DOC test]# find ./ -maxdepth 1 -type d
./
./Dir7
./Dir9
./Dir8
./Dir4
./Dir5
./Dir2
./Dir10
./Dir1
./Dir3
./Dir6

[root@Arch-DOC test]#  ls -d */
Dir1/  Dir10/  Dir2/  Dir3/  Dir4/  Dir5/  Dir6/  Dir7/  Dir8/  Dir9/

Similarly, we can go on to the next level directories and get a list of only that particular sib directories.

[root@Arch-DOC test]#  ls -d */*/*
Dir2/dir2a/sub-sub-dir2a1   Dir2/dir2a/sub-sub-dir2a2  Dir2/dir2a/sub-sub-dir2a4  Dir2/dir2a/sub-sub-dir2a6  Dir2/dir2a/sub-sub-dir2a8
Dir2/dir2a/sub-sub-dir2a10  Dir2/dir2a/sub-sub-dir2a3  Dir2/dir2a/sub-sub-dir2a5  Dir2/dir2a/sub-sub-dir2a7  Dir2/dir2a/sub-sub-dir2a9

[root@Arch-DOC test]# find ./ -maxdepth 3 -type d | wc
     73      73    1031

Using Stream-editor and replacing the : with null value on each directory while printing to stdout.
[root@Arch-DOC test]# ls -R | grep ":" | sed -e s/://g | wc
     73      73    1030


HTH

No comments:

Post a Comment