Using find to act on files is very useful, but if the files that are found need different actions based on their filetype, it gets a bit trickier. For example there are some log files foo.log but after 10 days they get compressed to foo.log.gz. So you are finding regular text files, as well as gzipped text files. Extend your find with an -exec and a bash shell to determine what file extension it is, and to run the appropriate grep or zgrep based on that. Then run it through awk or whatever else to parse out what you need.

# find . -type f -name 'foo.log*' -exec bash -c 'if [[ $0 =~ .log$ ]]; then grep foobar $0; elif [[ $0 =~ .log.gz$ ]]; then zgrep foobar $0; fi' {} \; | awk '{if(/typea/)a++; if(/typeb/)b++; tot++} END {print "typea: "a" - "a*100/tot"%"; print "typeb: "b" - "b*100/tot"%"; print "typec: "tot-(a+b)" - "(tot-(a+b))*100/tot"%"; print "total: "tot;}'
typea: 5301 - 67.4771%
typeb: 2539 - 32.3192%
typec: 16 - 0.203666%
total: 7856
share:

make many directories with Bash sequences

Sometimes it’s necessary to do things with sequences in Bash. If you want to create a bunch of directories that will be mount points for NFS servers you could do this:


# mkdir /mnt/fs42
# mkdir /mnt/fs42/vol0
# mkdir /mnt/fs42/vol1
# mkdir /mnt/fs42/vol2
# mkdir /mnt/fs42/vol3

But, even the best experts at the “up arrow key” wouldn’t want to do this for 50 file servers. A nested for loop would work, but it’s not necessarily the easiest way to go. The command seq makes a sequence of integers bounded by the numbers specified.

ok:

for i in `seq 1 50`; do for j in `seq 0 3`; do mkdir -p /mnt/fs$i/vol$j; done; done;

Using Bash sequences, you can tell the shell to interpret this {1..50} as a list of integers between 1 and 50. This also works with letters like {a..t}.

# echo {a..t}
a b c d e f g h i j k l m n o p q r s t
# echo {0..50}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

way better:


# mkdir -p /mnt/fs{1..50}/vol{0..3}

of course if you have non-sequential lists, you may have to specify each element like this:

{1,3,4,5,6,9}

share: