Showing posts with label Bash Tutorial. Show all posts
Showing posts with label Bash Tutorial. Show all posts

2009-06-24

Bash Tutorial: Part 3

We're back at it with more about using Bash. This time, rather than focus on new commands, we'll look at command operators and redirection. Command operators allow you to run multiple Bash commands in one statement. Redirection is not just a useful tool when performing magic, it's also a great way to change where the input and/or output of a command come from and/or go.

Command Operators
  • ;
    To execute multiple commands one after another, we use the ; operator. In other words, the first command specified will run until completion before the second command starts. For example, if you want to update your index for the locate command and then search for the xorg.conf, we would do the following:

    # updatedb ; locate xorg.conf

    The above is essentially the same as running this:

    # updatedb
    # locate xorg.conf

  • &
    Executing commands in rapid succession all on one line is cool, but it can be more useful to run multiple commands all at the same time. For example, if you wanted to write a Bash script to launch firefox, pidgin, and songbird all at once, the important piece would be the following command:

    $ firefox & pidgin & songbird

    Another use for the & operator is if you want to run a command in the background. In other words, if you would like to run a command, but instead of waiting for the command to finish to regain control of the terminal, the control will immediately be returned and your computer will continue executing the command in the background. To do this, you simply end your command with &. If you wanted to update your index for the locate command, but don't want to wait for this to finish, you could do the following:

    # updatedb &

Redirection
  • >
    Sometimes, the output of a command isn't immediately important, but you would like to save a record of it for later. To do that, we can use the > operator to create a new file, or overwrite an existing file, with the output of a command. For example, if you're trying to troubleshoot a networking problem, you might want someone to look at the output of the ifconfig command on your system. To save this output, you can do the following:

    # ifconfig > output.txt

  • >>
    Sometimes you want the output of a command to write to a file, but you don't want to lose the existing contents of that file. In this case, we can use the >> operator to append the output to the end of the specified file. For example, if you want to add the output of the lsmod command to the output file we just created, we could do the following:

    # lsmod >> output.txt

    If you have read some of my other posts, you have seen me use the two above redirection operators in conjunction with the echo command to create or add to files that control the behavior of some parts of the computer.

  • <
    There are also cases where you might want to specify the input of a command or program from a file. One example I can think of is if you have a text file that you would like to sort, you could use this as the input for the sort command like this:

    $ sort < unsorted.txt

    Of course, the above will simply output the text from unsorted.txt to the screen in sorted order. If we wanted to save the sorted version of the text file, we could combine input and output redirection to accomplish this:

    $ sort < unsorted.txt > sorted.txt

  • <<
    The << operator acts basically as a simple line based text editor. If you remember, we used it in the Installing Songbird post as an option for creating the Bash script to launch Songbird. Here's an example of the << operator with the sort command:

    $ sort << EOF
    > 3
    > 1
    > 2
    > EOF

    For another example, I've included the section where we used this in the Songbird post. In this example we use both input and output redirection to create a Bash script without dealing with a real text editor:

    # cat > /usr/share/Songbird/launcher.sh << EOF
    > #!/bin/sh
    > cd /usr/share/Songbird
    > ./songbird
    > EOF

  • |
    Pipes. Saving the best for last here, pipes allow you to redirect the standard output of one command to the standard input of another command. That may sound a little confusing as you read it, but let me give an example, that may clear things up:

    $ ls -al | less

    Using what we have learned about Bash command operators, the above command is basically the same thing as:

    $ ls -al > temp.txt ; less temp.txt ; rm temp.txt

    As you can see, piping commands is powerful and can save a bit of typing. My favorite use of pipes is with the grep command:

    $ ps -ef | grep firefox

    I'll cover both the ps and grep commands in future posts, but the above will give you information about any processes running on your system that have firefox as part of the process name. The above would be the same as the following:

    $ ps -ef > temp.txt ; grep firefox temp.txt ; rm temp.txt

That does it for Part 3 of the introduction to Bash.

See you next time.

2009-06-21

Bash Tutorial: Part 2

For part 2 of my Bash tutorial, I didn't have the time to make it as big as I had wanted, so it's a little shorter. Let's get started.

Displaying file contents
  • cat
    Concatenate files. The cat command can be used to type out the contents of a file or of multiple files. To type out the contets of your /etc/apt/sources.list file, just do:

    $ cat /etc/apt/sources.list

    If you want to type out the contents of multiple files right after each other, just do:

    $ cat file1 file2 file3

  • less
    less is similar to cat for displaying the contents of a single file. However, less is much better than cat for large files. If you use cat on a large file, you will only be able to see the end of the file because the contents would have scrolled out of view and even beyond the scroll buffer. less allows you to scroll through the entire contents of the file with the up and down arrows as well as the page up and page down keys. To use less to display the contents of your /etc/apt/sources.list file, just do:

    $ less /etc/apt/sources.list

That's all for now. Next time we'll be back with part3.

See you next time

2009-06-17

Bash Tutorial: Part 1

I know a lot of people get scared any time the command line comes up when using a computer. Really, the command line shouldn't be scary, and learning how to use the command line in any operating system will only make you better at using your computer. As such, I figured I would do a couple posts as an introduction into the Linux shell I use most: the Bourne-again shell (Bash). Let's get started.

Moving around the shell
  • pwd
    Print working directory. When you first open a shell, it starts you in your home directory, usually /home/[username], also abbreviated ~. If you're ever not sure what directory you're currently working in, the pwd command will tell you:

    $ pwd
    /home/[username]
    $

  • ls
    List files. If you want to see what files and folders are in a directory, ls is the command to do it. If you just want to see the contents of the current directory, just use:

    $ ls

    In Linux, files whose name begin with . are hidden. To list the contents of the current directory and show hidden files, use:

    $ ls -a

    If you want to see more information about the files, such as who has permissions to read, run, and execute files; the owning user and group; the file size; and the last date modified, just use:

    $ ls -l

    The ls command does not simply restrict you to looking at files in your current working directory; you can look inside any directory you want. To see the long listing of all files in the /usr/bin directory, just use:

    $ ls -al /usr/bin

  • cd
    Change directories. Now that we've covered how to see your current working directory and listing files within directories, the next place to go is to change directories. In order to change directories to /etc/X11, just do:

    $ cd /etc/X11

    We can also use .. to move up a level:

    $ pwd
    /lib/modules/2.6.26-2-686/kernel/net/ipv6
    $ cd ..
    $ pwd
    /lib/modules/2.6.26-2-686/kernel/net
    $ cd ../..
    $ pwd
    /lib/modules/2.6.26-2-686
    $ cd ../2.6.26-1-686
    $ pwd
    /lib/modules/2.6.26-1-686

Working with files
  • cp
    Copy. The basic syntax for the cp command is:

    $ cp [source file] [destination file]

    For example, if you wanted to make a backup copy of your .bashrc file before making changes:

    $ cp .bashrc .bashrc-backup

    This is a good for copying single files, but if you want to make a copy of an entire directory, we can use the -r option to do a recursive copy:

    $ cp -r Documents Documents-backup

  • mv
    Move. The mv command works similar to cp:

    $ mv [source file] [destination file or directory]

    If you want to simply rename your .bashrc-backup file to fix edits made to your .bashrc file:

    $ mv .bashrc-backup .bashrc

    If you want to move a file into a different directory:

    $ mv .bahsrc-backup Backup/

    If you need to move or rename a directory, it works exactly the same with the mv command as moving files.

  • mkdir
    Make directory. If you want to create a new directory for all your digital pictures, just do:

    $ mkdir Pictures

    You can also create a any missing parent directories with the -p option:

    $ mkdir -p Pictures/Vacation/2009


  • rm
    Remove. If you want to delete the .bashrc-backup file, just do:

    $ rm .bashrc-backup

    If you want to delete a directory and all the files and subdirectories within it, you can use the -r recursive option:

    $ rm -r /Pictures/Vacation/2009

  • rmdir
    Remove directory. If you want to delete a directory, you can use the rmdir command. The rmdir command requires that the directory that you're trying to delete is empty:

    $ rmdir Pictures/Vacation/2009

    Similar to the recursive option that we've seen with other commands, we have the -p parents option to use with rmdir:

    $ rmdir -p Pictures/Vacation/2009

    The above behaves the same as the following:

    $ rmdir Pictures/Vacation/2009
    $ rmdir Pictures/Vacation
    $ rmdir Pictures

Learning more

We've covered a lot in this post, but there is still more that can be done with the above commands. Here are some resources that are available to learn more of the options available.
  • man
    Manual. The man command will bring up what is called the "man pages" for other commands. The man pages describe the syntax and available options for bash commands and programs. The syntax for man is as follows:

    $ man [command]

    So, if you wanted to see more information about the cp command:

    $ man cp

  • info
    Information. The info command is similar to the man command, but the info pages can be a little easier to learn from if you're not used to reading man pages. If you want to see the info page for the cp command:

    $ info cp

That does it for Part 1 of the introduction to Bash.

See you next time.