

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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");
- Related Questions & Answers
- Opening and Closing Files in Perl
- Reading and Writing Files in Perl
- Rename multiple files using Python
- Rename multiple files using Java
- Create, Delete and Change Directories in Perl
- Database DELETE Operation in Perl
- Display all the Files in Perl
- How to copy multiple files in PowerShell using Copy-Item?
- How to rename multiple files recursively using Python?
- How to rename multiple files in a directory in Python?
- How to Copy files or Folder without overwriting existing files?
- How to delete empty files and folders using PowerShell?
- How to delete hidden files and folders using PowerShell?
- How to copy readonly and hidden files/folders in the PowerShell?
- How to copy files into a directory in C#?
Advertisements