Tips

This page contains a collection of various tips, for different programs in Linux.
[ Top | Bottom ]

Bash shell

Navigation tips

Go to the previous directory cd -

Cut from standard command

ls -lt | tr -s " " | cut -f6-10

Rename a file with a filename that contains a special character

If you have a file named "Some silly (name)" and want to rename it, use the inode to rename it: ls -i "Some silly (name)" the "-i" flag will display the file's inode:
53273 Some silly (name)
Code listing 2.1
Now use find to rename the file, using the inode:
find . -inum 53273 -exec mv \{\} NewName \;

Show return code of program

echo $?

Search for a sting in files

I have the following function in my ~/.bashrc.
forall() {
if [ $# -eq 0 ]; then
        echo Usage 1: forall directory string
        echo Usage 2: forall directory filepattern string
        return 1
elif [ $# -eq 2 ]; then
        echo "Searching in directory: $1 for all files, after the string: $2"
        find $1 -type f -exec grep --color=auto -Hn "$2" {} \;
elif [ $# -eq 3 ]; then
        echo "Searching in directory: $1 for files with name: $2 after the string: $3"
        find $1 -iname "*$2" -type f -exec grep --color=auto -Hn "$3" {} \;
fi
}
Code listing 2.2

Replace typo in command

$ find . -naem "*.txt" -print
find: invalid predicate `-naem'
Code listing 2.3
The above command would be valid if we replace "em" (in naem) to "me"( to have name ). Use carrots to make this change
$ ^em^me^
find . -name "*.txt" -print
Code listing 2.4

For more information:

Find broken symlinks

There is a handy utility called symlinks, that can scan your system for broken (dangling) symlinks. I use the followering command to remove any broken symlinks: This can fsck your system bad!
sudo rm -i `sudo symlinks -r /usr/share/man | grep dangling | awk {'print $2'}
[ Top | Bottom ]

Miscellaneous

Multiple pages pr sheet

Create a ps file containing 4 pages: psnup -4 file.ps > file.ps, and print that.

List open ports on system

To get processes listed that are not owned by you, you need to run the program as root: sudo lsof -i TCP:1-65536

Xresources

Some programs, like xterm/rxvt/nedit, can be customized by editing the ~/.Xresources file. To load the settings, execute:
xrdb -merge ~/.Xresources
If you use ~/.xsession, remember to add the above.

For more information:

  • My .Xresources: containing settings for xterm/urxvt/Acrobat reader and X cursor theme

bin to iso: bin2iso cvs up -p -r 1.21 conclusion.tex a2ps -B -f 7 --columns=1 -R oplęg.txt -o oplęg.ps paper size: http://www.sm6rpz.se/a4.html http://www.euronet.nl/~mailme/index4.html utf 2 iso and back: iconv --from-code=ISO-8859-1 --to-code=UTF-8 ./oldfile.htm > ./newfile.html tip: Installere linux fra harddrive
[ Top | Bottom ]

Perl

Search and replace string

Search and replace a string in all files which have an extension of .text, using perl
perl -pi -e "s/org/replace/g;" 'find . -name "*.text"'

Convert UTF-8 to ISO and the other way

#!/usr/bin/perl -p
# Convert UTF-8 to iso8859-1, or to "{" "UTF8:" utf8-char "}" and return error.

# NOTE!Do not remove the code that checks if the UTF-8 sequence
#is valid and can be converted to iso8859-1.
#Otherwise filters like this can be fooled into converting
#some 8-bit chars to \0 or control characters.

# 1. octet in non-ASCII char should be [\300-\377].  However, we check
# for [\200-...] in case the input starts in the middle of a UTF-8 char.
s/([\200-\377][\200-\277]*)/&utf2iso($1)/ge;

sub utf2iso {
    my($first,@rest) = unpack('C*', $_[0]);
    $first -= 0xC2;
    if (@rest != 1 || ($first & ~1)) {
warn "\nutf2iso: Non-iso8859-1 characters(s) in text.\n" unless $w++;
return "{UTF8:$_[0]}";
    }
    chr($first * 0x40 + $rest[0]);
}

END { die "utf2iso: $w non-iso8859-1 characters.\n" if $w && $w > 1;
Code listing 4.1
#!/usr/bin/perl -wp
# Convert iso8859-1 to UTF-8
s/([\200-\377])/pack('CC', 0xC0 + (ord($1) >> 6), (ord($1) & 0xBF))/ge;
Code listing 4.2
[ Top | Bottom ]

ssh

Port forwarding

To forward a local port to a port on another host (destination-host:destport), you need access to a server which runs sshd (ssh-server), and which has access to the destination host:

ssh -N -L localport:destination-host:destport ssh-server

Restrict commands that root can run

Need to run backup as root? But you don't want to allow root login via ssh on your backup machine? This is how you solve it:

ssh-agent and .xsession

With the help of ssh-agent you only need to enter your password for your ssh-key once. To do this ssh-agent has to be running and then you can simply type: ssh-add in a xterm. I run ssh-agent from my ~/.xsession like this:
#! /bin/sh
ssh-agent $HOME/.xsession-real
Code listing 5.4
$HOME/.xsession-real contains my normal .xsession, which starts my window manager.
[ Top | Bottom ]

CVS

Creating a CVS repository

Create a CVS repository
mkdir cvs_repository
export CVSROOT=/path/to/cvs_repository
cvs init
Code listing 6.1
Now that the repository is ready you can import your sources:
cd sources
cvs import -m "Imported sources" modulename vendortag releasetag
Code listing 6.2
modulename : This directory will appear as $CVSROOT/modulename, so you can have multiple modules in one repository. vendortag and releasetag are described in the the cvs manual: Tracking third-party sources

If you would like CVS to write information about revision and date in the header of your files, paste one of there files, depending on the file type, into you file (using LaTeX as example):

%%% CVS version control block - do not edit manually
%%% $RCSfile: compiling-from-source.xml,v $
%%% $Revision: 1.3 $
%%% $Date: 2004/10/03 20:30:49 $
%%% $Source: /home/enrique/Development/CVS_REPOSITORY/Homepage/linux/software/compiling-from-source.xml,v $
Code listing 6.3
which is going to be expandet to:
%%% CVS version control block - do not edit manually
%%% $RCSfile: compiling-from-source.xml,v $
%%% $Revision: 1.3 $
%%% $Date: 2004/10/03 20:30:49 $
%%% $Source: /home/enrique/Development/CVS_REPOSITORY/Homepage/linux/software/compiling-from-source.xml,v $
Code listing 6.4

For more information:

Difference between two revisions

Difference between two revisions: cvs diff -r X -r Y filename where X and Y are revisions, or you can use cvs diff -D "1 hour ago" filename If you want a "Live checked out" directory, which always contain an uptodate version of you sources, you can do this by editing CVSROOT/loginfo: cvs co CVSROOT and then add the following:
^modulename (date; cat; (sleep 2; cd /path/to/live/checkout; cvs -q update -d;) &) >> $CVSROOT/CVSROOT/updatelog 2>&1
Code listing 6.5
$CVSROOT/CVSROOT/updatelog contains a log of the autocheck out.
[ Top | Bottom ]

LaTeX

Enable hyphenation for language

Hyphenation for languages like danish (dansk ordeling) is not enabled by default in LaTeX, to enable it, run texconfig as root. Select hyphen then latex, and uncomment the line with your language.

Preambel

If you are looking for an ok preamble for you LaTeX documents try this preamble.tex. The language is set to danish, but.... FIXME
[ Top | Bottom ]

Gentoo

Network profiles

Gentoo supports multiple runlevels (profiles). I use the profiles to control if my laptop should start the network services or not. Gentoo has it's profiles in /etc/runlevels. Use rc-update to edit which services should start in which profile.

Next add the following to /etc/lilo.conf or if you are using a PPC machine, /etc/yaboot.conf:

Image=/boot/vmlinux-2.6.7-mm5
        label=New
        root=/dev/hda4
        read-only
        append="softlevel=nonetwork"
Code listing 8.1
Note the "append" setting.

Installing PERL modules not in portage

If you want to install a PERL module that is not i portage, you can use the g-cpan.pl command to install modules from CPAN.
[ Top | Bottom ]
Hi! you have reached the old part of my homepage, be sure to checkout my new site