linux 111108

Upload: diptimehra1247

Post on 04-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Linux 111108

    1/2

    Tips and tricks for working on the command line

    By Vincent Danen

    Anyone that has been reading these tips for any length of time knows that I am a command-line guy. It'sfaster, more efficient, and more powerful. Sure, a nice GUI is great to look at, but to really get things

    done, give me the CLI any day. Even to this day I still use a text-mode e-mail client (Mutt) and text-modeIRC client (Irssi). GUI clients that supposedly offer the same functionality do not, and are irritating at best.

    Having said that, the CLI can be intimidating and obscure, but it doesn't need to be. There are a number oftips and tricks that can be accomplished on the CLI and so this tip will provide a few of them.

    Renaming files is one thing that a good GUI file manager appears to have over the CLI. Usually clickingon a file name will give you the opportunity to edit the name in order to rename it, which is great for fileswith long names. This can be done easily in a shell too, either by using tab expansion or with this littletrick:

    $ mv thisisareallylongfilename{,.txt}

    This will move the file "thisisareallylongfilename" to "thisisareallylongfilename.txt" without having totype it twice. Directly on the CLI this may not look so fantastic due to tab expansion, but in a shell script,this can make renaming batches of files extremely simple. If you need to rename files with extensions, itis just as easy:

    $ mv foo.{jpeg,jpg}

    This renames foo.jpeg to foo.jpg .

    The rename command is also useful for renaming files in batches. For instance, suppose you had adirectory of .php4 files you wanted to rename to .php. The rename command does this quite easily:

    $ touch 1.php4 2.php4 3.php 4.php4$ rename .php4 .php *.php4$ ls *php*

    1.php 2.php 3.php 4.phpShell expansion characters are often overlooked as well. For instance, if you execute a command and findit needs something extra, you can use the !! character sequence to bring the command back, unedited. Forinstance, if you try to execute a shell script that does not have the executable bit set, you might see:

    $ ~/foo.sh-bash: /home/vdanen/foo.sh: Permission denied$ sh !!sh ~/foo.sh

    Other applications for this sequence include re-executing a command via sudo for elevated privileges.

    Another set of useful character sequences are:

    !* which will repeat the last command's arguments (whereas !! does the entire command)!$ to print the final argument!:3 to print the third argument of the previous command.

    For instance:

    $ ls /tmp/somedirls: cannot access /tmp/somedir: No such file or directory$ mkdir -p !*mkdir -p /tmp/somedir

  • 8/13/2019 Linux 111108

    2/2

    $ touch 1 2 3$ cp !:2 /tmp/somedircp 2 /tmp/somedir

    The final tip here is to point to one of my favorite Web resources. The [shell-fu]$ Web site currentlycontains nearly four hundred shell-related tips and is definitely a worthwhile read for anyone interested in

    becoming more efficient with the CLI.

    http://www.shell-fu.org/http://www.shell-fu.org/http://www.shell-fu.org/http://www.shell-fu.org/