News for hackers. Stuff that matters.
Re:Ask HN: What are some good examples to demonstrate unix p... by devnonymous on Tuesday March 11, 2014 @12:48PM (#7380464)
I've always found the `find` and xargs command pair to be the quickest way to demonstrate the unix philosophy. For example, consider, if we want to do this ...

# inplace (-i), execute (-e), subtitution of the regular expression 'Pattern' (s/Pattern/), # with the word 'Substitution' (Substitution/), on all matches (g) in file 'filename', you'd do ...

$ sed -i -e 's/Pattern/Substitution/g' <filename>

# Now if you want to do this for all text files under a directory tree ...

$ find -name '.txt' | xargs sed -i -e 's/Pattern/Substitution/g'

# ...while limiting it to just 2 levels of 'depth' ...

$ find -name '.txt' -maxdepth 2 | xargs sed -i -e 's/Pattern/Substitution/g'

# ...and for only those that were modified yesterday ... ... # ...belonging to a certain user .... ...

you get the idea. Other good tools that fall in the similar category are 'sort', 'uniq', 'tr' ...etc. Eg:

$ find /tmp/ -type f -exec stat -c "%s %n" {} \; | sort -n

There are no comments on this comment yet.