Lecture 6” Area

Lecture 6” Area

● Log into the course site ● Enter the “Lecture 6” area ● At 14:00, choose “Daily Quiz 5” ● Answer the multiple choice quiz (you have 10 minutes to finish) Finding files and directories, deletion, standard streams, piping J. M. P. Alves Laboratory of Genomics & Bioinformatics in Parasitology Department of Parasitology, ICB, USP Removing files ● Files can be removed with the rm (for remove) command ● By default, rm only removes regular files, but not directories ● To remove a file called file_name, simply run: rm file_name ● Notice that removing counts as “writing”, so you need the adequate permission ● What happens if the file belongs to you but you do not have write permission? Let’s try! J.M.P. Alves 3 / 60 BMP0260 / ICB5765 / IBI5765 Let’s do it! ● First, try to delete file smart_file from your home directory rm smart_file ● If everything went well, the file should have disappeared. Check! ● Now, try to remove the file called ~/dummy_fileX ● Notice that, although the file is yours, you have to answer a question before deleting it J.M.P. Alves 4 / 60 BMP0260 / ICB5765 / IBI5765 Quiz time! Go to the course page and choose Quiz 13 J.M.P. Alves 5 / 60 BMP0260 / ICB5765 / IBI5765 ● As you saw, removing files and/or directories does not depend on the permissions to the items being removed, but on the permissions you have to their parent! (the directory where they are located) ● List again the contents of the test_dir directory that is in your home area ● Let’s look at the ownership and permissions for the another_dir directory: drwxr-xr-x 2 dummy dummy 4096 Apr 4 16:15 another_dir J.M.P. Alves 6 / 60 BMP0260 / ICB5765 / IBI5765 Let’s try that again... ● Can we remove directory another_dir or not? ● As seen before, no; we do not have write permission to the parent directory (test_dir) of another_dir ● But what happens inside of directory another_dir? ● Enter the another_dir directory ● Create a regular file there, called test_1 ● Did you succeed? Always check! J.M.P. Alves 7 / 60 BMP0260 / ICB5765 / IBI5765 ● Did you notice there is another file already there? -rw-r--r-- 1 root root 0 Apr 4 17:07 test_0 ● It belongs to root, not to you. Try to delete file test_0 ● Answer y to the question. What happened? ● This seems like a violation, but it is not! You did not have write permission to file test_0, but you did have it to its parent directory... ● The directory is a special file (one that users cannot change directly) containing a table of the names and inodes for the files and subdirectories contained within the directory J.M.P. Alves 8 / 60 BMP0260 / ICB5765 / IBI5765 ● A directory is just a table with two columns: file name and inode identifier (not shown here, but the first two entries of that table are . and ..) ● When you delete a file or directory, all you are doing is removing its entry from that table – the data will still be on storage for a while! ● Incidentally, when you create a hard link, you are just adding another entry to one of those tables, pointing to the same inode ● When the system sees that there are no more hard links to an inode, it deletes the inode as well, and your data will eventually get overwritten J.M.P. Alves 9 / 60 BMP0260 / ICB5765 / IBI5765 A directory as the system sees it This is why a dot The directory is a represents the file, so of course it directory itself has its own inode Each file and subdirectory inside the directory is represented by a row in the table J.M.P. Alves 10 / 60 BMP0260 / ICB5765 / IBI5765 So, if you have write permission to a directory, that means that you have the power to change that table, adding or removing rows (i.e., creating or deleting files, respectively), regardless of your permissions to those items located inside the directory! J.M.P. Alves 11 / 60 BMP0260 / ICB5765 / IBI5765 Deleting directories ● Files can be removed with the rm (for remove) command ● By default, rm only removes regular files, but not directories ● Like for cp, to remove a directory you must give rm the -r option: rm -r dir_name ● Notice that this will recursively delete everything inside a directory (i.e., all files, subdirectories, files within subdirs, subdirs inside subdirs etc.)! ● If there are subdirectories to which you do not have write permission, will removing them fail or not? J.M.P. Alves 12 / 60 BMP0260 / ICB5765 / IBI5765 Now you do it! Go to the course site and enter Practical Exercise 14 Follow the instructions to answer the questions in the exercise Remember: in a PE, you should do things in practice before answering the question! J.M.P. Alves 13 / 60 BMP0260 / ICB5765 / IBI5765 Deleting directories ● As you saw in PE14, you cannot delete a directory if you are unable to delete at least one item inside that directory ● By default, rm silently deletes whatever you ask it to ● That, of course, makes it a very dangerous command: there is NO undelete command, NO “trash can” or anything like that for the Linux command line ● Therefore, always be sure of what you are doing before running rm! ● Remember the ls or echo trick: replace rm with one of those commands first to make sure only the intended files will get deleted, and nothing more (or less)! J.M.P. Alves 14 / 60 BMP0260 / ICB5765 / IBI5765 The CLI (usually) assumes you know what you are doing and does not try to stop you from hurting yourself! J.M.P. Alves 15 / 60 BMP0260 / ICB5765 / IBI5765 Deleting directories ● Another way to delete directories is the rmdir (remove directory) command ● rmdir can only delete directories that are empty (of regular files) ● To delete multiple directories that are nested, one can use the -p option (also known as --parents) ● Examples: rmdir dir1 rmdir -p dir1/subdirX ● The second command will only work if there are no regular files inside either subdirX or dir1 (if there are, then rm -r dir1 should be the command used) J.M.P. Alves 16 / 60 BMP0260 / ICB5765 / IBI5765 Standard streams ● Standard streams (for data flow) are automatically connected input and output communication channels between a computer program and its environment, available when the program begins execution ● They are considered a special kind of virtual text files ● One of the most important concepts in the use of the command line! ● There are three such streams: ● Standard input (stdin) ● Standard output (stdout) ● Standard error (stderr) J.M.P. Alves 17 / 60 BMP0260 / ICB5765 / IBI5765 Standard streams ● In most operating systems before UNIX, programs had to be explicitly connected (by the programmer) to the appropriate input and output devices ● One of the advances introduced by UNIX were abstract devices, which eliminated the need for the program to know where data was coming from (or going into) ● UNIX also implemented automatic connection of each running program to the standard data streams (which tie the program to actual physical devices) ● Standard input (stdin): where data comes from to enter the program ● Standard output (stdout): where data goes when it gets out of the program ● Standard error (stderr): where program errors (or warnings or diagnostic messages) go to when issued J.M.P. Alves 18 / 60 BMP0260 / ICB5765 / IBI5765 Standard streams ● Some programs do not require standard input, e.g., ls, pwd ● Some others do not require standard output, e.g., mkdir, cd ● Standard input is represented by number 0 ● Standard output is represented by number 1 ● Standard error is represented by number 2 J.M.P. Alves 19 / 60 BMP0260 / ICB5765 / IBI5765 Standard streams ● By default: ● Standard input (stdin): keyboard ● Standard output (stdout): screen Text terminal ● Standard error (stderr): screen #0 stdin Keyboard Process #1 stdout #2 stderr Display J.M.P. Alves 20 / 60 BMP0260 / ICB5765 / IBI5765 Standard streams ● If you were in class previously, you must have a program called average whose file has been placed in your $HOME/bin directory ● If you don't have that file, copy it from ~dummy/bin/ to your home directory ● The average program accepts data from the standard input and sends its output to standard output ● Start the program (remember: if it's not in a directory from your $PATH, you must give the relative or absolute path to be able to run it! Also make sure your copy of the program has execute permissions for your user) average (or ./average or bin/average etc.) ● Notice that the program started, and it is now waiting… for data! J.M.P. Alves 21 / 60 BMP0260 / ICB5765 / IBI5765 ● Data is coming from the standard input, which is the keyboard, by default ● So, type a number, and then ENTER ● Keep doing that until you are done ● To signal the end of “file” (after all, STDIN is a “virtual text file”), press Ctrl+d by itself, at the start of a new line ● Since the average program waits for the whole input before performing any calculation, no output appears until the end of input ● Other programs could behave differently; for example: head ● This program return the top 10 (by default) lines of a file. Try it with STDIN! Enter lines until the program exits J.M.P. Alves 22 / 60 BMP0260 / ICB5765 / IBI5765 Standard streams ● Just by existing, standard streams are already very useful ● But the capability of redirection makes them even more versatile ● That way, data can come from (or go to) different places than just the keyboard (or the screen) ● Redirection can be done between a program and input and output files or between different programs J.M.P.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    60 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us