Thursday, January 11, 2007

Eval & Xargs

eval
Give me another chance. Re-evaluate this line and execute it.
eval is incredibly useful if you have shell variables that include other shell variables, shell variables that include aliases, shell variables that include I/O redirectors, or all sorts of perversities. It's commonly used within shell scripts to "evaluate" commands that are built during execution.

xargs
http://www.unixreview.com/documents/s=8274/sam0306g/
Many UNIX professionals think the xargs command, construct and execute argument lists, is only useful for processing long lists of files generated by the find command.
The find-xargs command combination is a powerful tool.

Examining the "Too Many Arguments" Problem
pr -n 'find . -type f -mtime -1 -print'|lpr
find . -type f -mtime -1 -print|xargs pr -n |lp

find . -type f -name "*.txt" -print|xargs rm
find . -type f -name "*.txt" -exec rm {} \; -print

xargs One-Liners
find . -type f -name "*.txt" | xargs -i ksh -c "echo deleting {}; rm {}"

find . -type d -print|sed 's@^@/usr/project/@'|xargs mkdir -p
The following command creates in the usr/project directory, a copy of the current working directory structure.


in the usr/project directory, a copy of the current working directory structure:
Find command syntax
find [path...] [expression]
The expression is made up of options, tests and actions

find Options
-daystart, -depth, -maxdepth levels, -mindepth levels
-mount, -noleaf, -xdev

find tests
+n for greater than n,-n for less than n, n for exactly n.

-path pattern
find . -path './myproject' -print
-regex pattern
File name matches regular expression pattern. This is a match on the whole path, not a search.
-size n[bckw]
File uses n units of space. File uses n units of space. The units are 512-byte blocks by default or if `b' follows n, bytes if `c' follows n, kilobytes if `k' follows n, or 2-byte words if `w' follows n.
-type c
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
Find Actions
-exec command
-fprint file
-print, -printf format
-prune
If -depth is not given, true; do not descend the current directory.
If -depth is given, false; no effect.
-ls

Tuesday, January 09, 2007

Find command examples
http://linux.about.com/od/commands/l/blcmdl1_find.htm

find [path...] [expression]
The expression is made up of options, tests and actions


find . -name "rc.conf" -print

find . -name "rc.conf" -exec chmod o+r '{}' \;
find . -exec grep "www.athabasca" '{}' \; -print
find . -exec grep -q "www.athabasca" '{}' \; -print


find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print
This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded.

find -maxdepth 2 -type f
find -type f ! -perm -444 // Find files not readable by all (useful for web site)

find -name '*' -size +1000k

In the above example the system would search for any file that is larger then 1000k.


find /home -user joe
find $HOME -mtime 0
Search for files in your home directory which have been modified in
the last twenty-four hours.


find /       \( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\n' \) , \
\( -size +100M -fprintf /root/big.txt '%-10s %p\n' \)

Traverse the filesystem just once, listing setuid files and
directories into /root/suid.txt and large files into
/root/big.txt.
find . -atime +7 -o -size +`expr 10 \* 1024 \* 2` -print

this command was looking for any files in the current directory and its subdirectories (represented by .) that have not been accessed for more than 7 days (-atime +7) or (-o) that were greater than a certain size (-size +).

find / -name core -exec rm -f {} \;
find / -name core -print | xargs rm -f # more efficient than the previous one

Sunday, January 07, 2007

yum vs apt

From wiki

The Yellow dog Updater, Modified (YUM) is an open source command line package management utility for RPM-compatible Linux computer systems.

Yum is a full rewrite of its predecessor tool, Yellowdog Updater (YUP), and was developed primarily in order to update and manage Red Hat Linux systems used at the Duke University department of Physics. Since then, it has been adopted by Fedora Core, CentOS, and many other RPM-based Linux distributions, including Yellow Dog Linux itself, where it has replaced the original YUP utility.

Yum's repository system is quickly becoming the standard for RPM-based repositories.

Advanced Packaging Tool, or APT, is a package management system used by Debian GNU/Linux and its derivatives. APT was originally designed as a front-end for dpkg to work with Debian's .deb packages, but it has since been modified to work with the RPM Package Manager system via apt-rpm, and the Fink project has ported APT to Mac OS X for some of its own package management tasks.

Some experiences here( http://www.fedoraforum.org/forum/archive/index.php/t-1034.html)
yum Pros:
Comes with Fedora by default.
Widely supported by many RPM repositories.
Fairly easy to roll your own repository.

yum Cons:
No "pinning" or other mechanisms for backing out updates.
Somewhat confusing to create a hierarchy of "trust," in terms of which repositories to use for which packages.
No graphical update/management tool.

apt Pros:
Somewhat wider support across distributions (since its a Debian tool)
Synaptic, a graphical GUI for doing updates.
"Pinning" support, and uninstall support.

apt Cons:
Not a base part of the FC package set.
Perhaps a slightly higher learning curve to use effectively.

"I personally like Yum, but thats for a few reasons... Yum is slightly easier once you learn the commands, but it does take a while to download the headers if you dont have broadband. THats probably the main advantage of APT from my point of view. On a slower connection APt works better and faster. Ask Ug, he'll testify to that fact. If youve got broadband, i would say go for yum, but make sure you know some of the basic commands first. If youre on dialup, go for APT."