Linux Commands
Let's try to learn some more commands. If you did not create the script
in the last lesson. Enter the following 2 commands, after having logged
in to the system, of course:
- Login to the system. Make sure you are in your HOME directory. (typing
"cd" by itself and pressing return will put you in your
home directory)
- Type the following command:
"cp -f /home/files/myfirstscript
." (don't forget the period). For non-ctssn.com users, see note below
- Now type "cp -fR /home/public_html ."
- Type in "cat myfirstscript". What happens?
- Now, type in "more myfirstscript" What do you see this
time?
- Now, type in "less myfirstscript" What do you see this
time?
- Try this command "ls -al /etc/" Did you get to read everything?
- Now, try this command: " ls -al /etc/ | more" The "
| " is above the enter key on your keyboard. It is called a "pipe".
Can you figure out what it does?
- Try the previous command using the command "less" instead
of the "more". Does it work?
- Try the previous command without using the pipe character. What
happens now?
- Type "cd" to make sure you are in your home directory.
Do an "ls -al" What is the information you see on the new
file?
- Now, let's try to execute the script we copied over. Type "./myfirstscript"
Take note: that is a period followed by the forward slash, followed
by the name of the program. What happens?
- Now, type in "chmod 755 myfirstscript" and try to execute
the script again. What happens this time?
- Type in "mv myfirstscript mynewnamescript" . Do an "ls
-al" , what do you see?
- Let's rename the script back to it's original name. "mv mynewnamescript
myfirstscript"
- Does it still run? (It should, but make sure.)
- Type "cat lesson1.html | grep UNIX" . What do you see?
- Type "grep UNIX lesson1.html". Now what do you see?
- Type in "man grep". Do you get an indication of what grep
is used for?
- Finally, type "grep UNIX lesson1.html > UNIXTestFile"
. Do an "ls" and look for a file called "UNIXTestFile". Type in "more UNIXTestFile" . What do you see?
- Delete the UNIXTestFile. ("rm UNIXTestFile")
- Make sure you are in your home directory and type "cp lesson1.html
./public_html" .
UNIX Commands
| cat |
Concatenate files and print on standard
output. Works like the DOS "TYPE" command. Simply put,
"cat" can be use to read the contents of a file. |
| chmod |
Change the permissions on a file or directory |
| grep |
Find line matching a certain pattern. Very powerful
search tool. |
| less |
Filter for viewing files and directories. Works the
same as the "more" command, but lets you scroll backwards.
It is a little bulkier than more. |
| more |
Filter for viewing files and directories a screen
at a time. |
| mv |
Move or rename file |
| > |
A cool feature of Linux is that you can
easily send the info that normally gets printed on the screen into
a text file instead. This is called redirecting output. . For example,
ls > newFile, will list all files in a directory and write
them to a file called "newFile". If "newFile"
already exists, the contents will be over-written, otherwise a new
file will be created. This very useful feature can be used with many Linux commands. |
| >> |
Append the output of a program to a file.
If the file does not already exist, it will be created. If it does
already exist, the output will be written to the end of the file. |
| | |
Called the "pipe". It is used
to redirect the output of a command to another command. |
| |
|
You will use the "cat" command quite often. You might be
told to "cat the file", which means to read it using the cat
command. For example, "cat lesson1.html" will let you read
the file, lesson1.html. In a future lesson we will describe "chmod"
and file permissions in greater detail. For now, just know that if you
want to execute a file as a program, you should enter "chmod 755
filename".
The "|" (pipe) character allows you to pipe data from one
program to another program or command. For example, if you change directories
to /dev and do an "ls", the listing goes by too quickly to see what is there. Try this yourself. Now, use the pipe function and
pipe the results of ls to a paging program like more. Here's how: ls /dev/ | more Now you can scroll through the results of the "ls" command.
"grep" performs searches in files and directories. It is often used with wild cards to find information. For example, cd into your public_html directory. If you type, "grep linux *", a search will be performed on ALL (*) files in the directory for the word "linux." Then each line containing the word "linux" will be printed to the screen. If you want to save your results to a file, you can use the greater than character to redirect the output to a file. For example, "grep linux * > linuxLines" will perform a search on all files in the directory for the word "linux" and save the output to a file called "linuxLines". If the file already exists, it will be overwritten. To avoid this, you can use a 2 greater than signs to append the output to the end of the file.
|
|
Now for the Lab Assignment:
- You have a file named
"index.html" in your home directory. In a new file that you write with vi, please list 3 commands to quickly view the file.
- Make a copy of the file and call it "myPractice.html".
- Search the file for all
lines with the word "zip" in it and save them to a file
call "mypracticeresults.txt".
- Print the contents of
mypracticeresults.txt to the screen.
NEXT LESSON
Special note for NON-ctssn.com account holders: If you do not have an account with us, yuou will not be able to perform steps 2 and 3 on this page. This command copies files into your home directory from a location on our server. However, you can still download the practice files onto your linux box and follow along. Create a folder in your home directory called public_html, and go to Lesson #7. Use FTP to get the files. They are located in the pub folder. For step 2 on this page, myfirstscript is a shell program that you were to type in (or copy and paste) during lesson 4. Consequently, you should not have to copy or download it.
|