CS 2505 Fall 2018 Linux02 Basic Commands ------------------------------------------------------------------------------- For questions 1-5, suppose that your userid is cs2505 and that a listing (ls -l) of the contents of your current working directory provides the following information: cs2505@beech submissions]$ ls -l total 900 -rw-rw---- 1 cs2505 cs2505 16407 Aug 29 2018 aac7594.txt -rw-rw---- 1 cs2505 cs2505 8988 Aug 29 2018 abhir.2.txt -rw-rw---- 1 cs2505 cs2505 6239 Aug 29 2018 ajbarnes.txt -rw-rw---- 1 cs2505 cs2505 10746 Aug 29 2018 alamon.txt -rw-rw---- 1 cs2505 cs2505 4097 Aug 29 2018 alek6.txt -rw-rw---- 1 cs2505 cs2505 6413 Aug 29 2018 amm28053 -rw-rw---- 1 cs2505 cs2505 7744 Aug 29 2018 andysin.txt -rw-rw---- 1 cs2505 cs2505 1630 Aug 29 2018 apeace.txt -rw-rw---- 1 cs2505 cs2505 11402 Aug 29 2018 arbid17.docx -rw-rw---- 1 cs2505 cs2505 10715 Aug 29 2018 arman1.txt -rw-rw---- 1 cs2505 cs2505 2191 Aug 29 2018 atg115.txt -rw-rw---- 1 cs2505 cs2505 7633 Aug 29 2018 benw94.txt -rw-rw---- 1 cs2505 cs2505 5194 Aug 29 2018 bir1997.ascii -rw-rw---- 1 cs2505 cs2505 4287 Aug 29 2018 bousquet.txt . . . -rw-rw---- 1 cs2505 cs2505 6179 Aug 29 2018 willpr13.txt -rw-rw---- 1 cs2505 cs2505 5110 Aug 29 2018 wjenny24 -rw-rw---- 1 cs2505 cs2505 3089 Aug 29 2018 wryan8.txt -rw-rw---- 1 cs2505 cs2505 6769 Aug 29 2018 zakkl13.txt -rw-rw---- 1 cs2505 cs2505 7629 Aug 29 2018 zc219.txt -rw-rw---- 1 cs2505 cs2505 4270 Aug 29 2018 zchryb.txt The directory contains hundreds of files, and you do not know all the file names. You do know that most, but not all, of the file names end with the extension ".txt" (because some of the students who submitted them failed to understand the importance of using proper extensions). As you might guess, the first part of each file name is a PID. You may benefit from consulting the man page for the tar command. 1. [18 points] Write a single Linux command that could be executed to display the names of the files described. a) All the files, if any, whose name ends with the extension ".text". ANSWER: ls *.text b) All the files, if any, whose name begins with the letter 'r'. ANSWER: ls r* c) All the files, if any, whose name ends with the letter 'c', possibly followed by an extension. ANSWER: This is tricky. We have two cases. If there an extension, this will pick up the file: ls *c.* That's the easy part. If there's no extension, you might try: ls *c But this will pick up files like hoohah.doc, which we do not want. Here's my solution using ls: ls @(*c.*|!(*[!c].*)c) OK, this was a stretch. I'm using the extended globbing feature that the bash shell supports: @() means look for matches to one of the patterns given here The first pattern: *c.* matches any file whose name ends with c, followed by any extension at all The second pattern: !() means exclude matches to what follows in parentheses (*[!c].*)c means anything followed by a non-c followed by any extension that ends in c (so this picks up cases where there is a c at the end of the extension, but no c at the end of the file name) If you didn't research things deeply enough to discover globbing, you can also solve the problem by using the find command. 2. [12 points] Suppose your current working directory is the one shown above, and that you are the user cs2505. For each part, give a single valid Linux command that will create the specified tar archive. a) Bundle all the files in the current working directory that have the extension ".txt" into a flat archive named allFiles.tar, so that the archive is in your home directory. ANSWER: tar cf ~/allFiles.tar *.txt The "~/" specifies that allFiles.tar is supposed to be in the user's home directory. The only issue with my answer is that if there was a DIRECTORY in the current working directory, named something like foo.txt, then that directory would be included and my tar file would not be flat. If I were worried about that, I would add --no-recursion. b) Bundle all the files in the current working directory that were submitted by students whose PID began with the characters "li" into a flat archive named li_s.tar, so that the archive is in the parent of the current directory. ANSWER: tar cf ../li_s.tar li* 3. [12 points] Under the same assumptions as in question 2, give a single valid Linux command that will create the specified zip archive. a) Bundle all the files in the current working directory that have the extension ".txt" into a flat archive named allFiles.zip, so that the archive is in the parent of the current working directory. ANSWER: zip ../allFiles.zip *.txt b) Bundle the current working directory into an archive named submissions.zip, so that the archive is in the parent of the current working directory. (This archive will not be flat, and unpacking it would create a directory named submissions, which would contain (copies of) all the files in the current working directory.) ANSWER: zip -r ../submissions.zip ../submissions/ To include directory structure, you want to specify the directory name, rather than the files that are in it. But, by default, zip doesn't process directories recursively, so I need to use -r or zip won't actually pick up the files in ../submissions. 4. [12 points] The user cs2505 executes the following command in the parent of the working directory shown above: ls --format=single-column ./submissions/*5.txt The following output is produced: submissions/atg115.txt submissions/gceric5.txt submissions/jose95.txt submissions/leo1995.txt submissions/nickf15.txt submissions/seth15.txt submissions/unicho5.txt submissions/vinh0925.txt a) Describe the output that the following command would produce: ls --format=single-column ./submissions/*5.txt | wc You actually have enough information to show the precise output, but you must describe it logically. ANSWER: The '|' chains the commands, sending the output from the ls command as input to the wc command. The wc command counts how many lines, words, and characters are in the output from ls (not how many lines, words, and characters are in the files). b) Describe the output that the following command would produce: wc `ls --format=single-column ./submissions/*5.txt` You do not have enough information to show the output, so you must describe it logically. Those funny quote marks in this command are called backticks, and they are important. You might want to look that up. ANSWER: The backticks cause the output produced by the ls command to be substituted in the larger command. So, this is equivalent to running wc and giving a list of the file names, with paths, that are shown in the display above. wc will then be run on each of those files, and will produce a line/word/character count for each file. 5. [8 points] The user arbid17 has submitted a file shown in the listing given above; unfortunately, his file was not a plain ASCII text file, as required for the course. He attempts to fix the problem by resubmitting the same file, but renaming it arbid17.txt. Explain clearly why this will, or will not, fix the problem. ANSWER: Changing the file extension does not change anything about the contents of the file, so it will still not be plain text. 6. [10 points] Suppose a user executes the following command on a Linux system: find ~ -maxdepth 2 -type f -name "*.docx" What is the overall purpose of running this command, or in other words, why would a user wish to run it? Be very precise and complete. You may wish to examine the man page for the find command, and you may also want to experiment. ANSWER: The find command searches a specified directory tree and reports the names (and paths) of every file in that directory tree that matches the specified criterial In this case, the user is asking find to start in her home directory (~), look no more than 2 levels deep, look for regular files (-type f), and for files whose name has the extension ".docx". So, she is looking for apparent Word document files that are either in her home directory, or in an immediate subdirectory of her home directory. For question 7, suppose you are in the directory J3Files, which has the following contents, including a directory tree rooted in CS3114: J3Files |-- CS3114 | `-- J3 | |-- Client | | `-- Point.java | `-- DS | |-- Compare2D.java | |-- Direction.java | |-- Lewis.class | `-- prQuadTree.java |-- gradeJ3.sh |-- LogComparator.jar |-- PRGenerator.jar `-- testDriver.java 7. [28 points] For each part, write a single Linux command that will achieve the specified action, with no unnecessary side-effects, assuming the command will be executed in the directory J3Files. The parts are independent; in other words, each will be performed with the system in the state shown above. You will find a number of man pages to be very useful. There will be no partial credit for these answers. a) Copy the file Direction.java to the user's bin directory, created in the previous Linux assignment. ANSWER: cp CS3114/J3/DS/Direction.java ~/bin/ b) List the files in the subdirectory DS, sorted in ascending order by file size. ANSWER: ls -S -r CS3114/J3/DS/ The -S switch (capital S) specifies to sort by file size, in descending order. The -r switch specifies to reverse the sort order. You could combine them as -Sr, and you could specify the switches in either order. c) Copy the entire directory tree rooted in CS3114, including all the files therein, to the user's bin directory. ANSWER: cp -r ./CS3114/ ~/bin/ The "./" is not necessary, but I prefer to be explicit. The trailing slashes on directory names are also not required. d) Move the directory DS, and all its contents, to the directory J3Files. ANSWER: mv ./CS3114/DS/ ./ e) Remove the directory tree rooted in J3, including the directory J3 itself. ANSWER: rm -R ./CS3114/J3/ The rmdir command will not remove a nonempty directory. The switch -R, or -r, causes the rm command to remove directories and their contents, recursively. f) Rename the directory DS to be called Structures. ANSWER: mv ./CS3114/J3/DS/ ./CS3114/J3/Structures g) Display the name of every Java source code file in the directory tree rooted in CS3114, assuming that the files are named using proper extensions. ANSWER: find ./CS3114/ -name "*.java"