#234   - [1] +
  Tags: sed Click here to Share / Save
unwrapping ldapsearch's ldif output
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 //; }; '



  #233   - [-1] +
  Tags: history sed Click here to Share / Save
Remove numbers from history
Use the following command to give a history listing without the numbers for easier copy and pasting:

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


  #232   - [1] +
  Tags: find xargs rm Click here to Share / Save
Deleting difficult filenames
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


  #231   - [0] +
  Tags: find perl Click here to Share / Save
Rename many files using find and perl
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

  #228   - [-3] +
  Tags: kill ps Click here to Share / Save
kill process in one line
kill -9 `ps -fea | grep java | grep -v grep | awk '{print $2}'`


  #226   - [1] +
  Tags: No tagsClick here to Share / Save
Open port
iptables -A INPUT -p tcp -i eth0 --dport X -j ACCEPT

  #224   - [2] +
  Tags: cat Click here to Share / Save
Add header/footer to command output
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


  #223   - [8] +
  Tags: No tagsClick here to Share / Save
Suspend ssh session
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.

  #222   - [1] +
  Tags: No tagsClick here to Share / Save
Convert tabs to spaces
Use 'expand' to convert tabs to spaces:

$ expand MyFile.txt > notabs


  #221   - [4] +
  Tags: No tagsClick here to Share / Save
fsck my life
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




  #220   - [4] +
  Tags: awk tail Click here to Share / Save
Actively Monitor a File
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") }'


  #219   - [1] +
  Tags: No tagsClick here to Share / Save
Record your session
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.

  #218   - [1] +
Edit previous commands with fc
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.

  #217   - [0] +
List user accounts on a system
cut -d: -f1 /etc/passwd | sort
OR
getent passwd|cut -d: -f1|sort


  #216   - [4] +
Create a Terminal Calculator
Put the following in your .bashrc file

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


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


  #215   - [2] +
Calendar with today colored [handle single digit dates]
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)\(\ \|\$\)\|\$


  #213   - [0] +
Howto get DVDs playing on Ubuntu Gutsy
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


  #211   - [3] +
Remove comments and blank lines
sed '/ *#/d; /^ *$/d' file

Remove comments and blank lines from file

  #210   - [3] +
Calendar with current date
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') /"


  #209   - [4] +
Directory Tree
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/"'


  #208   - [0] +
Pipe files to an archive
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


  #207   - [0] +
Run commands on logout
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


  #206   - [4] +
Get your IP address
lynx -dump http://whatismyip.com | awk '/^Your/ {print $5}'


  #205   - [3] +
Convert permissions to octal
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!

  #204   - [2] +
Find matching programs
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