//Browsing archives (Page 1 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 the -p option to mkdir and make all parent directories along with their children in a single command.

mkdir -p tmp/a/b/c

View Comments »

Use && to run a second command if and only if a first command succeeds:

cd tmp/a/b/c && tar xvf ~/archive.tar

View Comments »

Add commas to all numeric strings in a file, changing "1234567" to "1,234,567"

sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' filename

View Comments »

Add a leading angle bracket and space to each line (quote a message)

sed 's/^/> /' filename

View Comments »

Place a filename at the beginning of the line to make it easier to edit the search at the end of the command.

$ </var/log/messages grep foo

$ </var/log/messages grep bar
$ </var/log/messages grep user1

View Comments »

Grepping for a process will return the grep command, this can be avoided by adding '| grep -v grep' to a command or easier in some cases altering the regular expression by adding brackets around a character.

ps | grep 'ss[h]'

The regular expression 'ss[h]' matches the literal string 'ssh' when it appears in the process list, but does not accidentally match the string 'ss[h]' when it appears in the process list as 'grep ss[h]'.

View Comments »

It can be useful to not only know what has gone to stdout and stderr but also where they occurred with respect to each other.
Allow stderr to go to err.txt, stdout to out.txt and both to mix.txt

((./program 2>&1 1>&3 | tee ~/err.txt) 3>&1 1>&2 | tee ~/out.txt) > ~/mix.txt 2>&1

View Comments »

Don't search history by grepping ~/.bash_history, or repeatedly hitting the up arrow, instead use CTRL+r (or '/' in vi-mode) for search-as-you type. You can immediately run the command by pressing Enter.

View Comments »

Instead of adding a prefix to the previous command with 'Up-arrow Home prefix Space' try 'prefix !!' to repeat the last command with 'prefix ' before it.
You can also use '!-n' to use commands other than the most recent.

View Comments »

Running a second command with the same arguments as the previous command, use '!*' to repeat all arguments or '!:2' to use the second argument. '!$' uses the final argument.

$ cd /home/user/foo

cd: /home/user/foo: No such file or directory
$ mkdir !*
mkdir /home/user/foo

View Comments »

Use awk to change the file extension for a group of files. For example to change all .htm files to .php:

ls *htm | awk -F. '{print "mv "$0" "$1".php"}' | sh

This can be tested first by leaving the '| sh' off to give a list of the commands that will be executed.

View Comments »

mknod backpipe p; while nc -l -p 80 0<backpipe | tee -a inflow | \
nc localhost 81 | tee -a outflow 1>backpipe; do echo \"restarting\"; done

Listen on localhost:80, forward to localhost:81 and log both sides of the conversation to outflow, automatically restarting if the connection dies.

View Comments »


tar cf - sourceFiles | gzip -c > fileToCreate.tgz


zip -d -c fileToExtract.tgz | tar xf -

View Comments »


#KSH only.
# Author: simonvc
# Purpose: Count the total number of occurances of a pattern in all files in the cwd.
# Alt: you could also do "$(grep $pattern * | wc -l) but where's the fun in that ;-)


$ echo $(($(grep -hc $pattern *| tr "\n" "+")"0"))

View Comments »

Prefer Perl over Sed? No problem! To use Perl as a Sed-like program:
perl -pe 's/foo/bar/; etc'

Have a Perl script ready? No problem!
perl -p foo.pl

View Comments »

GNU tar comes with native support for gzip, bzip2, and compress (adaptive LZ, LZW). However, many other useful compression algorithms exist, but most implementations of them don't support all the file system metadata that tar does. There are two general methods to using tar with arbitrary compression programs: via an option in tar itself and via piping. The first:
tar -cf foo.tar.ext --use-compress-program generic_compression_program file1 [file2] [file3] [...]

Note that this method is not very configurable and only works with programs that compress by default and will decompress files when invoked with the "-d" option.

The second method:
tar -c file1 [file2] [file3] [...] |generic_compression_program >archive.tar.ext

This method is more versatile, but it requires a bit more shell voo-doo to work (and may not require use of stdout redirection). For instance, to make a .tar.7z archive:
tar -c list of files and directories to compress |7z a -si outfile.tar.7z

To uncompress said archive:
7z e -so infile.tar.7z |tar -x

View Comments »

multiple command output into a single program:

diff -u <(ls -c1 dir_1) <(ls -c1 dir_2)

Will show you a diff of files in the root of dir_1 and dir_2

View Comments »

Using expansion to move a file aside without having to type the file name twice

cp ReallyLongFileNameYouDontWantToTypeTwice{,.orig}

View Comments »

Rename replaces string X in a set of file names with string Y.

rename 's/.html$/.php/' *.html

This will change the extension of every .html file in your CWD to .php.

View Comments »

Mail somebody about space running low in some path (ksh, bash):

PATHS="/export/home /home"
DU="/usr/bin/du -ks"
DF="/usr/bin/df -k"
for path in $PATHS
DISK_AVAIL=`$DF $path | $GREP -v "Filesystem" | $AWK '{print $5}'|$SED 's/%//g'`
if [ $DISK_AVAIL -gt 90 ];then
echo "Please clean up your stuff\n\n" > $MAILFILE
$CAT $MAILFILE | $MAILER -s "Clean up stuff" $mailto

View Comments »

ls -A -l -h --color=auto

Does ls but with :
-column list format (think dos)
-human readable sizes (megabytes, kb)
-color if enabled

Save to your default settings !

View Comments »

alias webshare='python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"'

Want to show something on your machine to someone over the web? Don't copy it or upload it somewhere. Just run "webshare" and the current directory and everything beneath it will be served from a new web server listening on port 8000. When your pal is finished, hit control-c.

View Comments »

Throw this in your command pipeline:

perl -ne '$sum += $_;}{ print "Sum: $sum\n";'

closes the
while ( <> ) {
loop that Perl wraps your code in (because of the
) and opens a new one that will only execute after the while loop has finished. You can use this in lots of Perl one-liner tricks.

For a contrived example, to sum the sizes of files in the current directory (learn the joys of cut(1) first):

ls -l | cut -c 30-42 | perl -ne '$sum += $_;}{ print "Sum: $sum\n";'

cut grabs characters 30-42 from stdin, which happen to coincide with my file size column in ls.

View Comments »

example, look for all the TODO and HACK strings I left in large java project and show a bit of context before and after using the -C switch of grep.

find path_to_start | grep \\.java | xargs egrep -C3 "TODO|HACK" | less

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit