Shell Shortcuts
Make sure you 'man' the following functions before you use 'em - follow blindly at your own risk.
Most of these were discovered using RH Linux, though a few are from FreeBSD.
Good Related Articles
Renaming Files:
rename .htm .html *.htm
will replace the first instance of .htm with .html in all files that match *.htm
Replacing Text:
all files in current directory:
perl -p -i -e's/str1/str2/g' *
all files in current directory and sub-directories recursively:
find . -type f | xargs perl -p -i -e's/str1/str2/gi'
count occurences of word 'str' in 'file'
perl -n -le'$c+=/str/g; END{print $c}' file
replace leading spaces with same number of leading tabs
perl -p -i -e 's/^( +)/"\t" x length $1/e' file(s)
File Listings:
count of all the files that are in current path and below, recursively:
find . | wc -l
count lines of php code in the current path, recursively:
find . -name "*.php" | xargs cat | wc -l
Managing Files
finding the 50 largest files in a directory:
put this in your ~/.bashrc file, save it, and type "source ~/.bashrc".
function big()
{
find . -printf "%10kk %p\n" 2>/dev/null | sort -rn | head -${1:-50} | more
}
now you'll be able to cd to a directory, type "big" and get the 50 largest files and
their size in kb in that directory and its subdirs. the command take an optional
argument, so "big 20" will show you the 20 largest files. note: this command can
take a while from /
delete all files from current dir that are zero bytes (recursively) (RH Linux, Mandrake):
better - find . -size 0b |xargs rm
slow --- find . -size 0c -exec rm {} \;
delete all files whose names do not contain 'str' (recursively)
best - grep -R "str" *|grep -Eo '^([^:]+)'|uniq|xargs rm
slow - find . -type f | perl -ne'`rm $_` if!/str/'
Printing
replace UNIX linebreaks with Windows linebreaks for printers that need them:
perl -pe's:\n:\r\n:' filename | lpr -P username@printer_ip
...but a better way is to get the 'unix2dos' tool and simply:
dos2unix < filename | lpr -P username@printer_ip
Web Stuff
figure out how large a single web page is (all files used on page + html)
wget -r -nv --level=0 --ignore-tags=a --delete-after https://www.domain.com/whatever
wget should then recursively grab everything the page uses in the way of images,
style sheet, javascript, etc but without following links. you'll get a readout of
how large all the pieces are and then wget will delete the files
vi[m]
replace duplicate words throughout the file, confirming each (don't want to screw
up the file, after all)
good --- :1,$s/\(\w\+\) \1/\1/gc
better - :%s/\v(\w+) \1/\1/gc
best --- :%s/\v(\w+)\s\1/\1/gc
Required Rambling: I did this because I realized a few years ago that *nix cmdline was vastly
more powerful than any other OS (namely Windows) that I'd ever used. But the learning curve is
pretty steep. The cmdline is like a mighty weapon... not just anyone can run on and use it to
it's full potential. Only through experience and training and making mistakes does one really
learn how to wield any tool. It is my firm belief that the commandline is much more powerful than
any GUI.
These are a few snippets I've discovered over the past year or so that have helped me get things
done on the commandline. I hope you find something useful.