Essential UNIX Tricks and Tools?
Chris Lesner asks: "What handy UNIX tricks/tools do you use everyday? I'm asking for stuff that amazes your friends and makes you wonder how they use UNIX w/o them.
Some simple examples include: job control (with fg, bg/&, jobs, Ctrl-Z); moving login sessions between machines with Screen for vt100 and VNC for X11 and using screen and VNC to share login session b/w users for demos etc.; using find, xargs -i and
echo to build command strings which after
inspection can be piped back though bash e.g.
`find . -type f | xargs -i{} echo "cp {} {}.bak" | bash` I'm asking b/c my source for this kind of information has dried up as my UNIX skills have matured. I'm guessing other Slashdot readers have the same problem. By the way, if you think the examples I give are lame I challenge you to better them!"
All the tricks you can cram into one of those big books, and more: Unix Power Tools -- O'Reilly's best book, IMHO.
-- Ken Kinder ken@_nospam_kenkinder.com http://kenkinder.com/
In my .bashrc:
In my-- Ken Kinder ken@_nospam_kenkinder.com http://kenkinder.com/
Well, seeing that my friends don't know much about UNIX, the following usually impresses them:
//empire/files -U jesus) /tmp
1. Connect to home router (FreeBSD) w/ PuTTy SSH client
2. smbclient to windows share (i.e. smbclient
3. mget files to
4. run command shell on client box
5. pscp files from router
6. in the meantime, as a finishing touch, perl -e $#29%% something
To impress normal people, you don't need to type anything fancy. Just type FAST, and as soon as a command returns, pretend to contemplate for a second, and then type your next command.
There's more fun too. It completes tons of crazy stuff. I'd check it out.
-- Ken Kinder ken@_nospam_kenkinder.com http://kenkinder.com/
It's 10 PM. Do you know if you're un-American?
most linux newbies do not have a clue how their ctl-alt-f(x) works, and I have found this tool to be very useful on occasion.
badness 10000
`find . -type f | xargs -i{} echo "cp {} {}.bak" | bash`
This is fugly and a waste.
find . -type f -exec cp "{}" "{}.bak" \;
Using your sig line to advertise for friends is lame.
Just use find . -type f | xargs -ixx cp xx xx.bak
Taral
WARN_(accel)("msg null; should hang here to be win compatible\n");
-- WINE source code
su root cd /; rm -rf *
:-)
This one works really nice when your getting ready to do a fresh install, since it allows to vent all that pent up frustation with your now obsolete system
-- The morphemes of your disquisition are ascertainable, but they have eschewed an ambit of transpicuous exposition.
In tcsh:
;
/home/*", to see who the disk pigs are.
alias dusort 'du -s \!* | sort -r -n | awk '"'"'{sum+=$1;printf("%9d %9d %s\n",sum,$1,$2)}'"'
In bash (or zsh):
function dusort ()
{
du -s "$@" | sort -r -n |
awk '{sum+=$1;printf("%9d %9d %s\n",sum,$1,$2)}'
}
An example way to invoke it is "dusort
A lot of people don't know about the "watch" command
/dev/hda
:p
watch -n1 df -h
I always use hdparm to speed up IDE drives too.
hdparm -c1 -d1 -A1 -a16 -m16 -u1 -S240 -W1 -k1 -K1 -X66
Of course there's aliases you can set to reduce keystrokes.
alias ll='ls -l'
pgrep and killall can be pretty useful.
killall java
renice 19 `pgrep java`
I also have a button on my GNOME panel that runs xkill so I can click on a locked up GUI app and terminate it.
If you have a multiprocessor box, you can do "make -j3" to run parallel builds.
And updatedb and locate are really useful for finding files quickly.
If you're trying to remember a command you've run before you can run this to find it quickly.
history | grep 'command'
They you can run !850 to run command #850 from your history for example.
And you can run this to find GNOME packages.
rpm -qa | grep gnome
That's all I can think of for now. Other posters are welcome to point out faster ways to do any of the tips I've given.
Here are some stupid shell tricks I've found moderately useful:
- bash: HISTCONTROL=ignoreboth, zsh: setopt HIST_IGNORE_SPACE;
This means when you type a command with a space in front of it, it doesn't go in your history. Useful if you do something that you don't want others to see (eg, xv ~/.pr0n/*.jpg).
- The zsh FAQ has an entry which describes how to get your xterm title to describe all sorts of fun stuff. Here's a bash equivalent:
- PATH management:
- This next one is absolutely necessary on any Linux machine:
- Recursively do something for each subdirectory:
Here's a dumb shell script that I call "randsort" - it prints out lines from stdin in a random order:This is useful 'cause you can do something like this:
xv `ls -t ~/.pr0n/*.jpg | head -200 | randsort`
and this randomization, for some reason, "feels" better than xv's -random option. It also has a number of "legitimate" uses. For instance:
Anyway, I got lots of stupid little crap like this. I try not to get too far into the customization stuff, as it's definitely a timesink and it's very important to be able to use a "standard" unix setup. For example, my .emacs is circa 80 KB (I'm big into Lisp), but I'm extremely proficient with vi, because there are lots of situations where emacs is not available, or vi is better-suited to the job.
The lameness filter is such a piece of horseshit. I mean, are the editors deliberately trying to prevent us from discussing code and technical matters? You'll need to re-indent all the snippets above because of this.
Lameness filter fodder:
C este un limbaj de programare cu scop general ale carui caracteristici sint economia de expresie, structuri moderne de control al fluxului si de date, precum si un set bogat de operatori. C nu este un limbaj de nivel "foarte inalt", nici "mare", si nu este specializat vreunei arii particulare de aplicatii. Dar absenta in restrictii si generalitatea sa il fac mai convenabil si mai de efect pentru mai multe scopuri decit limbaje presupuse mai puternice. C a fost la inceput proiectat si implementat pe sistemul de operare UNIX pe DEC PDP11 de catre Dennis Ritchie. Sistemul de operare, compilatorul C si in mod esential, toate programele de aplicatii ale lui UNIX (inclusiv software-ul folosit pentru a pregati cartea aceasta) sint scrise in C. Compilatoare de C exista deasemenea si pe mai multe alte calculatoare, intre care IBM System/370 Honeywell 6000 si Interdata 8/32. C nu este legat de nici un hardware sau calculator anumit si e simplu de scris programe care se pot executa fara nici o modificare pe diferite calculatoare care au limbajul C implementat. Aceasta carte are drept scop sa-l ajute pe cititor sa invete sa programeze in C. Ea contine o initiere, pentru ca noii utilizatori sa poata incepe cit mai repede posibil, capitole separate pentru fiecare caracteristica majora, si un manual de referinta. Marea parte a textului nu se bazeaza atit pe expunerea de reguli si propozitii cit pe citirea, scrierea si revizuirea de exemple. In cea mai mare parte exemplele sint programe reale si sint complete si nu fragmente izolate. Toate exemplele au fost testate direct din text, care este intr-o forma citibila pe calculator. Pe linga faptul ca am aratat cum se utilizeaza efectiv limbajul, am incercat in plus, acolo unde era posibil, sa-l ilustram cu algoritmi utili si cu principii de bun stil in programare si proiectare sanatoasa. Aceasta carte nu este un manual introductiv de programare. Ea presupune anumite familiaritati cu conceptele. de baza din programare, ca variabile, instructiuni de asignare, bucle, functii. Cu toate acestea, un programator novice va fi in stare sa citeasca cartea si sa-si insuseasca limbajul, chiar daca ajutorul unui coleg cu experienta mai mare i-ar usura munca foarte mult. In experienta noastra, C s-a dovedit un limbaj placut, expresiv si adaptabil pentru o mare varietate de programe. Este usor de invatat si "se poarta bine" pe masura ce experienta in programare cu el creste. Speram ca aceasta carte va va ajuta sa-l folositi bine.
C este un limbaj de programare cu scop general ale carui caracteristici sint economia de expresie, structuri moderne de control al fluxului si de date, precum si un set bogat de operatori. C nu este un limbaj de nivel "foarte inalt", nici "mare", si nu este specializat vreunei arii particulare de aplicatii. Dar absenta in restrictii si generalitatea sa il fac mai convenabil si mai de efect pentru mai multe scopuri decit limbaje presupuse mai puternice. C a fost la inceput proiectat si implementat pe sistemul de operare UNIX pe DEC PDP11 de catre Dennis Ritchie. Sistemul de operare, compilatorul C si in mod esential, toate programele de aplicatii ale lui UNIX (inclusiv software-ul folosit pentru a pregati cartea aceasta) sint scrise in C. Compilatoare de C exista deasemenea si pe mai multe alte calculatoare, intre care IBM System/370 Honeywell 6000 si Interdata 8/32. C nu este legat de nici un hardware sau calculator anumit si e simplu de scris programe care se pot executa fara nici o modificare pe diferite calculatoare care au limbajul C implementat. Aceasta carte are drept scop sa-l ajute pe cititor sa invete sa programeze in C. Ea contine o initiere, pentru ca noii utilizatori sa poata incepe cit mai repede posibil, capitole separate pentru fiecare caracteristica majora, si un manual de referinta. Marea parte a textului nu se bazeaza atit pe expunerea de reguli si propozitii cit pe citirea, scrierea si revizuirea de exemple. In cea mai mare parte exemplele sint programe reale si sint complete si nu fragmente izolate. Toate exemplele au fost testate direct din text, care este intr-o forma citibila pe calculator. Pe linga faptul ca am aratat cum se utilizeaza efectiv limbajul, am incercat in plus, acolo unde era posibil, sa-l ilustram cu algoritmi utili si cu principii de bun stil in programare si proiectare sanatoasa. Aceasta carte nu este un manual introductiv de programare. Ea presupune anumite familiaritati cu conceptele. de baza din programare, ca variabile, instructiuni de asignare, bucle, functii. Cu toate acestea, un programator novice va fi in stare sa citeasca cartea si sa-si insuseasca limbajul, chiar daca ajutorul unui coleg cu experienta mai mare i-ar usura munca foarte mult. In experienta noastra, C s-a dovedit un limbaj placut, expresiv si adaptabil pentru o mare varietate de programe. Este usor de invatat si "se poarta bine" pe masura ce experienta in programare cu el creste. Speram ca aceasta carte va va ajuta sa-l folositi bine.
I'm sure everyone knows about this...except newcomers to UNIX.
Its called nohup. Nohup is a command that will prevent your job from being terminated once you log out of your account. Always leaving those "please do not log out of this account" message to your co-workers? Well, that's all fine unless your co-workers are assholes. Also, even if they're not, you might want to do them a favor by doing this.
What you do is type (wihout the quotes) "nohup " where is the command or program you want not to be interrupted upon logout. You can type in normal syntax.
Another essential "command" if your at the command line and are going to start somethin which takes awhile is (w/o quotes) " but newcomers won't know them, nor will people who just download cygwin to use on their Windows OS, because some scientific applications require Linux.
social sciences can never use experience to verify their statemen
one of the first 'tricks' I saw a more experienced ;-)
:)
guy to perform in good old days when I was a
newbie...
How he transfered a text file from one of his
accounts (in Russia) to another one (in SUNY SB)
when the source side did not allow ftp
connections, telnet only?
Well, 'more foo' in one xterm, 'cat > foo' in
another, then click left-drag-move-click middle...
Vas'ka, you might be reading this... HI!
Paul B.
I believe nothing is more impressive than knowing what you're doing with the world of UNIX command line tools and standard in/out on the pipe.
I often show up Perl studs at work, creating similar functionality to their spaghetti code with a few lines of korn shell code in much less time.
Shell programming with the pipe is the most powerful form of elegant simplicity I've ever seen in any computing.
This works with half Unix/Emacs-savy people as well, btw. It surely didn't harm my career to show my boss the eshell when he cursed cmd.exe and explorer.exe for being the moronic tools they are
If all else fails, people will at least be impressed that you can memorize all those funky keystrokes. That works with vi as well, of course.
Here is a trick for my favorite shell, es the Extensible Shell, a derivitive of plan9's rc shell. The es shell has the most logical set of syntax rules I have ever seen in a shell. It is also the only shell where I have figured out how to do automated file renaming (very handy). Here is a transcript of a session as an example:
;; touch 123
;; touch 124
;; touch 125
;; touch 126
;; touch 127
;; touch 128
;; touch 129
;; ls
123 124 125 126 127 128 129
;; echo <={~~ (*) 1*}
23 24 25 26 27 28 29
;; for (i = <={~~ (*) 1*}) {mv 1$i one$i}
;; ls
one23 one24 one25 one26 one27 one28 one29
this functionality is great for preventing name collisions when consolidating files from two directories into a single directory.
as some explanation <={...} is like typing the return value of the command inside the braces, ~~
returns the part of the second aruments matches that were expanded from the the first argument's glob.
...and this lie crawls out of its mouth: 'I, the state, am the people.'
Does "FORMAT C:" count?
I agree with everything that's already been said about knowing the value of pipe's and the standard tools.
In addition to screen which has already been mentioned the two things that I've noticed impressing other people recently are, splitvt, and Emacs
Emacs is impressive because in the hands of an expert you can do almost anything - and splitvt is a stunning program which will turn one shell into two - I highly recommend you check it out.
The biggest single command which saves me time is 'cd -' which changes to your previous directory under bash.
It doesn't sound terribly useful, but it is... Take my word for it.
I wasn't as impressed when I first saw mouse pointer activation, but after using it for a while I get almost as annoyed when windows don't auto activate on mouse overs, as when I grab a non reverse polish calculator.
Degaussing scares the bad magnetism out of the monitor and fills it with good karma.
I believe that if you put the cd in .bash_profile instead of .bashrc, it will only be executed for login sessions - that is, for ssh, not scp.
Instead of
$ ps aux | grep foo | grep -v grep
use
$ ps aux | grep [f]oo
The brackets will show up in the ps output but don't match your pattern, so your grep is automaticly excluded from your final output.
Regular expressions aren't so much either a trick or a tool exactly, but you can use them with all the "good" tools.
Get the book "Mastering Regular Expressions," by Jeffrey E. F. Friedl. http://www.oreilly.com/catalog/regex2/
Read it slowly, a couple of pages every day. I didn't understand much of what he was trying to say until I read the book the second time.
But why make up my own clever things to say... From http://www.oreilly.com/catalog/regex/desc.html "There can be certain subtle, but valuable, ways to think when you're using regular expressions, and these can be taught."
I find that books that teach you how to think about problems and solutions are few and far between, and books that do it well are almost impossible to find. This books is one of those.
Once learned regular expressions are one of those things that can profoundly effect the way you work. And once your there "you wonder how they [other people] use UNIX w/o them".
...wouldn't this be better in every way?
find . -type f -exec cp -r {} {}.bak \;
Find is your friend!
I consider this pretty basic, but I remember some junior coworkers blown away by
<tt>something | sort | uniq -c | sort -rn</tt>
On the coding level, I constantly use the regex library (with extended regular expressions) and profiling. There are a lot of places where a single well-designed RE can eliminate many lines of code, and profiling can help you ensure that you close all files you open, free all memory you malloc, etc.
For every complex problem there is an answer that is clear, simple, and wrong. -- H L Mencken
Two biggies for me are 2>a to send errors to a file named "a". And strace, when you really need to know what a prgram is doing.
/dev/. The list goes on.
I couldn't live without strace. I had a font problem with X, strace told me where the problem was. xine wasn't playing sound, strace showed be that it was hitting the wrong entry in
For Debian users, apt-spy (requires installation of the package) to find fast sites for package downloads, and apt-listchanges to list what all the changes are.
To wow silly Windows users, eject and aafire (part of aalib).
Have you read my journal today?
I'm by no means an expert (I only know the basic commands), but one command I like to use is w.
finger tells you who's on the system from what location. w, on the other hand, tells you who's on, how long the system has been up (without a reboot), what everyone else is doing, how long they've been idle, etc.
Locate files with a string in them. For example:
find /etc -type f -exec grep -l hdparm {} \;
Also, using tar across pipes to copy a tree without an intermediate tar file:
tar cf - foo | (cd /bar ; tar xvf -)
I also use it over ssh in cron jobs:
tar cf - foo | ssh root@host "cd /bar ; tar xvf -"
Method of processing duck feet
> tar cf - foo | ssh root@host "cd /bar ; tar xvf -"
/bar
/bar && tar xvf -"
Use && to avoid dumping your crap in root if
does not exist:
tar cf - foo | ssh root@host "cd
Hey, that works in tcsh too. Keen.
- H
One of the first things I install on a new system is always xtail.
It has nothing to do with X Windows.
It works just like 'tail -f' but it can watch multiple files. You can even watch whole directories and it will notice when new files are created.
http://www.unicom.com/sw/xtail/
Very handy tool.
Ctags and tab completion in VI as well as vsplit and CVS from VI
having a good bashrc/vimrc/muttrc will save you soo damned much time and typing.
ssh keys and ssh-agent
Screen which has been mentioned is also somthing i cannot live without.
also perl from the command line.
one i use a lot:
perl -i.bak -lpe 's///g;'
replace whatever in your regex in whatever files
backing up original files to file.bak
Sorry man I don't controll the aliens.
For all non-hidden files in the present working directory: back the file up, then replace all
instances of PATTERN with REPLACEMENT:
$ perl -wpi.bak -e 's/PATTERN/REPLACEMENT/g' *
will complete, and will list possible completions, etc.I got tired of looking up all of the various -f* and -m* options in gcc. So the Bash completion project now knows how to complete gcc/g++ options, e.g.,
Other shells have had programmable completion for a while now. It's nice to see this feature added to bash, and it's nice to see someone volunteering to collect the widespread completion functions.
You cannot apply a technological solution to a sociological problem. (Edwards' Law)
for foo in *; do cp $foo `basename $foo .bak`; done
Next, two basic tips:
One last thing, to address a major peeve I have with many scripts I find:
Always use random names for temp files. Even if you don't want to use mktemp, please do something as simple as appending
After hacking around on *nix for years, everything in that book was old news to me. Well, almost everything. Out of 1000+ pages, here's the only trick I had never seen before:
If you have a directory full of files that are precious to you, run "touch ./-i" in that directory. This creates an empty file named "-i". Why do this? Because:
If you accidentally "rm *" there, the shell's default collating order while expanding the * will put the "-i" file first in the list, which will then be interpreted by rm as the interactive option, rather than a file.
(I've never used this trick, but I can see how it would be much less hassle than trying to remember to chmod u-w a precious file every time you get done working with it. Refinements on the technique include hardlinking the "-i" file to different directories instead of touch'ing it more than once, to save on inodes.)
(This also assumes that the user is already familiar with any of the half-billion methods of actually deleting a file whose name starts with '-'. This is /the/ most frequently asked FAQ in comp.unix.*.)
There. Now you know the most interesting trick in the whole book, and you don't need to spend the sixty bucks. :-)
You cannot apply a technological solution to a sociological problem. (Edwards' Law)
when you can just do this:
Click here if you just like to click on shit.
I also find myself using this shortcut when I'm tracking down spam:Beats typing the argument over and over - especially when it's long - and it's faster than hitting the up-arrow and editing the previous command. Hope someone finds this useful, I've already pulled a few great tips out of this thread myself.
Shaun
Thanks to the War on Drugs, it's easier to buy meth than it is to buy cold medicine!
I'm surprised no one's yet mentioned the command to "read mail, real fast".
My tips for becoming a good scripter, which I think is *essential* for a good Unix admin. (BTW my shell of choice is always Korn, pref. ksh93)
;-)
/dev/tcp/127.0.0.1/xx
Remember that shell is a proper programming language. I use loops and stuff on the command line all the time - not just in scripts.
Don't use awk when you can use cut etc. etc. You will learn a lot about scripting by trying to be efficient.
Learn how to use sed and vi *properly*. If you use vim, vim.sourceforge.net has loads of useful tips.
Learn Korn shell's ${!}, ${#}, ${x:y:z} type constructs.
Don't depend too much on OS (especially Linux) specific commands. It can make you lazy and you can look like a klutz when you have to use a different Unix.
Sensible use of functions can simplify large shell scripts enormously.
Don't be afraid of creating and using new file descriptors - they can be invaluable.
Become a regex guru.
On the command line, remembering that $_ is the final argument of the last command saves a ton of typing.
To get yesterday's date use TZ=GMT+24 date (assuming you're on GMT)
lsof is an incredibly useful tool - learn it and carry a copy around with you.
For showboating, nothing impresses the uninitiated like proficient use of a line editor.
A function I often use in scripts which produce a lot of output is this;
underline() {
echo $* | sed -e 'p;s/./=/g'
}
which easily produces nice, clear underlined headings.
A neat but not particularly useful Korn shell trick, (don't know if it works in bash) is
cat <
which lets you read output from port x on the local host.
Other stupid tricks are things like using echo * instead of ls, and all the "funnies" like drink bottle; opener.
Pushd and popd made my day when I learned about them. I now never use unix without 'em!
UGU offers a Unix tip of the day at http://www.ugu.com/sui/ugu/show?tip.today - that should keep you in tips for a while.
Why can't I moderate something "Wrong" or at least "Grossly Misinformed"?
for f in `find /my/resumes -name "*.txt" `; do mv $f $f.bak; sed 's/beer/water/g' $f.bak >$f; rm -f $f.bak; done
Here is a little script I use to create directory snapshots. I used Perl, but it would be easy enough to do it an any shell scripting environment:
...\n";
/usr/local/bin as "myarch". I can be browsing anywhere, such as my /home/rycamor directory, and I can just enter "myarch mail" to archive the /home/rycamor/mail directory, saving it as /home/rycamor/mail_06-02-2002_1615.tar.gz (the command it executes is simply "tar -zpscf mail_06-02-2002_1615.tar.gz mail/*")
#!/usr/bin/perl -w
$dir_in=shift(@ARGV);
chomp($dir_in);
$dir_in =~ tr/\///d;
print "Archiving $dir_in/*
use POSIX qw(strftime);
$now_string = strftime "%m-%d-%Y_%H%M", localtime;
$tarname=$dir_in."_".$now_string.".tar.gz";
$execstring="tar -zpscf ".$tarname." ".$dir_in."/*";
print($execstring);
system($execstring);
print("\n");
What this does is a create a standard Gzipped tarfile of a directory tree, preserving attributes, etc... and naming this archived file with the date and time appended to the directory name. Let's say this script is saved in
For any directory where you are doing something questionable, and you might want to create several backups as you go, this is a nice quick way to keep track of changes.
A while back I downloaded the latest CVS version of gaim (20020402) and decided to have a play around to see which maximum number of jobs gave the best compile times. My box is a dual celeron 550 with a modest 128MB of RAM. Results are as follows:
time make -j1
real 4m6.294s
user 3m27.700s
sys 0m48.820s
time make -j2
real 2m50.786s
user 3m36.000e
sys 0m49.190s
time make -j3
real 2m46.357s
user 3m30.230s
sys 0m49.410s
time make -j4
real 2m45.729s
user 3m31.810s
sys 0m50.790s
Switching to two or more maximum jobs gave a huge difference in compile times. Above -j2, there wasn't too much difference, with -j3 giving the (marginally) fastest compile times. Overall, multiple jobs are your friend if you hate waiting around longer that you have to for make jobs to finish.
Incidentally, if you like strace on Linux, the truss command under Solaris is very similar.
For really complex problems, be sure to look into the -o and -f flags.
--JoeProgram Intellivision!
ssh -R.
- A.P.
"Remember when the U.S. had a drug problem, and then we declared a War On Drugs, and now you can't buy drugs anymore?"
xrdb -merge
What about this little script I run at login...
if [ "$DISPLAY" != "" ] ; then
TTY_NAME=`tty | sed -e s/'\/dev\/'//`
IPNUM=`who | grep $TTY_NAME | awk '{ print $6 }' | sed -e s/'('// | sed -e s/')'//`
DISPLAY=$IPNUM:0
export DISPLAY
fi
It automagically detects if I'm tunneling X through ssh.
I tend to use aliases quite a lot to ease any routine trasks that pops up, especially for swithing between aløternative environment variable settings. .cshrc. Use awk to insert the preceding 'alias' and any quotes you might need.
One really useful tool is therefore an alias to save the current alias list to an alias file. The file is sourced from my
I used to think 'cd = pushd' was really handy, but after a while of having the equivalent of a 'back' button, I started missing 'forward', so I hacked up a couple of script replacements for pushd and popd that lets me do that. Never got around to debug them properly after they did most of what I need, so I'm not posting them.
not exactly rocket science, but:
For some shells, 'ls = ls -F' is indispensible.
ll = ls -l is a classic. I tend to also define
la and lla. Guess what they do.
sudo ergo sum
Probably posting too late for anyone to read this, but here goes. I used to write the Tip of the Week column for LinuxLookup. It hasn't been updated since September, but there's still a year's worth of tips on interesting ways of using various Unix commands, shell tricks, and other tools. The last columns I wrote and the archive of past articles are all here.
To make all of your files and directory world-readable with one command:
.
$ chmod -R go+rwX
(the X is the key, you don't have to worry about executables vs. directories)
Need a list of numbers of letters or anything? Check out jot (on FreeBSD, maybe on others):
$ jot 3
1
2
3
$ jot -w %c 3 65
A
B
C
(This is especially useful with for loops - "for i in `jot 20`;do touch foo.$i;done" will generate foo.1 through foo.20. Extra hint: -w %02d will give you a leading zero on 0-9)
grep's -A, -B, and -C flags can be very useful. Using them, you can have grep display the lines immediately before and/or after a match.
If you have a lot of files in a directory, so many that "*" complains of the argument length, bash (and probably other shells) can let you get around this by changing your command to one using a for loop. It won't be as fast, but it won't churn for several minutes only to tell you it won't work. ;)
instead of:
$ rm *
do
$ for i in *;do rm $i;done
You can get a list of all files in your directory excluding . and .. with 'ls -A'.
One of the most annoying things about 'less', at least in Debian and probably other dists, is that it clears the screen when you exit. Ugh! You can fix this problem by setting PAGER='less -X' in your environment.
sed '/^$/d' to delete blank lines
sed 's/ *$//' to remove trailing spaces
sed 1d to delete the first line of something
Google turned up a couple sed faqs:
http://www.dreamwvr.com/sed-info/sed-faq.html
http://www.faqs.org/faqs/editor-faq/sed/
I love 'while' loops from the command line. For instance, to monitor disk space every second:
..|grep xxx" or whatever.
tcsh% while 1
while? df -k
while? sleep 1
while? clear
while? end
Also good to monitor who's logged in with "who", processes "ps
# Make less behave more pleasantly:
export LESS=FMqai
# Control behavior of all incarnations of make without command line arguments (convenient for multiprocessors):
export MAKEFLAGS='-j 2 -l 2.5'
# Start lynx with the freshmeat.net search for the given string:
fm() {
lynx "http://freshmeat.net/search/?q=${1} useful for scripting languages:
vw() {
vim `which $1`
}
#!/usr/bin/perl -w
# Here's a (probably too-long) Perl script to manipulate my command history. It filters out all but the most-recent duplicates; this means I get to keep more of my elaborate commands for later.
use File::Copy;
my $origfilename = shift;
my $infilename = "$origfilename";
while (-e $infilename) { $infilename
move($origfilename, $infilename);
open IN, $infilename;
open OUT, ">$origfilename";
my %inlines;
my $whichline = 0;
while () { $inlines{$_} = $whichline++; }
foreach (sort { $inlines{$a} $inlines{$b} } keys %inlines) { print OUT; }
close OUT;
close IN;
#!/usr/bin/perl
# This one is nice; you give a regex argument and a replacement argument, and a list of files.
# For each file in the list, rename the file to the result of the regex substitution.
# e.g., sedmv 'NIN' 'Nine_Inch_Nails' NIN*.mp3
use File::Copy;
my $pat = shift;
my $repl = shift;
my @files = @ARGV;
foreach my $f (@files) {
my $newf = $f;
$newf =~ s{$pat}{$repl}g;
move($f, $newf) if $f ne $newf;
}
And of course there's my user-monitoring program.
It alerts you to logins, logouts, idle time, 'mesg' status, and 'w'hat people are doing, as things happen, for a specified buddy list or all users, in color, with very little load on the system.
Here is a script I put together to find the last day of the month using 'cal' -useful for checks in a cron job for only the last day of the month.
#!/bin/ksh
integer count=0
cal | while read A B C D E F G
do
((count+=1))
if ((count>3)) ; then
if [[ -z $A ]] ; then
break
elif [[ -z $B ]] ; then
lastday=$A
break
elif [[ -z $C ]] ; then
lastday=$B
break
elif [[ -z $D ]] ; then
lastday=$C
break
elif [[ -z $E ]] ; then
lastday=$D
break
elif [[ -z $F ]] ; then
lastday=$E
break
elif [[ -z $G ]] ; then
lastday=$F
break
else
lastday=$G
fi
fi
done
print "$lastday"
exit 0
Ctrl-R in Bash helps a good bit. It's a reverse search just as within emacs. It searches for the last command matching what you type in.
Speaking of, I find it very helpful that Bash recognizes a lot of emacs commands, eg Ctrl-A, etc.
Not much, but helpful for me
One of my favourites not mention yet is using "suspend" to suspend a bash session and then continue it with "fg". E.g. log in, "su" to root (creates a new shell), do something, then "suspend" the root shell going back to regular user, and then "fg" back to the root shell without typing a password. Alleviates the annoyance of typing the root password for every small chore you need done as root...
starting with already typed prefix, i.e.
would find `make clean'
Now that you've got a way of disabling bash_history, you might want to try the following script...
#!/bin/sh
for i in $(seq $3 $4)
do wget $1"$i".$2
done
Its pretty simple. Usage: stileget filename extension start number end number.
I won't tell you what it does, but try:
./stileget.sh http://static.stileproject.com/p00p/north wmv 1 9
./stileget.sh http://static.stileproject.com/p00p/ssaint wmv 1 5
./stileget.sh http://static.stileproject.com/p00p/analz wmv 1 6
./stileget.sh http://static.stileproject.com/p00p/tina wmv 1 4
./stileget.sh http://static.stileproject.com/p00p/bal wmv 1 6
./stileget.sh http://static.stileproject.com/p00p/bal mpg 1 6
Good isn't it? You'll need xine or mplayer to exploit the full power of this excellent Unid shell script. You can get more filenames, extensions, start numbers and end numbers at www.stileproject.com
Throw in a dev null, duh!
find . -type f -exec grep "find this" {}
or xargs if you prefer. Anyway-- you gotta have another "file" to grep for grep to tell you what file was the text was found in.
"You may all go to hell and I will go to Texas"
Sen. Davy Crocket to US Congress, Nov. 1, 1835
Corporate Gadfly
Jonathan Archer: the most beaten up Enterprise captain in Star Trek history