Linux-Shell N Commands
Linux-Shell N Commands
Linux-Shell N Commands
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.
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.
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.
'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'. Typing: cd /[directory name] will get you into one of the main directories in Linux. Typing cd .. 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
Normally we'll want to add on some parameters (those -[letter] combinations) so that we'll get some more detail
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.
Let's go back to Tony's 'stuff' file For example, if you saved Tony's e-mail attachment to your main /home directory, /home/[your name], you may want to create a directory to keep Tony's files You could make the directory for Tony tonyd (Tony's last name is Dweebweiler) mkdir tonyd then you can do: cp stuff tonyd Remember use your TAB key to save time. Now you're going to have TWO files named 'stuff' because you copied that file to the directory 'tonyd/' - you didn't move it there. You'll have the original 'stuff' in your home directory and then the copy in /home/[your name]/tonyd/. You'll be able to tell the difference between the two files because the copy of 'stuff' in the directory 'tonyd' will show a different time. If you use the command ls -l stuff on both files, you'll see this. If you had used the command cp -p instead of just cp you would end up with two identical files in two different places. If you don't want that, there's a better way of doing it so that 'stuff' is only in the directory 'tonyd'. That's the mv command. We'll talk about that shortly.
cp stuff stuff2 or choose a name that's meaningful for you other than 'stuff2' Now you have another file that you can add jokes to while you preserve the original file. You can open it in 'pico' and start writing: "Why did the chicken cross the road..." Always remember to use that TAB key and the up and down arrows to save yourself some time. Now we'll look at the command 'mv'.
What? You don't have the directory 'my_friends'? Well, let's create it with another command.
If you created a file called 'bad_jokes' and you wanted to get rid of the file, you would type rm bad_jokes, and because you made an alias, it will ask you. rm: remove `bad_jokes'?: You would press the the 'y' key unless of course you remembered that you have a real good one in there and then you would answer: with the 'n' key. Actually, any key other than 'y' is the same as responding with the 'n' key, so if you accidentally type 'w', don't worry.
'who'
'who' is a command to find out who's working on your system. As you now know, Linux is a multi-user system. Even if you're using one computer at your home, you may be working as more than one person. For example, if you logged in as 'root' but are working as 'bob'. You may see something like this: root tty1 May 20 09:48 bob tty2 May 20 10:05
This is just Linux's way of saying that 'root' started working on terminal 1 on May 20 at 9:48 in the morning and bob started working on terminal 2 at 10:05. This is mainly used in networked situations so the system administrator knows who's working. It can be used by your boss to find out that you've come in late too. You may use it to find out if you've opened more that one terminal so that you remember to log out.
You will probably not be using it a lot because we all know how perfect Linux is and how few errors there are when you're using it. But every once and a while you may want to download some software from the Internet You install it and - whoops! - there's some error. You may not have something installed that the program needs to run. You could just do something like this: [program X that doesn't work] 2> program_X_error You create a file with the error message. You could show it to someone who might know what's missing or you could send it to the author of the program. He or she would also like to know about it and will probably help you fix it.
whoami'
whoami is a nice little program that tells you who you are, just in case you didn't know already. You amnesia victims are in luck! Actually it tells you who you are in terms of how Linux understands who you are, that is to say, your user name. So if your user name is bob and you type whoami you'll get: bob This comes in handy if you switch terminals a lot and work as a different user. You may, in terms of computer use anyway, forget who you are!
'whatis'
To show you how to use the 'whatis' command 'whatis' is a command so you can find out what a program does. If you explore your Linux system, you will find a lot of programs and you may not know what they do. You would simply type: whatis grep for example, and you would get this:
grep (1) - print lines matching a pattern
Linux is good, but it's not all-knowing, so if you type: whatis orange juice You will get this message:
orange: nothing appropriate. juice: nothing appropriate.
basically telling you that Linux has no idea what orange juice is
'whereis'
whereis is a nice command for finding other commands or programs. If you decide to download any program from the internet, that program may need other programs in order to work. If you want to know whether or not you have it, you can type: whereis [program name] and find out. If you wanted to find out if you have the 'pico' editor and where it is, you would type: whereis pico and you may get this:
pico: /usr/bin/pico /usr/man/man1/pico.1.gz
It shows you where the command is as well as the location of its manual file. whereis isn't designed to find people, so if you type whereis Harry Linux is just going to say Harry:
'which'
To show you another tool for locating programs 'which' is similar to 'whereis'. It will give you the location of a program. At times, a program may not find another program it needs to make it run. It will need to know its location or "path". For example, a program may need Java to run it but thinks its in another place. You would simply type: which java and Linux will inform you of its location
/usr/lib/java/bin/java
This is a handy command because some locations of programs vary from one version of Linux to the next. A software developer may have designed his/her program to access Java, for example, from a different location. As Open Source software will always let you modify configuration files to get your program working according to your needs, you can get the program to work for your system.
echo'
To show you some uses of the 'echo' command 'echo' is a little command that repeats anything you type. For example if you type echo hello Linux will display the word 'hello' . There is really no need to do that under normal conditions. You may find 'echo' useful in the future if you start writing "shell scripts" which are like little programs that you could use to do a few commands at one time. You could use 'echo' in those scripts to tell you what the script is doing at any given time, or to prompt you to do something, like enter text. Shell scripts will be taken up in a later course. There is a practical use for 'echo' in everyday life. I sometimes use it to write short notes. If we use 'echo' along with 'pipe' (|) and 'tee', you've got a poor-man's post-it-note. For example: echo remember to tell Bill Gates he owes me 5 bucks | tee -a bill_gates.note Will make you a nice reminder note about dear 'ole Bill. Just remember to read your note. You could type: echo remember to open Gates note | tee -a remember_gates.note to make yourself a reminder for the other reminder note. Use less bill_gates.note or less remember_gates.note to read your notes
'wc'
People following this course from Europe may recognize this as the symbol for 'bathroom'. Unfortunately, if you type this in your terminal it will not show you the way to the 'facilities'. Actually, this command will give you the number of lines, words and letters (characters) in a file and in that order. Let's go back to the file about the people I owe money. If I type: wc people_I_owe_money.note I will get this output:
439 6510 197120 wc people_I_owe.note
As you can see, there are 439 lines, so that means if each line represents one person, then I owe 439 people money. There are 6510 words and a total of 197120 characters. I might add that this is a good tool for people who write letters professionally and get paid by the word.
Miscellaneous commands
These might come in handy from time to time 'dir'
The people who traveled down the MS-DOS road will know this one. Actually, dir=ls -l. It will give you the same result. If you do any downloading or uploading of files via FTP by way of a non-GUI FTP program in your terminal, you may find this command useful. I once ran into a case where the remote computer didn't recognize the ls -l command. I just typed in dir and that did the trick. Then I fired off a nasty e-mail asking why in the world they weren't using Linux!
'pwd'
The command pwd will show complete information on the directory you're working in. For example, if you type pwd you may get something like this:
/home/bob/homework
which shows you that you're in the directory 'homework' in your user directory as 'bob', so you know exactly where you are.
'date'
Did you forget your wedding aniversary? Your boyfriend or girlfriend's birthday? Tax day? (everyone wants to forget that one!) No need for that to happen anymore with Linux. Just type: date You'll get this: (or something like it, actually. If you get the same thing as I do, then I'd consider buying lottery tickets)
Thu Sep 7 20:34:13 CEST 2000
You probably get everything here. If you're living in central Europe, you will recogize the 'CEST' part. That stands for 'Central European Standard Time'. Linux recognizes world time zones and you set this up when you installed Linux. If you live on Mars, you're out of luck, unfortunately. There are other uses of the 'date' command. To see just the date, type: date +%D To see just the time, type: date +%T To see on what day Christmas falls this year (really, I'm not kidding!), type: date --date 'Dec 25' and you'll get the day that Christmas falls on this year. Substitute that for any date that you'd like to see. There are many other options. Consult your manual file ('man date') or ('info date')
'cal'
Typing cal will give you the calendar of the present month on your screen, in the nice standard calendar format. There are a lot of useful options. If you type: cal 2000 You'll get the entire calendar for the year 2000. Substitute any year you like. If you type: cal 12 2000 You'll see the calender for December of 2000. Substitute any year or month you like.
If you add the option cal -m, the week will start on Monday, as it is preferred in many countries. Just for fun, I typed cal 10 1492 and I found out that Columbus discovered America on a Friday. That was good luck for him because that way he got to relax for the weekend.
'exit'
As you can guess, you can get out of a terminal with the exit command. If you're working in text mode, typing exit will prompt you to login again. If you want to work as another user, use logout instead. If you're in x-windows, exit will close the X-Terminal you're working with exit with the option "stage right" will get you an error message.
'last'
The command last will show you the people who have logged into the computer today and the terminals they are/were using. If you type: last You may get something like this: fred tty6 Thu Oct 5 16:55 - 20:05 (3:10) bob tty1 Thu Oct 5 still logged in (3:10) root tty1 Thu Oct 5 16:23 - 16:43 (0:20) reboot system boot Thu Oct 5 16:22 As you can see, you worked as 'fred' for 3 hours and 10 mins. You are still working as 'bob'. You worked as 'root' for 20 minutes (probably some administration tasks) and you booted your computer at 4:22 PM. This is a good way to see who's been using the computer if it's networked. For example, if you saw a an entry for 'satan' and you hadn't given the Prince of Darkness permission to login, you could fire off a nasty e-mail to him about mis-use of your server. His address, by the way, is "thedevil@hell.com"
'df'
'df' is a command that you're going to use a lot if you're pressed for hard disk space. Once again, there are many programs that run graphically that will inform you of the space available on your Linux partition. But this is a very good, quick, non-graphic way to keep track of your hard disk space. If you type df You may get something like this. (This is actually taken from a system I use for testing versions of Linux.
You'll see it's somewhat pressed for space.)
Filesystem 1024-blocks Used Available Capacity Mounted on /dev/hda2 481083 408062 48174 89% / if you start seeing a 'df' output like this, it's time to get down to your local computer shop and buy a new hard disk. Anyway, 'df' is a good way to keep track of this.
'free'
'free' is a command that you can use if you want to know how much RAM memory you have free on your system. By typing: free and you will get something like this total used free shared buffers cached Mem: 14452 13904 548 28208 492 7312 -/+ buffers/cache: 6100 8352 Swap: 33260 1556 31704 Sometimes, if a program is running particulary slowly, you may find out that your memory usage is high using this command. Linux's memory management is quite good but a certain program may be "hogging" memory. You could exit that program and then type free again to see if it was the culprit.
'du'
'du' is the way to see how big files are. You can use it on a directory or on a particular file. This is another command I use a lot. It's probably best to use the option du -b (-b for bytes) and it will give you the exact figure in bytes. By default, 'du' shows the closest kilobyte figure. Let's look at a couple of examples: If I type: du people_I_owe_money.note I may get an output like this:
194 people_I_owe_money.note
As you can see, it's a big file. I owe a lot of people money. On the other hand look at the output for 'people_who_owe_me_money.note':
1 people_who_owe_me_money.note
No, that's not the kilobyte figure. That's the byte figure! You can also use this on a directory, and it will list the files and subdirectories and give you the byte or kilobyte count, whichever you prefer
top
To show you the use of the 'top' command. Here you will see what processes are running 'top' is a good command to use when you want to see what your system's doing. 'top' is designed to show you how your CPU is being used. It will give you a pretty complete list of everything that's going on in your computer. Here's a sample output of the 'top' command:
9:09am up 4 min 2 users load average: 0.12 0.13 0.06 34 processes: 31 sleeping 3 running 0 zombie 0 stopped CPU states: 11.1% user 0.7% system 0.0% nice 88.4% idle Mem: 62972K av 50420K used 12552K free 22672K shrd 5312K buff Swap: 1020116K av 3152K used 1016964K free 27536K cached PID USER PRI NI SIZE RSS SHARE STAT LIB %CPU %MEM TIME COMMAND 332 root 12 0 4492 4184 1480 R 0 8.3 6.6 0:09 X 350 bob 4 0 13144 11M 7728 S 0 2.7 19.3 0:04 netscape 386 bob 1 0 768 768 596 R 0 0.5 1.2 0:00 top 345 bob 0 0 972 972 704 S 0 0.1 1.5 0:00 FvwmButtons 1 root 0 0 168 168 144 S 0 0.0 0.2 0:02 init 2 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 kflushd 3 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 kpiod 4 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 kswapd 5 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 md_thread 48 root 0 0 136 120 96 S 0 0.0 0.1 0:00 update 137 bin 0 0 300 296 228 S 0 0.0 0.4 0:00 portmap 141 root 0 0 292 264 212 S 0 0.0 0.4 0:00 rpc.ugidd 157 root 0 0 512 508 412 S 0 0.0 0.8 0:00 syslogd 161 root 0 0 692 688 296 S 0 0.0 1.0 0:00 klogd 212 at 0 0 304 276 220 S 0 0.0 0.4 0:00 atd 225 root 0 0 424 416 348 S 0 0.0 0.6 0:00 inetd 258 root 0 0 580 548 428 S 0 0.0 0.8 0:00 lpd
If you happen to forget what your name is, you can type ps -u. This is the user mode and your user name will appear in the first column. Actually, there's more than that. There will be other columns about memory usage, the time you started running the processes and others. You can see other users processes if you type ps -au. If you're not using a networked computer, you will see yours and root's processes. If you're into detective work, you can type just ps -a and try to guess who's using the process. The information that you'll probably be most interested in is the column that shows the "process ID" or "PID". We'll get into why these are so important in the next part of the lesson.