User Tools

Site Tools


opus:fall2012:escoute1:part1

September Lessons Learned

Entries

September 5, 2012

The UNIX Philosophy

  • Small is beautiful.
    • A similar phrase would be something like KISS, Keep It Simple Stupid. This clause of the UNIX Philosophy to me invokes consideration of whether I'm typing more than necessary or keeping it short and to the point. It's somewhat of a challenge principle, How short of a sentence does it take to explain your point?
  • Do one thing and do that one thing well.
    • A command has a very specific thing it needs to do. Take the ls command for example, it only does one thing and one thing only, it lists the contents of a directory and that is all. It won't remove anything, it won't copy, it will only do what it was meant to do.
  • Everything is a file.
    • “Everything in a UNIX system is a file. Well, except things that aren't files, such as sockets.”

We also went over file types and file paths, which is mostly covered as my keyword. There are two different file paths, an absolute path and a relative path.

An absolute path looks like this if you change directory: cd /home/name/src

  • Other examples of absolute paths are:
    • /bin (the absolute path for where the basic user commands are stored)
    • /etc (the absolute path for where configuration files are stored)
    • /dev (the absolute path for where device files(special files) are stored)
    • / (this is the root directory)

A relative path would look something like this: cd src

That relative path would only work if you were in the /home/name directory (confirming that using the pmd command)

September 7th, 2012

/lib - library files/functions. Libraries hold and reference to many smaller programs. ldd - command that lists the necessary functions to perform a command.

libc is the standard C library

/media - Directory for mounting file systems on removable media like CD-ROM drives, floppy disks, and Zip drives.

/mnt - where removable devices or temporary file systems are placed upon plug-in. Such as flash drives.

September 12, 2012

Everything(mostly) about Vi

“Vi (pronounce: “vee eye”, not “six”, not “vye”) is an editor. An editor is a program to edit files. Goodbye.”

To launch Vi, just use the vi command. Use vi with a filename after it to open a file, in Vi Two modes..

  1. Command Mode is where many of the keys are simply commands that either edit the file, without going into insert mode, navigate the cursor by jumping words or lines, and doing extra convieniences upon entering insert mode.
  2. Insert Mode is where every key on the keyboard is used to simply type up text and will operate just as if you were in WordPad in Windows. The only way out of insert mode is to hit ESC.

Here are a few commands in no special order..

  • a - append to next character into insert mode
  • A - append to end of line into insert mode
  • i - insert at the end of the line
  • I - insert at beginnning of the line
  • o - insert line below current line
  • O - insert line above current line
  • j - move cursor to the left one character
  • l - move cursor to the right one character
  • j - move cursor down one line
  • k - move cursor up one line
  • ^ - move cursor to the beginning of current line
  • $ - move cursor to the end of current line
  • w - move forward one word
  • W - move forward one word, includes spaces
  • b - move back one word
  • B - move back one word, includes spaces
  • x - deletes single character under cursor location
  • X - backspaces
  • dw - delete word to the right of the cursor
  • db - delete word to the left of the cursor
  • dd - delete line
  • cw - deletes word, enters insert mode
  • cb - delete word back, enter insert mode
  • cW - delete word forward with spaces, enter insert mode
  • cB - delete word back with spaces, enter insert mode
  • p - Paste in current line
  • P - paste under current line
  • yy - yanks line into buffer (cut)
  • yw - yank right word
  • yb - yank left word
  • ~ - toggle case, moves cursor right one
  • s - substitute a character, enter insert mode
  • S - substitute a line, enter insert mode

The colon : is extended command mode, which has some notable commands..

  • :w - write current contents
  • :w filename - write current content to a new file named filename
  • :q - quits
  • :q! - force quit
  • :w! - writes current content over preexisting file
  • :wq - save then quit if it has a filename
  • :r filename - reads the file filename
  • :12,35w smallfile - writes the contents of lines 12 to 35 to a new file named smallfile

September 19, 2012

This day we went over the archiving and compression commands known as tar, which is the archiver and gzip, the compressor. The neat thing about having two commands for this process, you have the option to do one or the other, or both. If you've seen a .zip file before, that is an extension that indicates it was compressed AND archived. That limitation is that you cannot have one or the other through the means of a Windows program like WinZip or 7Zip. tar by definition, saves many files together into a single tape or disk archive and can restore individual files from the archive. Some worthy extensions may include..

  • -c creates, when used this way: tar -c archive.tar foo bar , it will create archive.tar from files foo to bar.
  • -A appends files to an archive
  • -d deletes files in an archive
  • -r append files to the end of an archive
  • -t list contents of an archive
  • -x extract files from an archive
  • -v verbosely list files processed
  • -f file archive

gzip is the compression/decompression tool that is often used after archiving a file. Use gzip [filename] to compress your file Some worthy extensions that should be used accordingly..

  • -d decompress
  • -f force
  • -v verbose
  • -# Regulates the speed of compression with -1 being the fastest and -9 being the best.

In class we also went over the command last and eventually learned how to get personalized information through a super incantation.

The command last lists all the logins for a complete month, in the case of this day, it listed all the logins from the month of September. This will reset once it has become October. You can imagine this being a very hectic, long list but luckily we have some ways to get the information we need.

We can use a pager command such as more which will show a “page” full of lines being generated, and you press space to move onto the next page. You can also use less to list these pages in reverse (from the beginning of the month to end).

A command with the more or less commands must be an incantation through the use of the pipe | . Which would look something like this: last|more or last|less This can be useful but we want more specific information from the last command so we created a super incantation, which is the following.. echo “You logged in `last|grep $USER|wc -l` times this month” To break this down..

  • echo prints the quoted material “You logged in.. times this month”
  • The incantation within the quotes will display the number of logins that the current logged in user has done for that month.
    • last prints the list of logins
    • grep $USER searches for a pattern, in this case, it searches for the logged in username out of the last command's output
    • wc -l counts the lines or the times $USER has logged in and displays this number.

On the current day (9/30/2012), this incantation displays You logged in 28 times this month. This is really cool and all, but imagine if you had to type that every time you wanted to know how many times you've logged in? Let's get lazy!

We next learned how to make any file an executable or program. We can use the incantation we created above and just type it up into a file, or you can be even lazier, and type the incantation and direct its output into a new file by using a full quotation and adding another echo, like so: echo 'echo “You logged in `last|grep $USER|wc -l` times this month”' > login

This will display our incantation into a new file known as login or overwrite the existing file login.

And finally to make this a working program, we need to change the permissions which can be viewed using ls -l.

To change permissions, use the command chmod with the corresponding numbers 744: chmod 744 login

This will make our file a program and you can just run it using ./login assuming you are in the same folder, otherwise change the path accordingly.

Keywords

File Types

The Regular, Directory, and Special File Types

Definition

Unlike most keywords, this one isn't just one simple keyword. A file type is a vague term that represents files, and in order to really know what one is, they should be defined, SUCH AS:

  1. A regular file type: A regular file is the most common file type there is, this file type represents the text files, compressed files, binary files,and “file files.”(not a real file, I just like the word. Basically any file that you would send as an email attachment is considered a regular file, within reason.
  2. A directory file type: A directory file isn't commonly known as a file, but it is. The common folk would call this a “folder”. You put all types of files within a directory file, even another directory file. What a directory file does is points you in the direction of more files, usually more than one. A symbolic link(which is a file) would fall into this category for that reason, which could point to a specific file or a directory. (A file that points to a directory; which points to more files.)
  3. A special file type: A special file is the most uncommon file type. These files define the system devices and temporary files created by processes. There are 4 basic types of files that are considered “special” and they are FIFO (First in, first out) which are also known as pipes, which allow communication between processes temporarily, then there's block and character devices which define devices.

An important consideration when we're talking files is that each file has permissions for them that determine which users can Read, Write, or Execute a file. These are formally known as access modes. These access modes are often represented in this format: Eg. drwxrwxr– or srwxr—–

References

ls

Definition

ls is a command for directory listing. What this mean is it lists all the contents of the directory in a curt manner. Usually just the names of the directories, files, and their file types by color coordination.

  • -l changes how ls operates and causes the command to do a long/detailed listing, revealing the file type and permissions.
  • -al causes a formatted listing with hidden files.

References

  • From the LAIR computer desktop.

unix Keyword 1 Phase 2

Home Directory

Definition

The Home Directory is basically the place that all of a user's files and folders are stored. From the home directory, one may be able to access all of their files, or just access specifically places files. The user is able to completely customize their home directory. When files and folders are in a home directory (including readable, writable, and executable files), they are only able to be accessed by the user or any other administrator on the system. That can be changed at any point, however.

References

Demonstration

Demonstration of the Home Directory

Firstly, we identify where the home directory is. When you log in, you should be able to use the pwd command and boom, you it shows the absolute path of your home directory. Another way to ensure that it is YOUR home directory, you can use pwd $USER and the same result should display. Try that in another relative directory and you will get the same result every time, because $USER represents your username, which is mainly used to identify your home directory.

We can also change directory or cd to your home directory using an absolute path or a relative path if you are in the /home directory.

The home directory is the one directory where it's not recommended to even consider changing the Owner access controls.

One of the most useful things you can do related to your home directory is usage of the cd command in order to quickly return to it, from anywhere in the file system, including finding your way into another user's home directory, you can easily change back to yours with simply cd

And finally you can use ls to see what it looks like to verify that they are your directories.

Assume your username is rabidrabbit

Back to back demonstration..

lab46:~$ pwd
/home/rabidrabbit
lab46:~$ pwd $USER
/home/rabidrabbit
lab46:~$ cd /
lab46:/$ cd /home/$USER
lab46:~$ pwd
/home/rabidrabbit
lab46:~/src/submit$ cd
lab46:~$ pwd
/home/rabidrabbit
lab46:~$ cd /home/tmong
lab46:/home/tmong$ cd
lab46:~$ pwd
/home/rabidrabbit
lab46:~$ ls
Desktop    Downloads  Music     Public     Videos    closet  file.txt     src
Documents  Maildir    Pictures  Templates  archives  data    public_html

Compression Before Archiving Experiment

Question

I notice we always archive a group of files before we compress. What would happen if we compressed these files individually first, then we archived them, would it still give us the same outcome?

Resources

I'll be using the information I learned from class to generate some sort of outcome from this, using the tar and gzip commands exclusively. Most of this information can be referred to my dated entries in this Opus.

Hypothesis

I think that this will work normally. The archiving before compression feels like a good practice but doing it the other way feels taboo. At the end of this experiment, I think we will have the same files we created.

Experiment

I plan on creating a directory for this experiment, to keep all my other stuff safe. Then I'll use the touch command to create 3 files and type in some information in them. Instead of normally using tar first, I'll proceed to compress each of these files, then I'll select them all for archiving. Once that is completed, I'll proceed to reverse this process and hopefully have the same 3 files and the information within them in tact.

Data

lab46:~/closet$ mkdir thelab
lab46:~/closet$ cd thelab
lab46:~/closet/thelab$ touch file1 file2 file3
lab46:~/closet/thelab$ ls
file1  file2  file3
lab46:~/closet/thelab$ vi file1
lab46:~/closet/thelab$ vi file2
lab46:~/closet/thelab$ vi file3
lab46:~/closet/thelab$ cat file1
This is file 1
lab46:~/closet/thelab$ cat file2
This is not file 1 or file 3
lab46:~/closet/thelab$ cat file3
file 3 this is.
lab46:~/closet/thelab$ gzip file1 file2 file3
lab46:~/closet/thelab$ ls
file1.gz  file2.gz  file3.gz
lab46:~/closet/thelab$ tar cvf archive.tar file1.gz file2.gz file3.gz
file1.gz
file2.gz
file3.gz
lab46:~/closet/thelab$ tar tvf archive.tar
-rw-r--r-- escoute1/lab46   39 2012-09-30 17:17 file1.gz
-rw-r--r-- escoute1/lab46   49 2012-09-30 17:09 file2.gz
-rw-r--r-- escoute1/lab46   42 2012-09-30 17:09 file3.gz
lab46:~/closet/thelab$ rm file1.gz file2.gz file3.gz
lab46:~/closet/thelab$ ls
archive.tar
lab46:~/closet/thelab$ tar xvf archive.tar
file1.gz
file2.gz
file3.gz
lab46:~/closet/thelab$ ls
archive.tar  file1.gz  file2.gz  file3.gz
lab46:~/closet/thelab$ gzip -d file1.gz file2.gz file3.gz
lab46:~/closet/thelab$ ls
archive.tar  file1  file2  file3
lab46:~/closet/thelab$ cat file1
This is file 1
lab46:~/closet/thelab$ cat file2
This is not file 1 or file 3
lab46:~/closet/thelab$ cat file3
file 3 this is.

Analysis

Based on the data collected:

  • My hypothesis was correct that whether you compress first or archive first, it does not matter too much.
  • This method is not a lazy method and when you compress files individually, the archive will have a bunch of compressed files instead of simply all the files compressed into one .tar.gz
  • You have to be careful of your naming conventions when you create your tar file, because you can mistakenly have a tar file named as a .gz which will make things funny.

Conclusions

It does not matter which order you do archiving and compression, you will eventually end up with the same files you had before. The archive before compression method we use however is just a lot easier and I now understand why we don't compress first.

opus/fall2012/escoute1/part1.txt · Last modified: 2012/10/03 18:48 by escoute1