Linux Tips
This is a small collection of Linux and KDE tricks to jog my memory. Maybe others will find them useful.
Emacs
- Replace carriage returns (^M) with line-feeds in EMACS:
replace-string: C-q C-m with: C-q C-j
Replacing C-q C-j with " " (SPACE) also works if you don't want to add an LF - Get rid of audible bell in EMACS by adding this line to your
config
file:
(setq visible-bell t)
DCOP
Shell script to rename a konsole window with an application namedcop $KONSOLE_DCOP_SESSION renameSession "Matlab R14"
/home/rob/Matlab7.0/bin/matlab -nodesktop
dcop $KONSOLE_DCOP_SESSION renameSession Shell #Rename back to "Shell"
Bash: Complex find operations
- Descend a directory tree and count the number of unique .txt
files. The loop is designed to feed a variable to the string chop
command in order to get rid of of the file's relative path, leaving
only the file name.
find . -name '*.txt' | while read i; do echo ${i##./*/};done | sort | uniq | wc -l
Or, a faster and neater version with no loop uses sed and the -u flag to sort:
find . -name '*.txt' | sed 's:.*/::' | sort -u | wc -l
[In case you're confused, the ':' in the sed regular expression are being used instead of the usual '/'. This is to avoid needing to escape '/' which appear in directory paths, making the expression easier to read.] - Descend a directory tree, find duplicate .txt files, and display
their
relative locations.
find . -name '*.txt | while read i; do echo ${i##./*/};done | sort | uniq -d | xargs -I '{}' find . -name {} - Delete .txt files in TARGET_DIR which have identical names to
files in DIR_A.
find ../DIR_A/ -name '*.txt | while read i; do rm -f ./TARGET_DIR/${i##../*/};done
Run a Matlab job in the background
With this line you can log in to a remote machine, start a Matlab job
(or anything else for that matter),
then log off again. The machine will automatically quit Matlab once
it's finished running your analysis.
nohup matlab7 < runBoot.m > /dev/null &
In case that looks like gibberish to you: "nohup" keeps the command
running even if you log off. "Matlab7"
is a script that starts Matlab from a
terminal (no GUI). "runBoot.m" is a Matlab script with the commands
you want to be run. The output of this is all fed to Linux's black
hole (/dev/null) and the ampersand gets back your command prompt.
List only directories
ls -l | grep "^d" or ls -dla */ .*/i.e. list detailed contents of current directory then pipe to grep and display only lines which start with the character "d"
Passwordless SSH
This is particularly useful if you want to automate backups to another machine over an SSH connection (e.g. with scp or rsync).- Generate a public/private key pair on the client machine
(i.e. that from which the ssh client will be invoked). Change
directory to /home/user/.ssh and execute the following command:
ssh-keygen -t rsa
Accept the default responses (by pressing enter) at all the prompts which follow. This creates a file called id_rsa.pub in the current directory. This file contains your public key. - Copy the contents of the id_rsa.pub file into a file called /home/user/.ssh/authorized_keys in your home directory on the server machine (i.e. that running the ssh daemon which will accept your connection). The authorized_keys file can contain a number of public keys, allowing passwordless incoming connections from various machines.
- You can now use ssh, sftp, and scp, without having to enter a password. The client uses the contents of the id_rsa file as a passport. This file contains your private key and should be kept safe from prying eyes.
Medline quick search
Ok, not really a Linux tip but to get Konqueror and Firefox to do a Medline quick search you can use this shortcut (for Konqueror. In Firefox substitute "\{@}" with "%s":http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=PubMed&term=\{@}