File / Directory Manipulation

ACSC 372 – Systems Programming  Creating Directories  Command mkdir

 Removing Directories

 Command rmdir 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  usage: mv  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  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 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 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  Print out the first few lines of a text file #include  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  2 #include Command cat (options -s, -n) 3 4 int main()  Print out an entire file to the screen 5 { 6  usage: cat 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 …  usage: more  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

#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 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]  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  Χρήση : uniq [επιλογές ] Nick 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. the file must exist.  Input from the keyboard >  redirect the output to a file  stdout if the file does not exist, it is created,  The default stream for sending output, when a program otherwise it is overwritten. runs. >>  redirect the output to a file – append all  Output to the screen output to a file  stderr create a file if it doesn’t exist,  The default stream for sending error messages output but won’t overwrite any information currently in when the program runs. the file  Error messages that show up on the screen  Allows for the error messages to be separated from normal output

29 30

File Redirection (cntd) File Redirection (cntd)

 Is redirection useful ? YES bash-3.1$ ls test/ /bin/DoesNotExist  Whenever you want to save the output of a ls: /bin/DoesNotExist: No such file or directory program test/:  Whenever you want to avoid typing input HardLinkToFile1.txt SymbLinkToFile2.txt test1 test2 continually bash-3.1$ ls test/ /bin/DoesNotExist 2> test/ErrorOutput test/:  Whenever you want to isolate any error ErrorOutput HardLinkToFile1.txt SymbLinkToFile2.txt messages. test1 test2 bash-3.1$ cat test/ErrorOutput ls: /bin/DoesNotExist: No such file or directory

 Note on Redirection: Redirect stderr to a file 0 stdin ./foo < InputFile 1 >> OutputFile 2 >> ErrorFile 1 stdout 2 stderr

31 32 Pipes Pipes (cntd)

bash-3.1$ cat test-uniq.test Dan  pipe allows the connection of the output of a Dan program into the input of another program. Dan Athena  This is achieved with the use of a vertical bar | Ryu Ryu  Examples: Athena Nick Sort the file and then remove the adjacent duplicated lines bash-3.1$ ls test bash-3.1 $ sort test-uniq.test | uniq HardLinkToFile1.txt SymbLinkToFile2.txt test1 test2 Athena bash-3.1$ ls test | wc Dan 4 4 52 Nick Ryu Report the number of lines, words, and characters of the content of the directory test bash-3.1$ sort test-uniq.test | uniq -c 2 Athena 3 Dan 1 Nick 2 Ryu

33 34

Pipes (cntd) File Redirection and Pipes

 You can have more than one pipe per line  We can have both file redirection as well as  Always read left to right piping. output flows left to right

bash-3.1$ sort < test-uniq.test 2> ErrorOutput | uniq 1> Output bash-3.1$ sort test-uniq.test | uniq | wc bash-3.1$ cat ErrorOutput bash-3.1$ cat Output 4 4 20 Athena Dan Nick Sort the file, then remove the adjacent duplicated lines, and finally report Ryu the number of lines, words, and characters

35 36