Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating folder using perl

Tags:

mkdir

perl

I want to create folder using perl where, in the same folder, a perl script exists. I created FolderCreator.pl which requires an input parameter of folder name.

unless(chdir($ARGV[0]){ # If the dir available change current , unless
    mkdir($ARGV[0], 0700);                             # Create a directory
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";    # Then change or stop
}

This worked fine only if we call the scipt, in the same folder where it resides. If it is called in another folder, if doesn't work.

Eg.

.../Scripts/ScriptContainFolder> perl FolderCreator.pl New
.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl New

First one is working fine but second one doesn't. Is there way create these folders?

like image 521
DarRay Avatar asked May 11 '11 05:05

DarRay


People also ask

How do I list a directory in Perl?

If you want to get content of given directory, and only it (i.e. no subdirectories), the best way is to use opendir/readdir/closedir: opendir my $dir, "/some/path" or die "Cannot open directory: $!"; my @files = readdir $dir; closedir $dir; You can also use: my @files = glob( $dir .

How do I move a folder in Perl?

If you want to move the directory ./dir1 under ./dir2 call move like this: move("./dir1", "./dir2/dir1");

How to create a new directory in Perl?

You can use mkdir function in Perl to create a new directory. You will need to have the required permission to create a directory. #!/usr/bin/perl $dir = "/tmp/perl"; # This creates perl directory in /tmp directory. mkdir($dir) or die "Couldn't create $dir directory, $!"; print "Directory created successfullyn";

How do I write to a file in Perl?

Perl | Writing to a File. A filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file. In order to write to the file, it is opened in write mode as shown below: open (FH, ‘>’, “filename.txt”); If the file is existing then it truncates the old content of file with the new content.

How to use traversing files and directories in Perl?

Traversing files and directories in Perl can also be done through File::Find module which comes with the Perl language. Find: find () function performs a depth-first search on the mentioned/defined @directories. It calls and invokes the "&wanted" function for each file or sub-directory found in that directory. find () works from top to down.

How to remove a Perl file from a directory?

You can use rmdir function to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it. #!/usr/bin/perl $dir = "/tmp/perl"; # This removes perl directory from /tmp directory.


2 Answers

You can use the FindBin module, which give us the $Bin variable. It locates the full path to the script bin directory to allow the use of paths relative to the bin directory.

use FindBin qw($Bin);

my $folder = "$Bin/$ARGV[0]";

mkdir($folder, 0700) unless(-d $folder );
chdir($folder) or die "can't chdir $folder\n";
like image 65
Miguel Prz Avatar answered Sep 24 '22 14:09

Miguel Prz


I think it works exactly as it is written, except you have a typo, namely missing a closing parenthesis around chdir.

unless(chdir($ARGV[0])) {   #fixed typo
    mkdir($ARGV[0], 0700);
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";
}

The script runs like this:

  1. If the script cant chdir to $ARGV[0] then:
  2. Make the directory $ARGV[0], with the permission mask 0700.
  3. Change the working directory to $ARGV[0] or exit the script with the error text "cant chdir..".

The starting directory for the script will be the directory it is called from, whatever that directory may be. On *nix that'll be the $ENV{PWD} variable inside your script. It will make a new folder in any folder it has permission to do so.

I think this behavior is logical, and as it should be. If you want your example to work, do this:

.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl ScriptContainFolder/New

You can also use an absolute path, such as

?> FolderCreator.pl /home/m/me/Scripts/ScriptContainFolder/New

ETA: Oh, and you should of course always, always put this in your scripts, no matter how small:

use strict;
use warnings;
like image 41
TLP Avatar answered Sep 26 '22 14:09

TLP