Calling all experts: Linux and Unix Tips and Tricks

Collapse
This is a sticky topic.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    Calling all experts: Linux and Unix Tips and Tricks

    As Linux and the various flavours of Unix are slowly spreading into the world of personal computers, I thought we could collect a few Tips and Tricks here to help each other making the best out of our distributions!

    Ideally any Tips posted here should be available for several distributions, but we won't reject any that aren't. :-)

    Please state, which distribution(s) the tips were tested with.

    I'll start with posting some of my favourites.

    *EDIT: For more extensive Tips, check the Linux and Unix articles section
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Colours in the console

    bash is one of the most popular shells in the Linux and Unix world and many distributions use it as their standard.

    Often however, it's just black and white. But, with a little trick we can make it nice and colourful.

    We'll have to edit the file ~/.bashrc - you can use your favourite editor for that (I normally use vim, but nano, gedit or emacs or example will do the job just as well). I'll show how to do the job with vim. Type
    Code:
    vim ~/.bashrc
    in a console window. Normally such a file will already exist, so you'll see some text straight away.
    First of all, check if you have these (or similar) lines somewhere, by scrolling down with the arrow keys:
    Code:
    case "$TERM" in
    xterm-color)
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
        ;;
    *)
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
        ;;
    esac
    If so, we'll have to comment it out. To do so, first of all press the key i on your keyboard to start editing mode. Then go to the beginning of each of those lines and enter a # symbol.

    Next, we'll add a line underneath. So, go underneath the lines you just edited, press Enter to start a new line and enter the following text:
    Code:
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    Now press the Esc key and type
    Code:
    :wq
    to save and exit. To test the settings you just made, type
    Code:
    bash
    now - and voilĂ , the shell shows colours!


    This tip was tested under Ubuntu, Debian and Fedora.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Creating aliases for bash

      If you work in bash a lot (or in any other shell for that matter, but we'll concentrate on bash), there might be commands you call again and again and again. Especially if these are relatively long, it can be a good idea to create shortcuts for them. A great option here is using aliases. Let me give you two examples:
      • You want to list more details with ls and preferably do so in colour. The normal command would be
        Code:
        ls --color=auto -l
        But we'll define an alias for that called ll, which makes life much easier.
      • You connect to a certain server via ssh frequently and don't want to call
        Code:
        ssh -X 123.456.78.9
        every time. No problem - we'll define an alias (say tux) to do that.
      OK, so how do we do this? First of all, let's create a file ~/.bash_aliases with the command
      Code:
      vim ~/.bash_aliases
      Now press the i key and type the following lines:
      Code:
      ## some ls aliases
      alias ls='ls --color=auto'
      alias la='ls -A'
      alias lh='ls -l -h'
      alias ll='ls -l'
      alias l='ls -CF'
      
      ## some other aliases
      alias tux='ssh -X 123.456.78.9'
      OK, so we've defined those now. Finish editing by pressing the Esc key and save and quit by writing
      Code:
      :wq
      and hitting the Enter button.

      Now we'll just have to let bash know about those aliases. Like before, we'll edit a file for that:
      Code:
      vim ~/.bashrc
      Go to the bottom of that file, press i to start editing and in new lines write:
      Code:
      if [ -f ~/.bash_aliases ]; then
          . ~/.bash_aliases
      fi
      Now, press Esc, type :wq, hit Enter and run
      Code:
      bash
      Your new aliases should work now - just type
      Code:
      ll #that's two small 'L's
      for example!


      This Tip was tested under Ubuntu, Debian and Fedora

      Comment

      • AmberJain
        Recognized Expert Contributor
        • Jan 2008
        • 922

        #4
        subscribing ............... ............... ............... ............... .......:)

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Having a personal welcome in bash

          When you start a new terminal window, you will get a little welcome message like
          Originally posted by bash on Ubuntu 8.04
          Linux tux 2.6.24-my-superkernel #1 SMP Sun Oct 31 24:00:01 UTC 2008 i686

          The programs included with the Ubuntu system are free software;
          the exact distribution terms for each program are described in the
          individual files in /usr/share/doc/*/copyright.

          Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
          applicable law.

          To access official Ubuntu documentation, please visit:
          http://help.ubuntu.com/
          This can become pretty boring, so we'll change it. How? Now that's easy!

          You'll need to work with root privileges, so either use sudo if installed or change to your root account with su.

          Now, you'll want to edit the file /etc/motd - I'll be using vim again:
          Code:
          sudo vim /etc/motd
          Keep the d key pressed to delete everything first (the command dd means "delete this line"), then press the i key to start editing. Enter the text you want it so say every time now. When finished, quit editing mode by hitting Esc, then save and close the file with
          Code:
          :wq
          When you start a terminal window next time, it should show you your new message! :-)


          Should work on any Linux distribution.


          @ambr: There's a "Subscribe" link in the first post, so you don't have to post something to subscribe any more.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Installing Google Chrome (BETA) under Linux

            Not long ago, Google presented it's own webbrowser: Google Chrome. It was praised quite a lot, but one of the problems with it was, that it's not officially available for anything but Windows yet. However, there is a project to get it running on Linux and Mac OS X. It's not perfect, but it works. Here's how to install it:
            1. Go to http://www.codeweavers.com/services/ports/chromium and download the newest version for your system.
            2. Install the file you just downloaded. If double clicking on it doesn't work, try this:
              • On Debian or any deb-based system (like Debian, Ubuntu or Knoppix), open a console and direct to where the download is saved. Then enter
                Code:
                sudo dpkg -i cxchromium_*
              • On a rpm-based system (e.g. Fedora, openSuSE, Mandrivia,...), open a console and direct to where the download is saved. Switch to your root account with
                Code:
                su
                Then enter
                Code:
                rpm -i cxchromium_*
              • On any other type of Linux distro, open a console and direct to where the download is saved. Switch to your root account with
                Code:
                su
                . Then enter
                Code:
                chmod +x cxchromium_*
                ./cxchromium_*
              • On a Mac, just drag the .dmg file into your Applications folder
            3. Either start Chrome via an icon (if you can find one) or use the command
              Code:
              chromium
              in a console window.
            4. Enjoy!
            I admit, it's not that fast yet, as this is actually a windows build using wine (so you may have to install wine too - check your distribution to find out how to do that), but it works!


            Tested on Ubuntu 8.04, but should work on most other systems too.


            NOTE: If you're interested in a real version for Linux, you can register to receive information as soon as it's available here or here for Mac versions.

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Useful key combinations

              In the command line:
              • If you've started a command in the command and realise you don't want to complete it (e.g. copying loads of files to the wrong destination), there's a simple method to stop the command: Crtl + C
              • If your screen is full of old output, you can easily clear it with Crtl + L
              • To log out in a console, press Crtl + d. (Note: If this is a terminal emulator in your graphical environment, it will probably exit.)
              • To jump within one command in the command line, use Crtl + A to jump to the beginning and Crtl + E to jump to the end.

              In the graphical environment:
              • You want to switch to a console quickly? Maybe even without login on to your graphical environment? Easy: Use Crtl + Alt + F1 to Crtl + Alt + F4 (and sometimes more) to get a basic console. To change back, it's normally Crtl + Alt + F5, Crtl + Alt + F7 or Crtl + Alt + F9
              • Your desktop environment has crashed? Doesn't happen very often, but it happens. The solution? Restart the X-Server with Crtl + Alt + Backspace

              Hint: Sometimes when talking about key combinations on Linux, you read about the Super key. This is normally the Windows key of your keyboard.


              Tested on Ubuntu, but should work on any Linux distribution.

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Surfing the web from the command line

                When we think of surfing the internet, it's usually combined with a graphical browser like Firefox, Opera, Konqueror, etc. However in some cases, you don't need that much or can't use a graphical interface. Does that mean, you can't surf the internet?

                No, it doesn't. There are actually some pretty good command line browsers available. My favourite is w3m, but lynx and links are also quite popular choices.

                So, how does this work? Basically, you just enter
                Code:
                w3m www.bytes.com
                to get here and then use your keyboard to navigate. To get help, press Shift+H. Some of my most used commands are:
                • u : Show the address a link points to
                • Crtl+t : Opens a link in a new tab
                • { and } : Switch between your tabs
                • Shift+B : Go back in the browser history
                • Shift+U : Enter an URL to browse to
                • q : Quit w3m
                • Shift+Q : Quit w3m without asking
                Another tip: starting w3m with the option -F makes w3m automatically render frames. As I like it to do so automatically, I created an alias
                Code:
                alias w3m='w3m -F'
                (See one of the earlier posts to find out about aliases.)


                This Tip was tested on Ubuntu and Debian, but should work on any Linux distribution.

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Downloading from the command line

                  When you download something, you either do so via a browser or a programmed designed for downloading. I've already talked about command line browsers, now I'll talk about a command line downloader: wget

                  wget works pretty easily: simply type something like
                  Code:
                  wget http://mirrors.us.kernel.org/ubuntu-releases/intrepid/ubuntu-8.10-desktop-i386.iso
                  to download an Ubuntu 8.10 ISO for example. But wget can do much more than that.

                  For example, it can resume downloads. Say you started downloading that Ubuntu ISO, but had to cancel the download. Then you can continue it with
                  Code:
                  wget -c http://mirrors.us.kernel.org/ubuntu-releases/intrepid/ubuntu-8.10-desktop-i386.iso
                  It's that simple.

                  Of course, there are much more functions as well - check man wget to find out more.


                  This Tip was tested on Ubuntu and Debian, but should work on any distribution.

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Viewing text files

                    If you want to view a text file in the command line, first choice is cat:
                    Code:
                    cat [i]textfile[/i]
                    But this isn't always the best choice.

                    Say, you have a very long file and want to look at it from top to bottom. In that case, have a look at less:
                    Code:
                    less [i]textfile[/i]
                    You can quit viewing by pressing q.

                    If you only want to view the first 15 lines of a file, head would be the tool of choice:
                    Code:
                    head -N 15 [i]textfile[/i]
                    Leaving out the -n 15 will show the first 10 lines of the textfile.

                    Now, you can also view the last 15 lines of a file of course - try tail:
                    Code:
                    tail -n 15 [i]textfile[/i]
                    The options are similar to those of head. However, there is a further very useful option:
                    Code:
                    tail -n 15 -f [i]textfile[/i]
                    will show any updates to the bottom of the file too.

                    Of course, you can combine these commands. For example
                    Code:
                    head -n 100 [i]textfile[/i] | tail -n 50 - | less
                    will show you the lines 50 - 100 and allow you to scroll through them.


                    These Tips were tested on Ubuntu, but should work on any distribution.

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      Showing and editing system time

                      Basically any modern desktop environment will offer a clock and calendar of some kind, but you're not limited to those - you can get those in the command line too!
                      • Current time and date
                        To show the current date and time as saved by the OS, call
                        Code:
                        date
                        To set that date, use something like
                        Code:
                        date -s "Wed Dez 03 21:38:59 GMT+1 2008"
                        (You'll have to be logged in as a superuser for this to work.)
                        This will set the date, but that setting will disappear with a system restart. To save it, you'll have to use the command
                        Code:
                        hwclock --utc --systohc
                        which will set the hardware clock to whatever the system clock is currently set to.

                      • Calendar
                        By simply entering the command
                        Code:
                        cal
                        or
                        Code:
                        ncal
                        you'll see a calendar, displaying the current date. For the calendar of a complete year, use
                        Code:
                        cal 2008
                        (or whichever year it's supposed to be.) You can also display a different month than the current one:
                        Code:
                        cal may 2008
                        If you want to see the previous and the following month too, try
                        Code:
                        cal -3
                        If you want Monday to be the first day of the week (as opposed to Sunday), use
                        Code:
                        cal -m
                        The date of easter for the current year can be called with
                        Code:
                        ncal -e
                        for western churches or
                        Code:
                        cal -o
                        for Greek and Russian Orthodox Churches
                        If you want to know the number of the week, use
                        Code:
                        ncal -w

                      Note, that all dates are saved and interpreted as after the 1st Jan 1970, 00:00:00, so if you want to use a date prior to that, you'll have to use an alternative method.


                      These Tips were tested on Ubuntu, but should work on any distribution.

                      Comment

                      • Nepomuk
                        Recognized Expert Specialist
                        • Aug 2007
                        • 3111

                        #12
                        Comparing files

                        Sometimes, you want to compare two files and spot the differences. Of course, you can do that manually, but there's also the tool diff, which can automate this. Here's how you use it.

                        Say, you want to check what device name a usb pendrive gets. First of all, you make a list of the current devices:
                        Code:
                        ls /dev/* > before
                        Now, plug the device in and wait for a few seconds, so that the system can recognise the new device. Then run
                        Code:
                        ls /dev/* > after
                        You now have a list of the devices before and after plugging in your pendrive.
                        To compare the two, use
                        Code:
                        diff before after
                        The output will be something like this:
                        Code:
                        > /dev/sdd
                        > /dev/sdd1
                        So, the entries after ">" have been added in the "after" file. If you do it the other way round (diff after before), you'll get
                        Code:
                        < /dev/sdd
                        < /dev/sdd1
                        Here, the entries after "<" have been removed.

                        Of course, this tool can be very useful in other scenarios, especially programming.


                        This Tip was tested on Ubuntu, but should work on any distribution.

                        Comment

                        • Nepomuk
                          Recognized Expert Specialist
                          • Aug 2007
                          • 3111

                          #13
                          Looking at running processes and stopping them

                          When you want to look at what processes are running, there are three main command line programs: ps, top and pgrep. Here's how to use them:
                          Code:
                          ps aux
                          This will list all running processes by all users. One interesting thing that we'll need later is the column PID.
                          Code:
                          top
                          This will display an updating screen which shows the processes that use up most CPU power with some further information.
                          Code:
                          pgrep -l -u [i]username[/i]
                          This will list all processes that the user username is currently running, including their PIDs. If you want to filter for certain processes, you can use
                          Code:
                          pgrep -l bash
                          instead (to filter out everything containing the word bash). These can be combined of course:
                          Code:
                          pgrep -l -u username bash
                          If you want to close a process, you can use the commands kill and pkill for that. Here's how they are used:
                          Code:
                          kill [i]pid[/i]
                          Now, that pid is the Process ID we noticed before. If you don't know the PID (or want to kill all processes with something specific in their names), use
                          Code:
                          pkill name
                          instead.

                          What both of these commands will do is send those processes a signal saying something like "Hey, please abort." However, if those processes have truly crashed, then that won't work. You should certainly try that first, but if the process doesn't shut down, use the following:
                          Code:
                          kill -9 pid
                          or
                          Code:
                          pkill -9 name
                          This sends a KILL signal, that can not be blocked.


                          This Tip was tested on Ubuntu, but should work on any distribution.

                          Comment

                          • anijos
                            New Member
                            • Nov 2008
                            • 52

                            #14
                            Alphabetical Directory of Linux Commands with Description

                            Linux Command Directory: Index

                            Comment

                            • Nepomuk
                              Recognized Expert Specialist
                              • Aug 2007
                              • 3111

                              #15
                              Adding own scripts and binaries

                              Let's say, you wrote a little tool called myTool and the binary is saved in your home directory under ~/myTool/bin/ - you won't be able to run it if you're not in that directory or if you don't use the full path. This is because it isn't in the so called PATH variable. You can have a look at this PATH variable by calling
                              Code:
                              echo $PATH
                              in command line - it may look something like this:
                              Code:
                              /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/real/RealPlayer
                              The directories listed here and separated by colons (":") are those, that are in the PATH variable and that you can therefore call from anywhere.

                              There are two realistic ways to add a command to the PATH variable: Add that tool to a directory, which is already listed or add a directory to the list. If this tool should be available to everyone using the system, the first option is best. The directory /usr/bin should be the best choice here in most cases. So either copy your tool there, create a link or write a little script, that will call your tool.

                              If only you want to use your tool, add the directory it's in to the PATH directory. For the current session, this command should do fine:
                              Code:
                              export PATH=$PATH:/home/user/myTool/bin
                              If you want to have it available permanently however, write that line into the .bashrc file in your home directory.


                              This Tip was tested on Ubuntu using bash, but should work on any distribution that uses bash. The syntax may be different for other shells.

                              Comment

                              Working...