-------------------------------------------
#remove the annoying beeps on term
setterm -blengt disable beep
xset b 0 disable beep
#column modification
$ cat data
1 2 3
4 5
6 7 8 9 10
11 12
13 14
How to you get everything in 2 columns?
$ cat data|tr ' ' '\n'|xargs -l2
1 2
3 4
5 6
7 8
9 10
11 12
13 14
What's the row sum of the "three columns?"
$ cat data|tr ' ' '\n'|xargs -l3|tr ' ' '+'|bc
6
15
24
33
27
------------------------------------------
#Here's a quick way to edit all files whose contents contain a given string:
files=""; for i in `find . -type f -print`; do files="$files `grep -l
------------------------------------------
#Move back and forth between files using vim:
:n -- move to the next file
:rew -- move back
:e #3 -- edit the third file in buffer
:ar -- list files status
------------------------------------------
#The escape characters make the regex hard to read/write (For me that is) ie to add index.html at the end of each href - replace /" with /index.html"
find . -name "*.html" -print0 | xargs --null perl -pi -e 's/\/\"/\/index.html\"/'
------------------------------------------
#who owns a perticular port?
fuser -v -n tcp 20020
------------------------------------------
#To move a text file into upper case letters, you can use Awk in the following way:
awk '{ print toupper($0) }' old_file > new_file
------------------------------------------
#defaultfile permissions....
umask 077
------------------------------------------
#Hack 4 Creating a Persistent Daemon with init
Make sure that your process stays up, no matter what
There are a number of scripts that will automatically restart a
process if it exits unexpectedly. Perhaps the simplest is something
like:
$ while : ; do echo "Run some code here..."; sleep 1; done
If you run a foreground process in place of that echo line, then the
process is always guaranteed to be running (or, at least, it will try
to run). The : simply makes the while always execute (and is more
efficient than running /bin/true, as it doesn't have to spawn an
external command on each iteration). Definitely do not run a
background process in place of the echo, unless you enjoy filling up
your process table (as the while will then spawn your command as many
times as it can, one every second). But as far as cool hacks go, the
while approach is fairly lacking in functionality.
What happens if your command runs into an abnormal condition? If it
exits immediately, then it will retry every second, without giving any
indication that there is a problem (unless the process has its own
logging system or uses syslog). It might make sense to have something
watch the process, and stop trying to respawn it if it returns too
quickly after a few tries.
There is a utility already present on every Linux system that will do
this automatically for you: init. The same program that brings up the
system and sets up your terminals is perfectly suited for making sure
that programs are always running. In fact, that is its primary job.
You can add arbitrary lines to /etc/inittab specifying programs you'd
like init to watch for you:
zz:12345:respawn:/usr/local/sbin/my_daemon
The inittab line consists of an arbitrary (but unique) two character
identification string (in this case, zz), followed by the runlevels
that this program should be run in, then the respawn keyword, and
finally the full path to the command. In the above example, as long as
my_daemon is configured to run in the foreground, init will respawn
another copy whenever it exits. After making changes to inittab, be
sure to send a HUP to init so it will reload its configuration. One
quick way to do this is:
# kill -HUP 1
If the command respawns too quickly, then init will postpone execution
for a while, to keep it from tying up too many resources. For example:
zz:12345:respawn:/bin/touch /tmp/timestamp
This will cause the file /tmp/timestamp to be touched several times a
second, until init decides that enough is enough. You should see this
message in /var/log/messages almost immediately:
Sep 8 11:28:23 catlin init: Id "zz" respawning too fast: disabled for 5 minutes
In five minutes, init will try to run the command again, and if it is
still respawning too quickly, it will disable it again.
Obviously, this method is fine for commands that need to run as root,
but what if you want your auto-respawning process to run as some other
user? That's no problem: use sudo:
zz:12345:respawn:/usr/bin/sudo -u rob /bin/touch /tmp/timestamp
Now that touch will run as rob, not as root. If you're trying these
commands as you read this, be sure to remove the existing
/tmp/timestamp before trying this sudo line. After sending a HUP to
init, take a look at the timestamp file:
rob@catlin:~# ls -al /tmp/timestamp
-rw-r--r-- 1 rob users 0 Sep 8 11:28 /tmp/timestamp
The two drawbacks to using init to run arbitrary daemons are that you
need to comment out the line in inittab if you need to bring the
daemon down (since it will just respawn if you kill it) and that only
root can add entries to inittab. But for keeping a process running
that simply must stay up no matter what, init does a great job.
------------------------------------------
RuBoard
#I've found it very useful to pipe the results of a
#MySql query. The below will execute
#an SQL select statement and pipe result to a textfile
mysql database_name -e "sql statement" > sql.txt
------------------------------------------
#startx with an error log
startx 2>&1 | tee startx.log
------------------------------------------
#to check the settings being used for your Xserver
#(like xinerama), use "whereis" to locate the startx script:
whereis startx
------------------------------------------
#then edit that shell script to add server arguments, like:
startx -- +xinerama
#to add Xinerama support, although this can be in the
#config file found in /etc/X11/XF86Config-4.
------------------------------------------
#to find out what package something belongs to:
rpm -qf /path/to/mysterious_file
------------------------------------------
#To modify font sizes, so that 12 points looks like
#you expect, you can specify the dpi for the Xserver:
startx -- -dpi 100
------------------------------------------
#to find what the Xserver currently thinks your dpi is:
xdpyinfo | grep resolution
------------------------------------------
#to list all fonts:
xlsfonts
------------------------------------------
#to fax under linux, assuming you have all the
#standard packages for this installed, and have
#printed your document to a postscript file:
fax send -v 123-4567 file.ps
------------------------------------------
#to make a pdf file from an HTML page, first view the HTML
#in a good browser and print to file(.ps). Then use:
ps2pdf file.ps file.pdf
------------------------------------------
#htmldoc is not standard in RedHat, but can be obtained from
#http://www.easysw.com/htmldoc.
#This is a graphical program but can also be run from the
#command-line. The below will convert an HTML document to pdf:
htmldoc -t pdf -f invoice012.pdf --webpage invoice012.html
------------------------------------------
#directory usage, list the size of directories
du -ch
------------------------------------------
#amount of swap space used
free
------------------------------------------
#convert a man page to a textfile
man command | col -b > command.txt
------------------------------------------
#change the prompt to the current directory and current time:
PS1="[\W \@]"
------------------------------------------
#tell me the date and time
date
------------------------------------------
#tell met the date as YYYY-MM-DD
date +%Y-%m-%d
------------------------------------------
#If I can't remember a command, you can search
#for all commands related to a term
apropos term
------------------------------------------
#when copying directories, hidden files are skipped.
#They can be copied by stipulating "cp .*", but then
#hidden directories aren't copied recursively. This is an
#issue when I backup my email folders (Kmail), because
#hidden files and directories are used. But there is a
#command to copy directories recursively, including
#hidden files and directories:
rcp -r original new
------------------------------------------
#If you're using a digital camera and download the images to
#a linux system, you probably have gphoto2 installed (this is
#standard on RedHat, the project URL is
#http://www.gphoto.org/).
#For the Canon Powershot G2, the below command will get all
#pictures from the camera (connected to the USB port):
gphoto2 --camera "Canon PowerShot G2" --port usb: -P
------------------------------------------
#Perl can be used to perform a regular expression substitution
#on a series of text files, making backups, using a single command:
perl -pi.bak -e 's/foo/bar/' filelist
#note: use with caution. Test your regular expression before applying it
#to a large group of files. In the example above, the backup files will
#have the extension ".bak".
------------------------------------------
#Make a man page from a perldoc:
pod2man Photos.pm > /usr/share/man/man3/Photos.3pm
#after that, "man Photos" will display the Photos module documentation
#in manpage format.
------------------------------------------
#make an Xauthority file for user Chris
mkxauth -u Chris -c
------------------------------------------
#find files larger than 1 Megabyte modified less than 250 minutes ago
find / -size +1000k -mmin -250
------------------------------------------
#to convert a postscript file (.ps) to .jpg, thumbnail size, with ghostscript:
gs -sDEVICE=jpeg -sOutputFile=file.jpg -r20 file.ps
------------------------------------------
#if booting from a boot disk, and it's necessary for some reason
#to boot into a shell with only the "/" partition mounted (e.g. other
#partitions have errors), enter the below at the "boot>" prompt:
linux init=/bin/sh
------------------------------------------
#RedHat installations use "cups" for printing
#instead of lpd. The below command will print
#multiple copies of "class3.ps" collated (in this case, 14 copies)
lpr.cups -#14 -o Collate=True class3.ps
------------------------------------------
#to do a screen capture of a single window, saved as "file.jpg":
import file.jpg
------------------------------------------
#to make a tar file of all html files in /www
tar -czf pt.tar.gz /www*
------------------------------------------
#to list files in a gzipped tar archive:
tar -ztf pt.tar.gz
-----------------------------------------
No comments:
Post a Comment