//Browsing archives (Page 2 of 13)
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow


Use CTRL+A to move to the beginning and CTRL+E to move to the end of a the current line


View Comments »



Loop over files with spaces in the name:

(Wrong) example:
$ touch a

$ touch b\ c
$ for i in *; do ls $i; done;
a
ls: b: No such file or directory
ls: c: No such file or directory


The trick is to change the separator ($IFS) to newline:
old_ifs=$IFS

IFS="
"
$ for i in *; do ls $i; done;
a
b c


View Comments »

to run 'ls -lh' whenever you change directories in zsh, put this in ~/.zshrc

chpwd() { ls -lh }


View Comments »

Don't forget the bash fork bomb. DO NOT TRY THIS AT HOME... Posted here so that you don't see this in a forum or a mailing list and use it without knowing:

$ :(){ :|:& };:


Explanation:
:()
defines a function called : (accepts no arguments)
{ :|:& };
This is the function: It calls the function itself and pipes the output to the same function ":" and puts the process in the background. (Recursive invocation) with ; it ends the function definition
:
Calls the function and creates havoc.

- Emre


View Comments »

You can use ssh in conjunction with tar to pull an entire directory tree from a remote machine into your current directory:

ssh <username@sourcehost> tar cf - -C <sourcedir> . | tar xvf -

For example, let's say you have a "bsmith" account on a host called "apple". You want to copy those files into your "bobsmith" account on a host called "pear". You'd log into your "bobsmith@pear" account and type the following:

ssh bsmith@apple tar cf - -C /home/bsmith . | tar xvf -

This technique is useful when you have insufficient disk space on the source machine to make an intermediate tarball.


View Comments »

zero out the hard drive /dev/sdc, with progress bar

pv /dev/zero | dd of=/dev/sdc bs=4096


View Comments »

/bin/mail -s "Enter Subject Line here" -a
/home/.../whateverFileYouWantAttached.txt
"you@YourEmailAddress"
< /home/me/textBody.letter


View Comments »

Perform a recursive grep of the Solaris /etc directory, bypassing those nasty sockets Sun keeps in there:
grep foobar `find /etc/* -type f -print`


View Comments »

use `grep -o` to select only the pattern matched rather than the whole line:


grep -o 'GET [^ ]\+ ' /var/log/httpd_access.log | sort | uniq -c | sort -n | head -n 100 > top100



View Comments »

sort a file by the first column in dictionary order, then by the second column numerically.


sort +0d -1 +1n -2 file.txt


View Comments »

For a fix of #67 that is a bit nicer

for support spaces in the arguments just fine, just make sure you quote "$i".

$ touch a

$ touch b\ c

$ for i in *; do ls "$i"; done;



View Comments »

Sort a file by line length:


cat test.txt | awk '{ printf "%06d|%s\n",length($0),$0}' | sort | cut -b'8-'


View Comments »

Subtract a small file from a bigger file.


grep -vf filesmall filebig


View Comments »

Overwrites a file with zeroes.
Put in in your .bashrc or .bash_profile in Mac OS X.

zero() {

case "$1" in
"") echo "Usage: zero <file>"
return -1;
esac
filesize=`wc -c "$1" | awk '{print $1}'`
`dd if=/dev/zero of=$1 count=$filesize bs=1`
}


View Comments »

Q: If I resize my xterm while another program is running, why doesn't bash notice the change?
http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11)

Bash won't get SIGWINCH if another process is in the foreground.
Enable checkwinsize so that bash will check the terminal size when
it regains control.

Put this in your bash config somewhere (e.g. Gentoo has it in /etc/bash/bashrc):

shopt -s checkwinsize


View Comments »

A slight improvement to #64 -- drops one grep and adds line numbers


find _path_to_start_ -name '*.php' | xargs egrep -nC3 'TODO|HACK' | less


View Comments »

Forget that your running as underprivileged? No need to retype the command.

> command_with_insufficient_permissions
Permission denied

> sudo !!


View Comments »

In bash (or anything using libreadline, such as mysql) press ALT+. to insert the last used parameter from the previous line.

Eg:

$ vim some/file.c
$ svn commit


View Comments »






Home Latest Browse Top 25 Random Hall Of Fame Contact Submit