Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.

Slides:



Advertisements
Similar presentations
Pre-Assessment Questions
Advertisements

The Unix File System. What are the three parts of every file on a Unix filesystem? And where is each stored? Filename - stored in directories Inode -
Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");
UNIX file systems Learning Objectives: 1. To understand the basics of file systems 2. To understand the hierarchical structure in Unix file system 3. To.
6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
Perl File and Directory Access Software Tools. Slide 2 Changing Directories You can change the current working directory within Perl, just like the cd.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
The UNIX File System CS465. File Systems What is a file system? A means of organizing information on the computer. A file system is a logical view, not.
File Security. Viewing Permissions ls –l Permission Values.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Linux+ Guide to Linux Certification, Second Edition
CS 497C – Introduction to UNIX Lecture 12: - The File System Chin-Chih Chang
Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
Links Learning Objectives: 1. To understand the basics of links & its usage 2. To learn the construction / removal of different types of links 3. To distinguish.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Linux Linux File System.
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
UNIX Files and Security Software Tools. Slide 2 File Systems l What is a file system? A means of organizing information on the computer. A file system.
Links Software Tools. Lecture 4 / Slide 2 Links l A link is a pointer to a file. l In fact, in UNIX all filenames are just links to a file. Most files.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Learning basic Unix command IT 325 operating system.
COMP1070/2002/lec4/H.Melikian COMP1070 Lecture #5  Files and directories in UNIX  Various types of files  File attributes  Notion of pathname  Commands.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
The file structure and related utilities CS240 Computer Science II.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
CHAPTER 1 UNIX FOR NONPROGRAMMERS By U ğ ur Halıcı.
Linux Directory Navigation. File & Directory Commands This is a minimal list of Unix commands that you must know for file management: ls (list) mkdir.
2 Manual & Filestore. Introduction Using the manual The UNIX filestore File permissions.
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
LIN Unix Lecture 3 Hana Filip. LIN UNIX Resources UNIX Tutorials UNIX help for.
Linux+ Guide to Linux Certification, Second Edition
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
File Security and Permissions. File Permissions (1) u With respect to a particular file, Unix divides the set of all users on a system into three categories:
Chapter 4: File Security & Permissions Also: Hard and Soft Links, see p77-80 &
E X C E E D I N G E X P E C T A T I O N S Basic LINUX Linux System Administration Dr. Hoganson Kennesaw State University Operating Systems Directory structure:
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Using Linux Commands Lab 4. Using the Shell in Linux Commands Syntax  Options: could be added to the commands to change their behavior (-a, -la, --help)
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
Revision: Absolute and relative paths. (root) staffusrbinstudetc ResearchTeachingPrivate pgugitmasters xxxgtrxxx CUA Coursework1.txt CUA xxx02uxxx04u.
2 Manual & Filestore Mauro Jaskelioff. Introduction Using the manual The UNIX filestore File permissions.
1 Lecture 2 Working with Files and Directories COP 3353 Introduction to UNIX.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Directories.
File System Security ls -l. First Columm d = directory l = symbolic link b = block special file c = character special file p = fifo (or named pipe) special.
1 Introduction to Unix. 2 What is UNIX?  UNIX is an Operating System (OS).  An operating system is a control program that helps the user communicate.
1 File Permissions Look at permissions with ls -l Output: –-rw-r--r-- 1 enda users 1234 Jun 2 10:51 file.ext.
UNIX file systems Learning Objectives: 1. To understand the basics of file systems 2. To understand the hierarchical structure in Unix file system 3. To.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
Learning basic Unix command It 325 operating system.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Linux Filesystem Management
Tutorial of Unix Command & shell scriptS 5027
Commands Basic syntax of shell commands UNIX or shell commands have a basic structure command -options target command comes first (such as cd or ls) any.
Lecture 2 Working with Files and Directories
Some Linux Commands.
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Using Linux Commands Lab 3.
Tutorial of Unix Command & shell scriptS 5027
Module 6 Working with Files and Directories
Lab 2: Terminal Basics.
January 26th, 2004 Class Meeting 2
Presentation transcript:

Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command involving file system management / accessing

COMP111 Lecture 20 / Slide 2 Perl File and Directory Access Table of Content  Changing Directories  Globbing  Removing a File  Renaming a File  Hard Links  Soft Link  Making and Removing Directories  Permissions  Modifying Permissions  Recursive Directory Processing

COMP111 Lecture 20 / Slide 3  You can change the current working directory within Perl, just like the cd shell command.  In Perl, the chdir function, takes a single argument -- the directory name to change to.  chdir returns true if you ’ ve successfully changed to the requested directory, and false if you could not. chdir("/etc") || die "cannot cd to /etc"; Changing Directories (1)

COMP111 Lecture 20 / Slide 4 Changing Directories (2)  Like other functions, parentheses are optional: $ cat cd1 #!/usr/local/bin/perl5 -w print "where to go? "; chomp($where = ); if(chdir $where){ print "chdir succeeded, now in $where\n"; }else{ print "chdir did not succeeded\n"; } $ cd1 where to go? /bin chdir succeeded, now in /bin $

COMP111 Lecture 20 / Slide 5 Changing Directories (3)  When your Perl program starts, the current working directory will be inherited from the shell that invoked it.  However, using chdir in Perl will not change the working directory of the shell when your program ends.  The chdir function without a parameter defaults to taking you to your home directory.

COMP111 Lecture 20 / Slide 6 Globbing (1)  The shell takes a * and turns it into a list of all the filenames in the current directory.  Similarly, [a-m]*.cpp turns into a list of all filenames in the current directory that begin with a letter in the first half of the alphabet, and end in.cpp.  The expansion of * or /etc/host* into the list of matching filenames is called globbing.

COMP111 Lecture 20 / Slide 7 Globbing (2)  Perl supports globbing.  Just put the globbing pattern between angle brackets or use the glob = = = glob("[a-m]*.cpp");# same  In a list context, the glob returns a list of all names that match the pattern (or an empty list if none match).

COMP111 Lecture 20 / Slide 8 Globbing (3)  Example: $ ls addressletter1namessort $ cat glob1 #!/usr/local/bin/perl5 = ; foreach $file print "$file\n"; } $ glob1 address letter1 $

COMP111 Lecture 20 / Slide 9 Globbing (4)  If you use a full pathname in your glob, you will get full pathnames as a result: $ cat glob2 #!/usr/local/bin/perl5 = ; foreach $file print "$file "; } print "\n"; $ glob2 /etc/hostname.le0 /etc/hosts /etc/hosts.allow /etc/hosts.deny $

COMP111 Lecture 20 / Slide 10 Globbing (5)  If you want just the simple filename, you can use substitute to chop off the directory part of the string: $ cat glob3 #!/usr/local/bin/perl5 = ; foreach $file $file =~ s#.*/##;# delete to last slash print "$file "; } print "\n"; $ glob3 hostname.le0 hosts hosts.allow hosts.deny $

COMP111 Lecture 20 / Slide 11 Globbing (6)  Multiple patterns are permitted inside the = ;  The argument to glob is variable interpolated: $ cat glob4 #!/usr/local/bin/perl5 -w if(-d "/homes/horner/111"){ $where = "/homes/horner/111"; }else{ $where = "/homes/horner"; = ; print $ glob4 /homes/horner/111/letter1 /homes/horner/111/names

COMP111 Lecture 20 / Slide 12 Removing a File (1)  The Perl unlink function deletes a file, exactly like the UNIX rm command. unlink("bill");# bye bye bill  Example: $ ls letter1namesunlink1 $ cat unlink1 #!/usr/local/bin/perl5 -w print "what file to delete? "; chomp($what = ); unlink($what); $ unlink1 what file to delete? letter1 $ ls namesunlink1 $

COMP111 Lecture 20 / Slide 13 Removing a File (2)  unlink can take a list of names as well: unlink("bill", "gates"); unlink ;# delete all.cpp files  The return value on unlink is the number of files successfully deleted. foreach $file ( ){ if(unlink($file) == 0){ print "trouble deleting $file\n"; }

COMP111 Lecture 20 / Slide 14 Removing a File (3)  With no arguments to unlink, $_ is used as the default. foreach ( ){ if(unlink == 0){ print "trouble deleting $_\n"; }

COMP111 Lecture 20 / Slide 15 Renaming a File  The Perl function rename allows you to rename files.  Here is how to rename the file gates into cheap : rename("gates", "cheap");  rename returns a true value if successful. if(rename("gates", "cheap")){ print "Bill is now cheap\n"; }else{ print "Bill is still gates\n"; }

COMP111 Lecture 20 / Slide 16 Hard Links  The Perl function link allows you to create a hard link.  Here is how to link from the file gates to cheap : link("gates", "cheap");  link returns true if successful. if(link("gates", "cheap")){ print "Bill is now also cheap\n"; }else{ print "Bill is still only gates\n"; }

COMP111 Lecture 20 / Slide 17 Soft Links  The Perl function symlink allows you to create a soft (symbolic) link.  Here is how to symbolic link from gates to cheap : symlink("gates", "cheap");  readlink returns the name pointed at by the specified symbolic link: symlink("gates", "cheap"); $x = readlink("cheap"); print "cheap points at $x\n";#cheap points at gates

COMP111 Lecture 20 / Slide 18 Making and Removing Directories  The Perl functions mkdir and rmdir allow you to make and remove directories.  mkdir takes the name of the new directory and a mode that determines the permissions  Use 0755 for the mode to allow user full (rwx) permission, and no write permission for group and other (rx only). mkdir("gatesdir", 0755); rmdir("gatesdir");

COMP111 Lecture 20 / Slide 19  The leading mode number is always 0, and the other 3 numbers are permissions for user, group, and other respectively.  The mode values are octal, and have the following meanings: x 2 -w- 3 -wx 4 r-- 5 r-x 6 rw- 7 rwx Permissions

COMP111 Lecture 20 / Slide 20  The Perl function chmod allows you to change file and directory permissions: mkdir("gatesdir", 0755); #u:rwx g:rx o:rx chmod(0750, "gatesdir"); #u:rwx g:rx o: chmod(0531, "gates"); #u:rx g:wx o:x chmod(0642, "gates", "cheap"); #u:rw g:r o:w Modifying Permissions

COMP111 Lecture 20 / Slide 21  Here is the skeleton of a program for recursive directory processing: #!/usr/local/bin/perl5 -w sub dirtree { = ; # local variable required print "Directory listing for: $_[0]\n"; foreach print "$_\n"; dirtree($_) if (-d $_); # recursion } dirtree($ARGV[0]); Recursive Directory Processing