Originally Published: Thursday, 22 June 2000 Author: Tom Dominico, Jr.
Published to: learn_articles_firststep/General Page: 1/1 - [Printable]

Shell Skills, Part 1: Working with Files

So, you've got Linux installed and running, and now you're staring at a blank shell prompt. You're thinking to yourself, "OK...now what?" In this article, we'll give you the background you need to start using the command line effectively.

   Page 1 of 1  

So, you've got Linux installed and running, and now you're staring at a blank shell prompt. You're thinking to yourself, "OK...now what?" In this article, we'll give you the background you need to start using the command line effectively.

First, let's talk about the shell. The shell is a program that takes your typed commands, and passes them to the operating system so that it can perform some sort of action. There are different shells available to you, and each one behaves differently. By default, most Linux distributions use the Bash shell, so that's the one we'll use for our examples. If you want to find out which shell you are using, type the following at a command line:

echo $SHELL

If you see something like "/bin/bash", you'll know that you are using the bash shell. Other shells include pdksh (the Public Domain Korn Shell), tcsh (the TC Shell), and zsh (the Z Shell). For most users, bash is an excellent choice. Bash has some very nice features such as a command history. Pressing the up arrow will go through your most recent commands, so that you don't have to retype them. Bash also supports tab completion, meaning that if you type part of a path or filename, bash will attempt to complete it for you when you press the TAB key. If there are multiple possibilities, pressing "tab" twice will display them all.

Another time-saving feature of the shell is the ability to use "wildcards" when typing in filenames. Wildcards are special characters with a particular meaning to the shell. Lets go through some examples, assuming that we have the following files in our home directory:

article.txt article1.txt article2.txt article33.txt

  • The question mark ('?') represents any single character.

    tom@murdock:~$ ls article?.txt article1.txt article2.txt

    Notice that it didn't match 'article33.txt', because the question mark only represents a single character.

  • The asterisk ('*') matches any number of characters, including zero characters.

    tom@murdock:~$ ls article*.txt article.txt article1.txt article2.txt article33.txt

  • A set of brackets ('[]') can hold what is known as a "character class". What does that mean? Well, The shell will only use the characters inside the brackets when matching filenames.

    tom@murdock:~$ ls article[12].txt article1.txt article2.txt

    Note that this would not match a file named "article12.txt". It will only match a filename that starts with "article", is followed by a "1" OR a "2", and ends with ".txt".

You can mix and match these characters to form very powerful commands, making the task of working with files much easier.

tom@murdock:~$ ls article[13]*.txt article1.txt article33.txt

Well, you get the idea. Wait a minute, though - what about this 'ls' command we've been using in the examples? Well, that's the "list" command, and lists information about files and directories. It can accept a variety of useful arguments. An argument is another name for the options you type after the command name. They usually start with one or two dashes. For example, here are some of the more useful arguments to the 'ls' command:

'-a': Show hidden files. Hidden files start with a period ('.'). '-l': "Long" listing. Show file sizes, security information, etc. '-F': Helps you to identify file types by displaying a slash ('/') after each directory, an asterisk ('*') after executable files, and the at sign ('@') after symbolic links. '-X': Sort files by their extensions (such as ".txt"). Filenames without extensions appear first. '-t': Sort files by the time of last modification, with the most recently modified files being displayed first.

You can use multiple arguments at once. For instance:

tom@murdock:~$ ls -alt

This would show hidden and non-hidden files in a long listing, sorted by time of last modification. There are many other helpful arguments. Type 'ls --help' to see them all.

You should now have enough information to starting working more effectively with files. In Part 2 of this article, we'll take a look at things like pipes, redirection of standard input/output/error, and other important shell concepts. As always, we welcome your suggestions - if there's a topic that you'd like to see covered on FirstStep, send an email to tomd@linux.com.

Tom Dominico (tomd@linux.com) is the FirstStep Project Manager. If you're interested in helping out the community by becoming a writer or news contributor for the section, send email to hr@linux.com.





   Page 1 of 1