Useful tricks with linux date
Dec 16

Have you ever spent countless time writing a shell script trying to say you want all “data” between this date and that date. I’ve had numerous scripts like this.  Well, recently I got tired of doing this so I re-read the man page for “date” and came across this helpful time.

Normally when I use date I format it how I like, such as 2008-12-16.  To do this I could do

date +%Y-%m-%d

Or to do 12-16-2008

date +%m-%d-%Y

But here is the real gem.  I needed to find out what date it was 6 months ago, so this is what I did.

date +%Y-%m-%d –date=’6 months ago’

And it gave me the correct date of 2008-06-16

Backing up a mysql database
Dec 12

I had the need to do a backup and compress a mysql database, so this is how I did it.

mysqldump --opt databasename -uUsername -p | gzip > databasename.sql.gz

More Useful RPM Queries
Dec 12

Here’s another useful what to query RPM’s installed on your machine. I had the need to see not only the rpm’s installed on the machine but what the size of each package was, so here’s the query I used

rpm -qa --queryformat='%{SIZE} %{NAME}-%{VERSION}-%{ARCH}\n'

You can then sort the output based on size

rpm -qa --queryformat='%{SIZE} %{NAME}-%{VERSION}-%{ARCH}\n' | sort -n

Turning xp_cmdshell on in Microsoft SQL Server
Dec 12

No it’s not Linux related, but it’s something I came across in my job today. I needed to be able to use xp_cmdshell to have SQL Server execute a dos command, but that command is turned off by default (for security reasons).

Open up Query Browser or MS SQL Server Management Studio and log in as SA or another privledged user. Open a new query. Run

sp_configure

Then scroll all the way to the bottom, if xp_cmdshell is there, then skip these 2 steps, otherwise run

sp_configure show_advanced_options, 1
reconfigure with override

Now run the following commands

exec sp_configure xp_cmdshell, 1
reconfigure with override

Now you can use xp_cmdshell in all your scripts

Useful RPM commands
Dec 12

Now that 64bit systems have come along, I’ve come across issues using just

rpm -qa

If you have package that installs both a x86 & x86_64 package, it will only show the package name once for both packages.

Try the following, it will list all packages along with the arch type

rpm -qa --queryformat "%{NAME}-%{VERSION}.%{ARCH}\n"

Command line stuff
Dec 12

Say in the past you had run a very long command, if you want to re-run the last “whatever” command type

!whatever

If you want to re-run the last command but substitute v3 for v4 run:

^v3^v4

Using VI to Search and Replace
Dec 12

Easy to use way to search and replace in VI.

  1. Open your file: vi filename
  2. type: :0,$s/search_term/replace_term/g

This will replace all search_term with replace_term from the beginning (0) to the end ($) of the file. You can also use (.) which means from “here”, so (.,$) would be here to the end. (0,.) would be beginning to here.

You can also use (%) instead of (0,$) to search entire document. Or you can use (23,90) to only search between lines 23 and 90 (substitute your own line numbers).

Useful command for killing process
Dec 12

If you want to kill a process, but don’t know the process id, but you do know the service name, try this. It will get the process id and kill it all in one step.  Replace httpd with the process name you want to kill

ps auxwww | grep httpd | awk ‘{print $2}’ | xargs kill -15

Recursively Deleting
Dec 12

I needed a quick command to delete a specific file or directory that existing in multiple subdirectories. Here is what I came up with.

find . -name “filename” | xargs rm -rf

Note: files can be a filename or directory name. You can use rm with -rf or without

Quick Guide to Nano or Pico text editor
Dec 12

Here are a few tips on how to get around in NANO

To edit a file type: nano filename
*** if you are using one of the legacy Linux servers, it will say NANO Command Not Found. Use PICO instead

  • CTRL-Y – Page Up
  • CTRL-V – Page Down
  • CTRL-W – Where Is (Search)
  • CTRL-K – Cut current line (press multiple times to multiple lines)
  • CTRL-U – Paste lines just cut (if you cut multiple lines it will paste them all)
  • CTRL-C – Word Count / Line Count / Current Position (will be displayed at bottom of page)
  • CTRL-O then ENTER – Save file
  • If no changes have been made, you can exit by hitting CTRL-X
  • If changes have been made, but you DO NOT want to save, hit CTRL-X then N
  • If changes have been made, but you DO WANT to save, hit CTRL-X then Y, then ENTER

« Previous Entries