ACSC 372 – Systems Programming
Total Page:16
File Type:pdf, Size:1020Kb
File / Directory Manipulation ACSC 372 – Systems Programming Creating Directories Command mkdir <directory> Removing Directories Command rmdir <directory> Lecture 3: Introductory UNIX commands II Removes an empty directory 2 File / Directory Manipulation (cntd) File / Directory Manipulation (cntd) Copying Files Moving Files Command cp (options -i, -r) Command mv (option -i) Makes a copy of a file Moves a file from one location to a new location usage: cp <Source> <Destination> usage: mv <Source> <Destination> Option -i Option -i Asks if you want to write over a file that already exists Asks if you want to write over a file that already exists Option -r Recursively, will copy all files and subdirectories Used also as a rename command 3 4 File / Directory Manipulation (cntd) Examining Files Removing Files Command rm (options -i, -f, -r) Everything in UNIX is a file Removes a file Different files have different uses usage: rm <filename(s)> How do you tell what type a file is? Option -i Command file Verify the delete Examine file types Option -f Force the removal of a file Not 100% accurate. Overrides the -i flag It is based on a combination of the permission codes of the file, the name of the file, and an analysis of the first lines of option -r the file. Recursively, delete all files and directories 5 6 Examining Files (cntd) Examining Files (cntd) Examination Mechanism bash-3.1$ file test/ http://en.wikipedia.org/wiki/Magic_number_(programming) Compiled Java class files (bytecode ) start with 0xCAFEBABE on big-endian systems. test/: directory GIF image files have the ASCII code for 'GIF89a' (0x474946383961) or 'GIF87a' (0x474946383761) JPEG image files begin with 0xFFD8FF, and JPEG/ JFIF files contain the ASCII code for 'JFIF' (0x4A464946) or JPEG/ EXIF files contain the ASCII code for 'Exif' (0x45786966) beginning at byte 6 in the file, followed by more metadata about the file. bash-3.1$ file test/test1/test1.txt PNG image files begin with an 8-byte signature which identifies the file as a PNG file and allows immediate detection of some common file-transfer problems: \211 P N G \r \n \032 \n (0x89504e470d0a1a0a) test/test1/test1.txt: ASCII text Standard MIDI music files have the ASCII code for 'MThd' (0x4D546864) followed by more metadata about the file. Unix script files usually start with a shebang , '#!' (0x2321, or 0x2123 on little-endian processors) followed by the path to an interpreter. bash-3.1$ file test.c PostScript files and programs start with '%!' (0x2521). PDF files start with '%PDF'. Old MS-DOS .exe files and the newer Microsoft Windows PE (Portable Executable) .exe files start with the ASCII string 'MZ' test.c: ASCII C program text (0x4D5A), the initials of the designer of the file format, Mark Zbikowski . The definition allows 'ZM' as well but it is quite uncommon. bash-3.1$ file tes The Berkeley Fast File System superblock format is identified as either 0x19540119 or 0x011954 depending on version; both represent the birthday of author Marshall Kirk McKusick . Executables for the Game Boy and Game Boy Advance handheld video game systems have a 48-byte or 156-byte magic tes: executable (RISC System/6000 V3.1) number, respectively, at a fixed spot in the header. This magic number encodes a bitmap of the Nintendo logo. or obj module not stripped Zip files begin with 'PK', the initials of Phil Katz , author of DOS compression utility PKZIP . Old Fat binaries (containing code for both 68K processors and PowerPC processors) on Classic Mac OS contained the ASCII code for 'Joy!' (0x4A6F7921) as a prefix. bash-3.1$ TIFF files begin with either "II" or "MM" depending on the byte order (II for Intel, or little endian, MM for Motorola, or big endian), followed by 0x2A00 or 0x002A (decimal 42 as a 2-byte integer in Intel or Motorola byte ordering). Unicode text files, encoded with UTF-8, often start with \xEF\xBB\xBF ( in a Latin-1 text editor), the UTF-8 encoding of the Byte Order Mark . If they are encoded with UTF-16 , they often start with the Byte Order Mark coded as \xFE\xFF or \xFF\xFE depending on endianness . 7 8 Examining Files (cntd) Examining Files (cntd) bash-3.1$ head -8 test-math.c Command head (option -n) #include <stdio.h> Print out the first few lines of a text file #include <math.h> 10 lines by default Provides a quick way to see if this is the file you’re looking for int main() { usage: head -n filename Print out the first n lines int a, b; bash-3.1$ 9 10 Examining Files (cntd) Examining Files (cntd) bash-3.1$ tail -7 test-math.c Command tail (option -n) Print out the last few lines of a text file c = sin(0); 10 lines by default printf("a= %d\nb= %d\nc= %f\n", a,b, c); Provides a quick way to see if this is the file you’re looking for return 0; usage: tail -n filename } Print out the last n lines bash-3.1$ 11 12 Examining Files (cntd) Examining Files (cntd) bash-3.1$ cat -ns test-math.c 1 #include <stdio.h> 2 #include <math.h> Command cat (options -s, -n) 3 4 int main() Print out an entire file to the screen 5 { 6 usage: cat <filename> 7 int a, b; 8 float c; Option -s 9 10 a = -2; suppress multiple empty lines into just one blank line 11 12 b = abs(a); Option -n 13 14 c = sin(0); Number all output lines 15 16 printf("a= %d\nb= %d\nc= %f\n", a,b, c); 17 18 return 0; 19 } bash-3.1$ 13 14 Examining Files (cntd) Examining Files (cntd) Command cat concatenate Command more (options -s, -d) Print out multiple files one right after the other Print out a file one screenful at a time cat <filename1> <filename2> … usage: more <filename> option -s If the file is very large, it will scroll off the screen too suppress multiple empty lines into just one blank line fast to read Option -d No way to read a scrolling file without stopping the Prompt the user with friendly messages at the end of program each screen. Ctrl-C will kill a running program 15 16 Examining Files (cntd) Examining Files (cntd) bash-3.1$ more -d test.c #include<stdio.h> #define TAB '\t' Advanced usage: #define SPACE ' ' #define NLN '\n' spacebar #define BACKSP '\b' Scroll to the next screen int check_char(int flug); return void main(void) Move one line { int ch1,flug=-1,counter=0,n_lines=0,total_ch=0; float average; n return printf("\nType anything you like and '@' to QUIT\n"); ch1=getchar(); Move n lines while(ch1!='@') { flug=check_char(ch1); q if (flug==1) { if (ch1==NLN) { Quit more without finishing total_ch++; n_lines++; printf("\nNumber of characters in this line = %d\n",counter); --More--(44%)[Press space to continue, 'q' to quit.] 17 18 Examining Files (cntd) Examining Files (cntd) Option -l Command wc (options -l, -w, -c) Report only the number of lines bash-3.1$ wc -l test-math.c Stands for word count 20 test-math.c Report the number of lines, words, and characters Option -w in a file Report only the number of words usage: wc <filename> bash-3.1$ wc -w test-math.c 30 test-math.c bash-3.1$ wc test-math.c Option - c 20 30 174 test-math.c Report only the number of characters bash-3.1$ wc -c test-math.c bash-3.1$ 174 test-math.c 19 20 Examining Files (cntd) Examining Files (cntd) Command sort (options -r, -n, -k) Option -r Sorting in reverse order Sort the lines of a file into alphabetical order Επιλογή -n usage: sort [options] <filename> Sorting in numerical order Useful on data files or output from programs Επιλογή -k [number] Sorting based on column #number 21 22 Examining Files (cntd) Examining Files (cntd) bash-3.1$ cat test-sort.txt bash-3.1$ sort -r test-sort.txt john 32 london peter 4 chicago jean 7 paris marco 21 rome marco 21 rome luis 1 geneva anna 18 nicosia john 32 london antonio 58 madrid jean 7 paris peter 4 chicago filip 18 brussels luis 1 geneva antonio 58 madrid filip 18 brussels anna 18 nicosia 23 24 Examining Files (cntd) Examining Files (cntd) bash-3.1$ sort -nk 2 test-sort.txt bash-3.1$ sort -k 3 test-sort.txt luis 1 geneva filip 18 brussels peter 4 chicago peter 4 chicago jean 7 paris luis 1 geneva anna 18 nicosia john 32 london filip 18 brussels antonio 58 madrid marco 21 rome anna 18 nicosia john 32 london jean 7 paris antonio 58 madrid marco 21 rome 25 26 Examining Files (cntd) Examining Files (cntd) bash-3.1$ cat test-uniq.test Dan Dan Command uniq (option -c) Dan Athena Remove duplicate copies of a line that are Ryu adjacent Ryu Athena Nick Χρήση επιλογές : uniq [ ] <filename> bash-3.1$ uniq test-uniq.test Dan Useful on files that might have repeated Athena unnecessary data Ryu Athena Option -c Nick bash-3.1$ uniq -c test-uniq.test prefix lines by the number of adjacent duplicated 3 Dan 1 Athena occurrences 2 Ryu 1 Athena 1 Nick 27 28 Input and Output Streams File Redirection Use of special operators for reading , writing stdin and appending data from/to a file. The default stream for receiving the input when a program < redirect the input from a file runs.