Working with files
More UNIX Commands Now that you can navigate directories and move files around, you need to be cat able to work with the files and directories more themselves. head tail diff file
cat - ‘concatenate’ to link together cat - ‘concatenate’ to link together
This command will let you view the Practice: contents of a file. View the contents of final.paper (in dir2). $ cat filename $ cat final.paper
more - ‘view more’ more - ‘view more’
However, on large files the contents Press the ‘spacebar’ to go forward one will fly by too quickly to read when you screen. use cat. Press the ‘b’ key to go back one screen. Press the ‘q’ key to return to the shell The command more will let you view the prompt. contents of a file, one page at a time.
$ more filename
1
more - ‘view more’ head - ‘view head of file’
Practice: Using head you can view the top ten lines in a file, which can often give you a View the contents of history.txt (in good idea of what is in the file. dir2). $ head filename $ more history.txt
tail - ‘view tail of file’ head / tail
Similar to head, tail will show you the Practice: last ten lines of a file. View the beginning and end of the file shrodinger (in dir1). $ tail filename $ head shrodinger $ tail shrodinger
diff - ‘differences’ diff - ‘differences’
As you create and edit more files, you Type diff and then the two file names. may often find that you need to compare two files to see the exact differences between them. $ diff filename1 filename2
2
diff - ‘differences’ diff - ‘differences’
The output (what you see on the screen) The output not only tells you what is is only the lines which are different different between the two files, but it also between the two files (lines which only tells you exactly what has been changed appear in one file or the other). These from file1 to file2. are indicated by the less-than and You will see something like: 8a8 greater-than symbols. The numbers refer to the lines numbers < indicates the lines from file 1 which are different in each file. > indicates the lines from file 2
diff - ‘differences’ diff - ‘differences’ Practice: a = text has been added to file2. Compare the files sonnet9 and d = text has been deleted from file2. sonnet10 (in UNIX_class/Shakespeare)
c = text has been changed in file2. $ diff sonnet9 sonnet10
diff - ‘differences’ diff - ‘differences’ Practice: You can also use diff to see which What two words have been changed in files are in one directory, but not in sonnet10? another. From rude to vile, and from bliss to rapture $ diff directory1path directory2path What two things have been added to sonnet10? $ diff ~/school/docs ~/school/documents (return) -- Shakespeare
3
file - ‘file type’ file - ‘file type’
In UNIX it is not immediately apparent $ cd ../dir2/picts what type of file you have (HTML files, ASCII text, images, etc.) $ file unixbutton.JPG unixbutton.JPG: JPEG file Type file, then the file name. However, keep in mind that sometimes $ file filename the output of file is just a ‘guess’ by the system.
file - ‘file type’ Review More UNIX Commands
Practice: cat – view the contents of a text file
In dir2: more – use to view files a screen at a time head – see the first few lines of a file What is the file type of final.paper? tail – see the last few lines of a file What is the file type of cats? diff filename1 filename2 – finds differences between two files file filename – ask UNIX to try to determine the file type
Review More UNIX Commands The End…
Next… Remember to check the man pages for each of the new grep commands as well.
4