All pastes #2086285 Raw Edit

Anonymous

public text v1 · immutable
#2086285 ·published 2011-10-02 17:26 UTC
rendered paste body
ACM 369 LINUX LAB 2 FURTHER LINUX COMMANDS
1. FILENAME EXPANSION AND WILDCARDS (Pattern Matching): To save time in commands and to refer to several files in one statement, Linux provides special characters (? and *) known as wildcards or metacharacters.  Question mark (?) stands for any single character and asteriks (*) stands for any string of character.  Examples: ls ab?  :matches 3 character filenames begin with "ab". ls ab* : matches  filenames begin with "ab". All this is similiar to MS DOS. 

Exercises: a) List the files with 3-character filenames. b) Remove the files with filenames begining  with a and ending with z.  c) List all c files. d) List only hidden files.

Another way to do filename substitution is by matching on character sets. A character set is any number of single alphanumeric characters enclosed in square brackets-[ and ].
Examples: [ab]c : matches ac and bc , ab[1-8]: matches ab1, ab2,...ab8
Exercise:  Interpret the patterns  a. *[0-9]*	b. [p-tz]??	c. [A-Z]*	
Exercise: List files with "o" or "c" extension.
2. FURTHER NOTES ON DOCUMENTATION: We already saw that help is available by typing man commandname or info commandname or  commandname --help. Now navigate to the directory /usr/share/doc to see what documentation is available. Now learn what the following commands do by typing the command. If you are lost, ask for help as described above. Some of these commands will need a file, in that case create one as described above. For example, you can find out what uname does by using the command "man uname" and "uname --help". For a more detailed manual, you have to use "info coreutils uname" since uname forms part of the coreutils package.
3. REDIRECTION AND APPENDING: The command program prog>file redirects the program output to the file, erasing its old contents while prog>> file appends the output to the file. Use the following commands. Examine the contents of file1 after each step.
cal 1980 > file1
cal 1981 >> file1
less file1
Verify that you can append one text file to another by  doing cat file1 file2 > file12.
4. PIPING: Unix allows you to connect processes, by letting the standard output of one process feed into the standard input of another process. That mechanism is called a pipe. Example: Using the more command, and a pipe, you can manage the screen presentation of command output. For example, to route the output of command 1 as input to command2 we use command1 | command2.  Using the more command, and a pipe, you can manage the screen presentation of command output. Examine the contents of the /etc directory by typing ls -l /etc | more.
5. Exercises a) Save the names of c program files to the "cfiles" file. b) Combine a.txt and b.txt files in c.txt file. This will require to cat the two files and redirect the output to the third as described above
6. SOFT LINKS: Links let you give a single file more than one name or include the same file in different directories. The ln –sf command is used to create a link to a file. Watch the order ln –sf aa bb  creates a link named aa to file bb. As an exercise create a new directory and link the file that you created in Step 2 to this directory. You can link a directory by omitting the f option. 
7. Try the following command sequence by typing the following commands in order.
1. cd 
2. pwd
3. ls 
4. ls -al 
5. cd . 
6. pwd     (where did that get you?) 
7. cd .. 
8. pwd 
9. ls -al 
10. cd .. 
11. pwd 
12. ls -al 
13. cd .. 
14. pwd     (what happens now) 
15. cd /etc 
16. ls -al | more 
17. cat passwd 
18. cd - 
19. pwd 
8. Explore /dev. Can you identify what devices are available? Which are character-oriented and which are block-oriented? Can you identify your tty (terminal) device (typing who am i might help); who is the owner of your tty (use ls -l)? 
9. Explore /proc. Display the contents of the files interrupts, devices, cpuinfo, meminfo and uptime using cat. Can you see why we say /proc is a pseudo-filesystem which allows access to kernel data structures? 
10. Make subdirectories called work and play. 
11. Change into subdirectory play and create a symbolic link called terminal that points to your tty device. Do not try to make a hard link to the tty device; this is dangerous. 
12. Create a file called hello.txt that contains the words "hello world" using any editor that is available. The midnight commander, mc, jed, joe, pico, kate or kedit may be available. The standard Unix editors are vi and emacs but they are more difficult to use.
13. Can you use "cp" using "terminal" as the source file to achieve the same effect? Copy hello.txt to terminal. What happens? 
14.  Describe three different ways of setting the permissions on a file or directory to r--r--r--. Create a file and see if this works. 
15. COMPILE AND RUN A C PROGRAM: COMPILE AND RUN A C PROGRAM: The C compiler is usually gcc, it accepts C language source files using extension .c. Run Kernighan-Ritchie's first example by entering these lines into a file with extension .c using an editor.
#include <stdio.h>
main()
{printf(" Hello World", "`\n ");
Compile the file using gcc filename. Run your program using ./a.out. Then run the second example.
#include <stdio.h>
/* Print Fahrenheit Celsius Table for fahr=0,20,...300 */
main()
{   int fahr,celsius;
    	int lower,upper,step;
    	lower=0;
    	upper=300;
    	step=20;
    	fahr=lower;
    	while(fahr <= upper) {
     celsius=5*(fahr-32)/9;
     printf("%d\t%d\n", fahr, celsius);
     fahr=fahr+step; } }


LAB ANSWERS:
1)using "ls ???" command listed all files which has 3 characters
b) using "rm a*z" command removed the all files which begin with a and end with z.
c)since c files end with .c using "ls *.c" listed all c files.
d)since hidden files begin with "." dot, using "ls .*" command listed all hiddden files. ls -a would work also.
2) using “cd /usr/share/doc” related directory is seen. There were lots of documents
3) 
3)