Linux Command Line Tips And Tricks

Print Article Print Article

Linux Command Line Tips And Tricks

By Web Hosting Help Guy

All developers and sysadmins who work in a Linux environment are familiar with the following scenario – you’re at the command prompt and you know exactly what you’re trying to do, but you just can’t think of (or find) the right commands to accomplish the task in one fell swoop. Frustrated, you pound your way through four steps, finally accomplishing the task that you know you could have taken care of in one step – if only you had known the right step.

Luckily for you, we’ve gathered together our brightest Linux minds and command line ninjas and asked them to help us out by providing their most useful tricks, shortcuts, and secrets to share with the world. They’re listed below, along with a helpful description as to what they accomplish and when to use them. Enjoy!

Moving a MySQL Database To Another Server:

Not only does this command dump the database, but it creates the dump, compresses it, and securely transfers it to the remote host via SSH. Then, it decompresses the dump and imports it to the new location.

File Location/Management Commands:

This command locates all files on your server that are over 20MB (or a size of your designation) and reports both the size and the location of each file that qualifies. This command is generally restricted to those with root access.

The above command will list the size of every file in the current folder in Human readable format AND print the size of the current folder.

Simliarly, the first command below will print the size of all files in a given folder, while the one below that will find any file you specify, beginning the search in the root directory.

Usage commands:

The first command lists processes by their percentage of CPU usage, while the second command lists them by memory usage. A great quick trick to see where your server resources are going.

Permissions:

The above commands find potentially harmful file permissions and change them to more secure and acceptable levels. Remember, though, sometimes file permissions need to be set at 777, and the above command will change all 777 permissions to the more restrictive 755.

Calendars

The first command will display a calendar for the current year, while the second shows a calendar for any given year, which is perfect if for some reason you want to see what day you started second grade on in 1984.

Create a Mirror Site

This command is particularly useful if you have to move your entire website to another server – it compresses, encrypts it, and creates a mirror of it, allowing you to transport that exact copy to another server.

Compressing Many Files Into One For Export

It’s a load on your servers if it has to connect, disconnect, and reconnect every time you want to transfer data from one place to another, such as in an FTP export. The first command above compresses data from many folders into one file for easier export by making a .tgz archive. Similarly, the second command copies a file to another server of your choice.

Disabling Line Wrap In The Command Line

When coding a lengthy sequence, it can sometimes be difficult to see where the line breaks are, since in default mode the line of code will wrap it below. Turn it off with the above command for easier copying and pasting long lines of code.

What are some of your favorite command line tricks?

Related posts:

  1. Two Clever Ways To Connect To The Web From The Command Line
  2. PHP Tricks For Beginners

Comments

Brandon Florin November 19th, 2009 at 11:09 am

Compress/Extract over ssh in one command via stdout:

tar cf – /path/to/folder | ssh user@host “cd /usr/src && tar xf -”

Web Hosting Help Guy November 19th, 2009 at 4:49 pm

That’s a great command Brandon, thanks for sharing!

Daniel November 20th, 2009 at 4:37 am

There are some faults or too complex workarounds in your examples.

Permissions: You just seem to remove write permissions of group and others. Try it this way: chmod -R og-w targetdir

Create a Mirror Site: Compression using -z parameter in rsync is quite buggy and may break. If you have the bandwidth it may even slow down the transfer.

Moving a MySQL Database To Another Server: I guess it should be gunzip -c | mysql (instead of > mysql). However, you don’t need to call gzip as ssh offers the -C option if allowed by the remote server.

Disabling Line Wrap In The Command Line: Well, it’s got nothing to do with the command line, it’s just the editor…

Web Hosting Help Guy November 20th, 2009 at 11:12 am

Hey Daniel, thanks for the input on our tips. Yours definitely do the trick as well — also, I agree that the “disabling wraps” command was worded a little vaguely. Maybe something like, “Disabling Line Wrap When Editing Files W/ Pico On The Command Line.”
Thanks for the input!
–WHHG

Linux Command Lines Tips and Tricks | Software Passion November 22nd, 2009 at 2:59 am

[...] Linux Command Lines Tips and Tricks Linux Command Lines Tips and Tricks [...]

Doug W November 29th, 2009 at 8:42 pm

Reformatting print page:
curl http://webhostinghelpguy.inmotionhosting.com/developer-tools/linux-command-line-tips-and-tricks/print/ | sed ‘s///’ > print.htm; ls -rt1 | tail -1 | awk ‘{ print $0; }’ | xargs firefox &

Doug W November 29th, 2009 at 8:47 pm

Hmm, that didn’t come out the way I had meant for it to. The text file below has the proper formatting :)

http://mysitesurl.com/fix.txt

Atul Khachane January 25th, 2011 at 1:10 pm

simple but powerful :)

Leave A Comment

Name *required

Email *not published *required

Website