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


Sometimes you want to find a program without knowing the full name. This can be done with the following one liner:

IFS=: ; for D in $PATH; do for F in $D/*PATTERN*; do [ -x $F ] && echo $F; done; done

For example:
$ IFS=: ; for D in $PATH; do for F in $D/*text*; do [ -x $F ] && echo $F; done; done
/usr/bin/gettext
/usr/bin/glib-gettextize
/usr/bin/gnome-text-editor
/usr/bin/xgettext


View Comments »



Converts the symbolic permissions to octal (ie: numbers) when using 'ls -l':

$ls -l | sed -e 's/--x/1/g' -e 's/-w-/2/g' -e 's/-wx/3/g' -e 's/r--/4/g' \
 -e 's/r-x/5/g' -e 's/rw-/6/g' -e 's/rwx/7/g' -e 's/---/0/g'

-755  1 jrl jrl  111943 2003-10-21 19:57 logscan
-644  1 jrl jrl   35468 2003-11-23 16:13 htfoo
-700  1 jrl jrl 3100672 2004-05-15 17:00 mutt
-644  1 jrl jrl   10162 2005-02-22 14:14 joinstep2.php
-777  1 jrl jrl   41079 2005-04-21 13:02 setistats
d755  2 jrl jrl      47 2007-10-26 14:41 rf
-700  1 jrl jrl     104 2008-02-05 11:26 getc
If you're going to use this, you may want to make an alias rather than type it in each time!


View Comments »

If a file named $HOME/.logout (a file named .logout in your home directory) exists, and the following trap statement is in your .profile, .logout is executed when you logout.

Add this to .profile:
trap "$HOME/.logout" 0


View Comments »

If you want to select specifically the files to add to an archive you can pipe the output from find (or any command that gives a list of files) to cpio:

$ find ./dir/ | cpio -o --format=tar > archive.tar
or
$ find ./dir/ | cpio -o --format=tar -F test.tar


View Comments »

The following alias will print the directory structure from the current directory in tree format.

alias dirf='find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"'


View Comments »

Add the following sed commands to cal to get a calendar with the current date marked:

cal | sed "s/^/ /;s/$/ /;s/ $(date +%e) / $(date +%e | sed 's/./#/g') /"


View Comments »

Get DVDs playing on Ubuntu Gutsy by installing libdvdcss2.


echo "deb http://packages.medibuntu.org/ gutsy free non-free" | sudo tee -a /etc/apt/sources.list echo "deb-src http://packages.medibuntu.org/ gutsy free non-free" | sudo tee -a /etc/apt/sources.list wget -q http://packages.medibuntu.org/medibuntu-key.gpg -O - | sudo apt-key add - && sudo apt-get update sudo apt-get install libdvdcss2


View Comments »

If your version of grep supports coloring matches, you can use the following to give a calendar with the current date colored:

cal | grep --color=auto -E  \(\ \|^\)$(date +%e)\(\ \|\$\)\|\$


View Comments »

Put the following in your .bashrc file

function calc { echo "${1}"|bc -l; }


Usage:
$ calc 2+2
4
$ calc "sqrt(2)"
1.41421356237309504880


View Comments »

Use the 'fc' command to open your previous command for editing in your default editor. Saving the changes will then run the edited command.

'fc -l' will show a list of previous commands to edit and 'fc ' will open a command from the list for editing.


View Comments »

Record eveything printed on your terminal screen with the following command:
$ script -a 

Now start doing stuff and everything is appended to . Leave out the -a to overwrite the file.


View Comments »

This is a way to monitor "/var/log/messages" or any file for certain changes.
The example below actively monitors "stuff" for the word "now" and as soon as "now" is added to the file, the contents of msg are sent by email

$ tail -f stuff | awk ' /now/ { system("mail -s \"Now Occured\"  mail@foo.com < msg") }'


View Comments »

To do a filesystem check on all of your partitions, here's a quick one-liner. It parses out /etc/fstab for the partitions and runs a fsck on them.
for dev in `grep dev /etc/fstab | grep -v \#`; do fsck ${dev}; done


View Comments »

Use 'expand' to convert tabs to spaces:

$ expand MyFile.txt > notabs


View Comments »

Suppose you have just sshed into a computer and you want to get back to the terminal prompt of the computer you started with. Escape, by default with ssh is "~", so enter "~" followed by "ctl-z" to suspend. You can then use 'fg' to go back to the ssh session.


View Comments »

If you want to surround the output of a command by a header or a footer try the following:

$ command | cat headerFile - footerFile


For example
$ ls *txt | cat header - footer
Here is a list of files:
1.txt
2.txt
End of file list


View Comments »

iptables -A INPUT -p tcp -i eth0 --dport X -j ACCEPT


View Comments »

Rename a lot of files at once:
find . | perl -ne'chomp; next unless -e; $oldname = $_; s/aaa/bbb/; next if -e; rename $oldname, $_'

Change 'aaa' and 'bbb' to what you want to find and replace in the filename


View Comments »

To delete a file who's file name is a pain to define (eg. ^H^H^H) find it's inode number with the command "ls -il". Use the line below to find and delete the file.
find . -inum 12345 | xargs rm


View Comments »

Use the following command to give a history listing without the numbers for easier copy and pasting:

history | sed 's/^[ 0-9]* //'


View Comments »

I'm working on a project to access data from my corporate Active Directory server using ldapsearch. The version of ldapsearch that comes with Red Hat Enterprise Server spews out LDIF in column truncated format. That is to say it inserts a carriage return at column 80 of the output.

This sed goodie unwraps the ldif output.

UNWRAP=' /^ / {; H; d; }; /^ /! {; x; s/\n //; }; '


View Comments »






Home Latest Browse Top 25 Random Hall Of Fame Contact Submit