![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy, Rename and Delete Files in Perl
Here is the Perl example, which opens an existing file file1.txt and read it line by line and generate another copy file file2.txt.
#!/usr/bin/perl # Open file to read open(DATA1, "<file1.txt"); # Open new file to write open(DATA2, ">file2.txt"); # Copy data from one file to another. while(<DATA1>) { print DATA2 $_; } close( DATA1 ); close( DATA2 );
Renaming a file
Here is the Perl example, which shows how we can rename a file file1.txt to file2.txt. Assuming file is available in /usr/test directory.
#!/usr/bin/perl rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );
This function renames takes two arguments and it just renames the existing file.
Deleting an Existing File
Here is an example, which shows how to delete a file file1.txt using the unlink function.
#!/usr/bin/perl unlink ("/usr/test/file1.txt");
Advertisements