Sea shells by the seashore, Can you say that ten times fast? How about 'Shells in Linux'? That's not as difficult. Learning to use shells in Linux isn't going to be difficult either.
Why you need to use a shell As I mentioned in a previous lesson, a shell is just a way for your computer to receive commands. The most common shell used for working in Linux is the 'bash' shell. Our lesson will deal with that one.
The most common commands a computer receives are ones to copy files, move files around, list files and delete files. Popular operating systems have perfected this to such a degree that they have graphic interface programs to do all this for you just by moving the mouse around and clicking on a few buttons. Linux has these programs too, but anybody who's seriously thinking of using Linux on a day-to-day basic should be familiar with the commands that you type in by hand. Some people see this as a throw back to the old days. I see it as a way to have more power over your computer because even those operating systems that are billed as more 'user friendly' have provided you with a shell, just in case you need it. And sometimes you do!
The '.bashrc' file Before you start using the 'bash' shell you should be aware of a file that sits in your home directory called '.bashrc'. You'll find a lot of files on the system that end in 'rc'. Those files allow you to configure a certain program to run just the way you like it. The best way to find it is to type.
(ls lists files)
You can open that file with vi, joe or pico, as we talked about in the last lesson.
For example, in your home directory you would type:
An introduction to aliases In that file, you can add something called an 'alias'. Everybody knows what 'alias' means- 'an assumed name'. An 'alias' in this file are some lines that you write so that your bash shell assumes that one command is really a variation of it. As you already know, you can modify a command with a dash '-' and a letter To see where the .bashrc file was, you could have typed 'ls -a' and that would have shown you every file in the directory, including those that start with '.' If you find yourself using these '-letter' combinations a lot, you can modify your .bashrc file so that even though you type the simple command, like 'ls', you actually get 'ls -a'. Some of these aliases may be very important to keep you from sending that novel you just wrote into non-existence by accident. I have a couple of entries in my .bashrc file to keep me from getting into trouble. They are:
alias cp='cp -v -i' alias rm='rm -i' alias mv='mv -i'
Let me explain them. 'cp' is the command to copy a file to another place or to make a copy of a file under a different name. In order not to copy a file to a place where there's already a file by the same name, you could type cp -v -i, (-v for verbose, -i for interactive) and it would ask you if you really want to do it in case there's another file by the same name. Then the -v would show you where it went. This is probably a good idea all the time, so you could create an alias for it in your '.bashrc' file. 'rm' is the remove/delete command. In Linux, this means gone forever [cue ominous organ music] You obviously have to be very careful with this one, because in the bash shell there is really no 'trash' bucket to pick it out of if you delete it. That's why I've added the -i (interactive) command to my alias, so that it asks me if I really want to delete that novel I just wrote.
'mv' is for moving files to a different place or renaming a file. I have an alias for it for the same reasons as the 'cp' command.
Adding aliases to the .bashrc file
Well, you now have '.bashrc' open in 'pico' or your new, favorite text editor. It would be a good idea to add this line first, so you know what you've done.
# my personal aliases The pound sign (#) tells the shell not to read that line. It's known as a 'comment'. Then you would add:
cp='cp -v -i'
on the next line write:
so we don't send anything into byte heaven without a warning. And finally mv='mv -i'
So you're aliases will look like this
alias cp='cp -v -i' alias rm='rm -i' alias mv='mv -i'
Save that file and logout and login again. Now you have a safer, easier shell environment. As you get more proficient at Linux, you can add more aliases as you see fit. Now your shell's ready to go. If you type logout and then login again, your aliases will work. There is also a short-cut. If you type: source .bashrc your aliases will be ready to go.
Frequently Used Shell Commands
If you install a window manager like KDE, you can copy, delete, move and rename files by way of a graphic user interface like Konqueror. But as I mentioned before, shell commands are pretty standard in Linux, so we're going to teach you the ones that you're most likely to use. You can use them both in text mode or in your x-terminal when you're in graphics mode. 'cd' command
To show the student the basic uses of the 'cd' command The first command you'll use is 'cd'. We talked about this in a previous lesson, so let's review the basics. 'cd' means 'change directory'.
cd /[directory name]
will get you into one of the main directories in Linux.
Will get you out of it.
Typing cd without the / and a sub-directory name will get into that subdirectory.
Remember, you don't have to type the whole name
Typing: 'cd' and the first letter or letters of a directory and the TAB key will complete it for you. Then all you have to do is press enter.
If you type just: cd
you'll go back to your home directory
The 'ls' command
To show the student the various variations of the 'ls' command 'ls' is another command that we've discussed a bit before. Let's go into some more detail. Typing 'ls' will list the contents of a directory with just information about file names.
You can use 'ls a*' to list the names of all the files that begin with the letter 'a' and so on down through the alphabet. Please do not use the cuneiform alphabet unless you're from ancient Mesopotamia.
Normally we'll want to add on some parameters (those -[letter] combinations) so that we'll get some more detail Using
To show the student how to get some more detail with 'ls -l' 'ls -l' will get you a detailed listing of the directory like this. -rw-r--r-- 1 bob users 103824 Jul 10 12:01 waikiki.jpg
The first part, those letter, are file permissions. We'll go into that in a later lesson. That basically shows what you and others are allowed to do with the file, like read it, modify it or make nasty comments about it.
The next one shows that you've got 1 file. The next one that it belongs to you, 'bob'. The next one represents that Linux, not only recognizes users, but also groups of users. We'll go into that in the next course. The numbers are the size of the file in bytes. You have the date and when it was created or modified or copied there. Lastly, you have the name of the file, waikiki.jpg, which is obviously your important tax records.
'ls -l' will give you a detailed listing in alphabetical order, starting with any file that begins with a number, then any file that begins with a capital letter and then any normal file that begins with a lowercase letter. So if you have a file '007_secrets.txt' that'll be the first one you see.
Other parameters with 'ls'
Now let's add some more parameters on 'ls -l -t' will give you a listing according to the time with the newest ones appearing first in the list. You don't need to separate the parameters either. 'ls -l -t' and 'ls -lt' are the same thing. If you want the oldest to appear first,
The 'r' is for reverse order.
There are a couple of parameters in capitals that you may want to use. 'ls -lS' will list your files from biggest to smallest. 'ls -lSr' will reverse this order. 'ls -lX' will list files according to type in alphabetical order. If you have any that end in *.zip, then those will be last. If you want those to be first, now you know: 'ls -lXr' There is also a parameter that will help you if you want to find out more about these commands or any others. 'ls --help' will give you details of all the possible variations for this command.
The 'file' command
File is an interesting little command. MS-DOS got us all used to the idea that a file had to have an extension (file.extension- like 'novel.txt') that told us what kind of a file it was. In Linux, it's 50/50. You don't have to do that if you don't want. In MS-DOS, programs end in '.exe', in Linux, they don't have to and probably won't. Linux is also color coded, as you know, so once you get used to the colors, you'll know what the file is about. Let's say your friend Tony, the one with the magic markers, sends you a file he says is really neat. It's attached to an e-mail message and it says 'stuff'. You can see what type of file it is by typing: 'file stuff'
You may see something like this: 'stuff: ASCII text'
This means that the file contains plain text. It's probably some jokes or his favorite chili recipe. If you don't know what some particular file contains and you're curious about it, you can use this command to get some information about the file. 'more' and 'less'
To introduce the student to these commands 'more' is a command that you can use to read, for example, what's written in that 'stuff' file that Tony sent you. You would type 'more stuff' to see the jokes. Press the 'q' key to stop viewing the file
It's useful for this, but not really ideal. We'll talk more about 'more' in this lesson when we use it in combination with other commands 'less' is better for viewing files. You can scroll back up to see the whole text if you want. You can't do that with 'more'. 'less' is more than 'more', if you get my meaning. type