Monday, October 24, 2011

Of cats and redirection...

Let's say you want to cat a file and redirect the output to itself, something like:

cat file > file;

Or perhaps more usefully:

cat file | tail -n 100 > file;

You will end up with an empty file. This is likely because of how the shell handles inodes and redirection (just a guess). A quick and way to get around this is:

cat file | tail -n 100 | dd of=file;

And viola! file points at the correct inode.

Tuesday, August 30, 2011

Tar Directory Structure

16:37:31 <blkperl> hmm so how do you tell tar from 1998 to only tar the directory structure
16:38:29 <haus> port update tar?
16:38:42 <haus> or...yum update tar
16:38:43 <anakha> I don't think that's an option :)
16:41:22 <marut> mkdir tmp && cd tmp && ( cd .. && find . -type d ) | xargs mkdir -p && rmdir tmp && tar -cvf dirstructure.tar .


...rewind...


mkdir tmp && cd tmp && ( cd .. && find . -type d ) | xargs mkdir -p && rmdir tmp && tar -cvf dirstructure.tar .

...oh yeah.

Monday, August 22, 2011

pkginfo y u no read stdin





package_file="$(mkfifo tempfifo && (gunzip -c "packagename.pkg.gz" > tempfifo &) && pkginfo -d tempfifo | awk '/application / {print $2}')"

Wednesday, August 17, 2011

ZFS chmod -R and what not to do

Suppose you need to recursively apply a ZFS ACE to a directory structure. This is what you should not start by doing:

find . -type d -exec chmod -R A+group:videocat:r-x---a-R-c--s:-d----:allow {} \;

The problem is the -R. Guess what that does. :)

Chant fix!


for each in `find . -type d`; do while ls -dV $each | grep videocat > /dev/null; do chmod A0- $each; done; done;

Thursday, June 30, 2011

python shell

ipython has some bugs in it, but readline and tabcompletion are not-exactly-modern marvels

so, add this to your .bashrc


alias pysh='python -ic "import readline, rlcompleter;readline.parse_and_bind('\''tab:complete'\'')"'


credit for this one goes to MostAwesomeDude

Thursday, June 23, 2011

Find 3.2

While find can be used to recursive perform an operation on files with the -exec flag, doing so can be costly. In the case of recursive permissions, you could change

find . -type f -exec chmod 644 {} + -o -type d -exec chmod 755 {} +

To

chmod -R a+rX .

This recursively adds the read bit to all files, and if the execute bit is set for user, group, or other on the file, it is applied for all else. If you're updating 10000+ files, this will run much faster. (And it's easier to type.)

Wednesday, June 22, 2011

ldapsearchin

ldapsearch -LLL -x -h ldap.schoolname.edu -D uid=me,ou=People,dc=shcoolname,dc=edu -b ou=Group,dc=schoolname,dc=edu gidNumber=* gidNumber |grep gidNumber | cut -f2 -d" " | sort -g

Monday, June 20, 2011

Find 3.1

Here are the last two finds combined into a one-liner:

find . -type f -exec chmod 644 {} + -o -type d -exec chmod 755 {} +

The '+' can replace the escaped ';' to tell the find exec to collect all the files first, and then execute (think xargs). And the '-o' will be short-circuited, so it will only test for a type of 'd' if the type is not 'f'.

find 3

First, find all directories starting in the current directory and recursing deeper, and chmod them all to 755.

find . -type d -exec chmod 755 {} \;

Then, find all files starting in the current directory and recursing deeper, and chmod them all to 644.

find . -type f -exec chmod 644 {} \;

Sweet.

Friday, June 17, 2011

find 2

find . -exec grep foo {} \;

find

the first in a series on find

find . | xargs grep foobar

Innagural spell

Using cut to chop things


cut -f1 -d"-"  /etc/dfs/sharetab


Will print out the first part up to the '-' of every line in the file /etc/dfs/sharetab