//Browsing archives (Page 9 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


This trick allows you to use a process *almost* anywhere you can use a file. To illustrate, let's consider the diff command. Most versions of diff require you to pass exactly two file names as arguments. But what if we want to diff something, like the contents of a directory, that doesn't necessarily exist in a file? This is where we can use process substitution. For example, to diff the contents of two directories, you could use:

diff <(find dir1) <(find dir2)


View Comments »



Print a file until a regular expression is matched.
cat file.txt | perl -pe "exit if(/Last line we want/)"


View Comments »

Remove all empty directories within the current directory

find . -type d -empty -exec rmdir {} \;

Or another way to do it: perl -MFile::Find -e"finddepth(sub{rmdir},'.')"


View Comments »

<esc>-. (that's 'escape' followed by '.') inserts the last arguments from your last command. It comes in handy more than you think.

cp file /to/some/long/path
cd <esc>-.

This can be more useful than using '!!' as the argument can be edited after insertion.


View Comments »

This script displays the contents of files (or stdin) in ascii, hexadecimal, decimal, octal, and binary formats.


#!/usr/bin/perl undef $/; # slurp files while( $content = <> ) { $offset = 0; print "OFFSET ASC HEX DEC OCT BIN\n"; while( length $content ) { $n = ord( substr( $content, 0, 1, '' ) ); printf "%08x %c %2x %3u %3o %s\n" , $offset, , ( $n > 0x1f && $n < 0x7f ) ? $n : ord '.', , $n, , $n, , $n, , substr( unpack( "B*", pack( "n", $n ) ), -8 ) ; $offset++; } }



View Comments »

The line below will give you the real names of the users currently logged in:

grep -f <(w -h|awk '{print $1}') <(getent passwd) | awk -F":" '{print $5}'


View Comments »

Add this line to your .bashrc
PS1='C:${PWD//\//\\\}>'

Why would you do it? You shouldn't; unless you have something for dos prompts. But you could always do this to fool your coworkers


View Comments »

bash -n scriptname.sh

This validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like ech hello instead of echo hello.


View Comments »

Counts files in the current directory and subdirectory
alias lll='for i in *; do echo "`ls -1aRi  $i | awk "/^[0-9]+ / { print $1 }" | sort -u | wc -l` $i" ; done | sort -n'

found is vserver-tools


View Comments »

To print only the 3rd line:
sed -n 3p file


To print lines from 3 through 7:
sed -n 3,7p file


View Comments »

My little "iso2cd" alias. Not clean, but handy. The Burning device will be auto detected.

example call:
iso2cd debian_lenny_final.iso

alias iso2cd="cdrecord -s dev=`cdrecord --devices 2>&1 | grep "\(rw\|dev=\)" | awk {'print $2'} | cut -f'2' -d'=' | head -n1` gracetime=1 driveropts=burnfree -dao -overburn -v"


View Comments »

On client machine
nc -lpvv port > file

On server machine
nc -vv clientip port < file


Example :
Client: nc -l -p 6868 > file.txt
Server: nc 31.41.59.26 6868 < file.txt


View Comments »

Have a bunch of garbled text? Curious as to what words might be found inside? Try the following:

echo "Garbled Text" | grep -o -F -f /usr/share/dict/words | sed -e "/^.$/d"


Breakdown:

The grep -o -F -f /usr/share/dict/words takes whatever xclip told it and finds all the words. -F means "I'm going to tell you a list of patterns to match against, not just one." -f means "Not just a normal pattern, but everything inside this file." /usr/share/dict/words is the system dictionary. -o means "Just tell me what you found, not the entire thing."

sed -e "/^.$/d" means "Get rid of every word that's a single letter long.

For example:

echo asjdpastaxrdsdtasteifcoinade | grep -o -F -f /usr/share/dict/words | sed -e "/^.$/d"

Gives the following list of words: as, pasta, taste, if, coin, ad

For a more complicated version, this can be combined with xclip to work with the clipboard. The following command will find words in the text currently copied and place a comma separated list of words back in the clipboard.
(for i in `xclip -o -selection clipboard | grep -o -F -f /usr/share/dict/words | \
sed -e "/^.$/d"`; do echo -n "$i, "; done) | tee >(xclip -selection clip)


View Comments »

mplayer -ao pcm -vo null -vc dummy -dumpaudio -dumpfile <output-file> <input-file>


View Comments »

If you bork your terminal by sending binary data to STDOUT or similar, you can get your terminal back using the reset command rather than killing and restarting the session. Just type 'reset' at the command line (note that you often won't be able to see the characters as you type them).


View Comments »

Display the top ten running processes - sorted by memory usage

ps aux | sort -nk +4 | tail

ps returns all running processes which are then sorted by the 4th field in numerical order and the top 10 are sent to STDOUT.


View Comments »

echo | openssl s_client -connect www.google.com:443 2>/dev/null |openssl x509 -dates -noout

Remotely connects to an https site, fetches the SSL certificate and displays the valid dates.


View Comments »

!command:p

!command will search your history and execute the first command matching 'command'. If you don't feel safe doing this put :p which prints the command rather than executing it.


View Comments »

If you're having problems unmounting a volume or other situations where a process accessing a file can stop you, the following will kill the process accessing 'filename'.

fuser -k filename


View Comments »

In bash scripting if you have a situation where you don't want to wait forever for a user to respond, you can use the read command with the -t option which causes read to time out in "number of seconds" specified.

From read command man page:

-t timeout : Cause read to time out and return failure if a complete line of input is not read within timeout seconds. This option has no effect if read is not reading input from the terminal or a pipe.

-p prompt : Display prompt, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.

Example:
$ ans="y"
$ read -t 5 -p "Want to proceed ?(y/n)" ans; echo "You entered $ans"

This is also useful as a replacement for the sleep command as it can be cancelled more easily. For example instead of 'sleep 60':
read -t 60 -p "Press enter to continue"


View Comments »

Typically when one types the history command, it displays the command number and the command. For auditing purposes it would be helpful to display the timestamp as well. To do so we need to set the environmental variable HISTTIMEFORMAT.

HISTTIMEFORMAT supports format strings of strftime.

Some important format strings:

%T Replaced by the time ( %H : %M : %S )
%F Equivalent to %Y - %m - %d

$ export HISTTIMEFORMAT='%F %T '

Now execute
$ history

it will print the command line history with corresponding timestamp when the command was executed, such as:
499  2009-02-11 18:20:52 ls


View Comments »

Here's a simple one liner you can use to syntax check all php files in your working directory.
find . -type f -name "*.php" -exec php -l {} \; | grep -v 'No syntax errors'


View Comments »

Suppose we have to find the differences between local file "/tmp/localfile" and remote file "/tmp/remotefile" located on remote host 123.4.5.6

This is how can do it:
$ ssh user@123.4.5.6 "cat /tmp/remotefile" | diff - /tmp/localfile

And using vimdiff:
$ vimdiff scp://user@123.4.5.6//tmp/remotefile /tmp/localfile

(of course we would need ssh to work using public key authentication so that we can do remote commands execution without being prompted for passwords).


View Comments »

You can use the following to run a command only when the load average is below a certain level

echo "cp /large_folder /backup/" | batch

This is great for one off jobs that you want to run at a quiet time. The default threshold is a load average of 0.8 but this can be set using atrun.


View Comments »






Home Latest Browse Top 25 Random Hall Of Fame Contact Submit