Handy Linux command lines
Kind of a mish-mosh of snippets below. A lot of them are useful if you’re managing servers. The rest are useful more generally and occasionally. Many of these utilities may already be on your system, others you may have to install. (Hopefully you’re using a Linux distro that has all of these in its standard software repository so you can just search, click, install.)
Get a random number between 1 and 100
echo $(($RANDOM % 100 + 1))
Actual non-swapped physical memory in use by a process in kilobytes
ps aux | grep process_name | awk '{sum += $6}; END {print sum}'
For example, to find out the total amount of memory being used by all Apache processes, use “apache” as the process name.
Count the number of processes with a given name
ps aux | grep process_name | wc -l
Total system memory
head /proc/meminfo | sed -n /MemTotal/p | sed -r 's/MemTotal:\W*(.*).kB/\1/'
This is useful in scripts that need to compare memory against the total memory available on a system.
Print all lines in a file that contain a pattern
grep 'pattern' filename
Print all lines in a file that do not contain a pattern
sed -n '/pattern/!p' filename
Do something once, at some later time
at time > type your commands at the prompt CTRL-D
This one is kind of odd. For example, first you type at 09:30 at a console. A prompt will appear. Then you type commands you would like executed at that time. Press CTRL-D when you are done entering commands.
Mail output of a command to someone
command | sed -e "1iSubject: Command results\n" | sendmail -t email_address
The sed -e bit prepends the subject line to the beginning of the output for sendmail.
Relabel an ext2/ext3 (Linux) formatted drive
e2label device label
For example, sudo e2label /dev/sda1 data.
Relabel a DOS formatted drive
First, create ~/.mtoolsrc:
# For example, where /dev/sdh2 is replaced with your MSDOS device drive c: file="/dev/sdh2"
Then:
mlabel c:
Convert password protected PDF files to unprotected PS
pdftops -upw password filename.pdf
I used this the other day when I needed to load a password protected PDF into GIMP.