Documente Academic
Documente Profesional
Documente Cultură
1. Command Shell
A program that interprets commands Allows a user to execute commands by typing them manually at a terminal, or automatically in programs called shell scripts. A shell is not an operating system. It is a way to interface with the operating system and run commands.
2. Command Syntax
Commands can be run by themselves, or you can pass in additional arguments to make them do different things. Typical command syntax can look something like this:
The command line prompt that shows up in the terminal window contains useful information. It has the form: murphy@murphy-laptop:~/tp$ There are three parts in the command line prompt, separated by @ : and $ murphy@ murphy-laptop : ~/tp $
The first part before the @ shows the user which is logged in at the moment. In this example it is the user murphy. The second part between the @ and the : shows the name of the computer you are working on. In this example it is murphy-laptop. The third part, between the : and the $ shows the current directory you are working in. In our example it is ~/tp (~ is a shortcut for the users home directory; ~/tp is equivalent to /home/murphy/tp).
Exercise1:Identify the three parts of the command prompt on the computer you are working on. What is the name of the logged in user? What is the name of the computer? Which is the current directory immediately after opening up the terminal window?
i.
At any time, in order to see the current directory you can type pwd The letters come from print name of current/working directory. Example murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp Exercise 3:Type pwd and see the full path of the current directory.
ii.
In order to see the list of files and directories that are found in the current directory, type ls The name of the command comes from list directory contents. Example murphy@murphy-laptop:~/tp$ ls laborator01 test.c
Two names are listed: laborator01 and test.c. Usually they are printed with colors, and based on the color we can tell which are files and which are directories. In order to see more details about the files and directories, type ls -l murphy@murphy-laptop:~/tp$ ls -l
Files 2 1
size 4096 0
The same two entries are listed, but with more information next to each of them. We can see access rights, time of last modification, owner, etc. What is the most important for the moment is the first letter in each line. If the first letter in a line is d, then the name on that line is a directory. If the first letter is - then the name on that line is a file.
Exercise 4: Type ls to see the content of the current directory. Then type ls -l. Identify the files and the directories based on the first letter in each line. Then determine which color shows directories in the short form of ls, and which colors show files.
iii.
In order to change the current directory, type cd <new path on disk> The letters come from change the current directory. You must specify the path of the new current directory.
Example For changing the current directory to the sub-directory laborator01 you type murphy@murphy-laptop:~/tp$ cd laborator01 murphy@murphy-laptop:~/tp/laborator01$ pwd /home/murphy/tp/laborator01
For going one level up, to the parent directory, you type murphy@murphy-laptop:~/tp/laborator01$ cd .. murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp
The root directory is / murphy@murphy-laptop:~/tp$ cd / murphy@murphy-laptop:/$ pwd / An absolute path can also be specified, starting from the root directory: murphy@murphy-laptop:/$ cd /home/murphy/tp murphy@murphy-laptop:~/tp$ pwd /home/murphy/tp
Command cd /home
Description Change the current working directory to /home. The '/' indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home". Move to the parent directory of the current directory. This command will make the current working directory "/home. Move to the user's home directory which is "/home/username". The '~' indicates the users home directory.
cd ..
cd ~
iv.
In general the text editors are able to create empty files on disk. However, sometimes it is useful to create an empty file directly from the command line. In order to do it, type: touch <file name> An empty file with the specified name will be created. If a file with that name already exists on disk, it will remain there and moment of last update is changed in the properties of the file.
Example Watch the date shown next to the test.c file in the files listing. After executing touch upon the file, the date will change: murphy@murphy-laptop:~/tp$ ls -l
Files 2 1
size 4096 0
Files 2 1
size 4096 0
Note the appearance of the new file exemplu.c after running the touch command: murphy@murphy-laptop:~/tp$ touch exemplu.c murphy@murphy-laptop:~/tp$ ls -l
Files 2 1 1
size 4096 0 0
Exercise 5: Create an empty file named file.txt in your home folder. Type ls -l and see the last modification time of the file. Wait a minute or so and run touch on the file. See again the last modification time and notice the change.
v.
It is done through rm <file name> The letters come from remove files or directories. Example In order to delete the test.c file, we type: murphy@murphy-laptop:~/tp$ rm test.c murphy@murphy-laptop:~/tp$ ls -l
Files 2 1
size 4096 0
vi.
A new directory is created with the command mkdir <directory name> The letters come from make directories.
Example In order to create the directory laborator02 we type: murphy@murphy-laptop:~/tp$ mkdir laborator02 murphy@murphy-laptop:~/tp$ ls -l
Files 2 2 1
Exercise 7: Create a directory named test.dir in your home folder. Change the current directory to it. Type pwd to view the current directory.
vii.
Deleting a Directory
In order to delete a directory, type rmdir <directory name> or rm -r <directory name> The first command deleted only empty directories. If the directory contains any file or sub-directory, a warning will be displayed and the directory will not be deleted. The second command deletes the folder, recursively, together with its files and sub-directories. Example In the directory laborator02 we have created other files and sub-directories. First we try to delete the directory laborator02 using rmdir, then using rm -r. Notice the warning message issued after we try the first command. murphy@murphy-laptop:~/tp$ rmdir laborator02 rmdir: failed to remove laborator02: Directory not empty murphy@murphy-laptop:~/tp$ rm -r laborator02 murphy@murphy-laptop:~/tp$ ls -l
Files 2 1
size 4096 0
viii.
Copying a file
In order tocopy a file cp <sourcefile><destinationfile> Command cp myfile yourfile cp -i myfile yourfile Description Copy the files "myfile" to the file "yourfile" in the current working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists. With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.
Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before overwriting the file. Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-p option), file attributes (-p option), and copy recursively (-r option). With these options, a directory and all its contents can be copied to another directory.
ix.
Description Move the file from "myfile" to "yourfile". This effectively changes the name of "myfile" to "yourfile". Move the file from "myfile" from the directory "/data" to the current working directory.
The result of running the command is that the input C file will be compiled and linked, and the output binary file will have the name specified by us. The option instructs the compiler to print out all warnings about our source code. Example Suppose we have a program stored in a file test.c: #include <stdio.h> int main(void) { printf("Will it compile?\n"); return 0; } murphy@murphy-laptop:~/tp$ gcc -c test.c murphy@murphy-laptop:~/tp$ gcc o test test.c murphy@murphy-laptop:~/tp$ ./test The ./ characters are important when we are running programs from the current directory. We can see that the program ran successfully, and our message got printed.
Lab Tasks:
1. Verify that you are in your home directory. 2. Make the directory adir using the following command: 3. List the files in the current directory to verify that the directory adir has been made correctly. 4. Change directories to adir. 5. Verify that you have succeeded in moving to the adir directory. 6. Create a file testfile.txt 7. Verify that the file testfile exists. 8. List the contents of the file testfile to the screen. 9. Make a copy of the file testfile under the name secondfile. 10. Verify that the files testfile and secondfile both exist. 11. List the contents of both testfile and secondfile to the monitor screen.
12. Delete the file testfile. 13. Verify that testfile has been deleted. 14. Clear the window. 15. Rename secondfile to thefile. 16. Copy thefile to your home directory. 17. Remove thefile from the current directory. 18. Verify that thefile has been removed. 19. Copy thefile from your home directory to the current directory. 20. Verify that thefile has been copied from your home directory to the currentdirectory. 21. Change directories to your home directory. 22. Verify that you are in your home directory. 23. Verify that a copy of thefile is in your home directory. 24. Remove thefile from your home directory. 25. Remove thefile from the directory adir. 26. Remove the directory adir from your home directory with the following command. 27. Verify that thefile and adir are gone from your home directory. 28. Write a C program that tells the user to think about a number between 1 and
100 and then tries to guess the number by asking questions of the form Is your number greater than ...? and Is your number smaller than ...?. Save the program in a file called guess.c. Compile and link the file with gcc. Run the program.