Tuesday, November 10, 2009

10 Useful 1-liners for the Linux Command Line


During my time as a Linux admin, I have collected a list of useful one-line commands that I could use from the command line to increase efficiency by reducing repetitive work load. I've also found that it was useful to document commands that are very necessary, but not on a daily basis, for easy access to their syntax.

Here are 10 of those such commands:


Change text in multiple files:
perl -pi.bak -e "s/basic/default/g" *.txt

Revert back from changes made by above command:
for i in *.txt.bak ; do mv --reply=y $i ${i/.bak/} ; done

Change extension of all file of given type to specified extension:
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1txt/'` ; done

Move and rename a file with a date stamp (great for log rotation):
mv filename "/`date +%Y%m%d`_filename"

Resize image(s) using ImageMagick:
mogrify --resize 250 filename.jpg

Generate a Self-Signed SSL Certificate using OpenSSL:
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout secure.example.com.key -out secure.example.com.crt

Generating a Certificate Signing Request (CSR) for Verisign, Thawte, Network Solutions, etc.:
openssl req -new -newkey rsa:1024 -nodes -keyout secure.example.com.key -out secure.example.com.csr

Hash a password for encryption (hash can be used in /etc/shadow):
#Where 'password' is your password and 'nnnnnnnn' is an 8-character seed used to encrypt it.
perl -e 'print crypt("password","\$1\$nnnnnnnn\$"),"\n"'