0

I have a huge file's list (more than 48k files with paths) and I wanna to do a symlink for these files

Here is my PHP code:

$files=explode("\n","files.txt");
foreach($files as $file){
$file=trim($file);
@symlink($file,"/home/".$file."@".rand(1,80000).".txt");
}

The problem is the process takes more than 1 hour

I thought about checking if the file exists first and then do a symlink, so I made some research in php.net and there some functions like is_link() and readlink() for what I wanted in the first place, but a comment took my attention:

It is neccesary to be notified that readlink only works for a real link file, if this link file locates to a directory, you cannot detect and read its contents, the is_link() function will always return false and readlink() will display a warning is you try to do.

So I made this new code:

$files=explode("\n","files.txt");
foreach($files as $file){
    $file=trim($file);
    if(!empty(readlink($file))){
        @symlink($file,"/home/".$file."@".rand(1,80000).".txt");
        }
}

The problem now : "there is no symlink files !"

How I can prevent this problems ? Should I use a multi threading or there is another option

1 Answers1

1

Obviously you are running Linux-based operating system and your question is related to File system.

In this case I would recommend to create bash script to read the file.txt and create the symlinks for all of them.

Good start to this is:

So you may try something like this:

#!/bin/bash
while read name
do
# Do what you want to $name
ln -s /full/path/to/the/file/$name /path/to/symlink/shuf -i 1-80000 -n 1$name'.txt'
done < file.txt

EDIT:
One line solution:

while read name; do ln -s /full/path/to/the/file/$name /path/to/symlink/shuf -i 1-80000 -n 1$name'.txt'; done < file.txt

Note: Replace the "file.txt" with full path to the file. And test it on small amount of files if anything goes wrong.

ino
  • 2,345
  • 1
  • 15
  • 27
  • Here I am not able to post the code properly, the `shuf -i 1-80000 -n 1` is in backtick "`" – ino Dec 29 '17 at 18:14
  • Is there any chance to make it as a Linux command without using a bash script ? – Learning Team Dec 29 '17 at 18:35
  • @x00xTeam I have updated the solution. I must say I thought it is one runtime solution not for repetitive usage ;) – ino Dec 29 '17 at 18:42
  • Can you just give me the right command of checking and symlinking. I don't need from it to get paths and files from my file I need just the command – Learning Team Dec 29 '17 at 19:52
  • You already have the right command, it is `symlink`. Just catch its return code which is TRUE on success or FALSE on failure. Actually now you are ignoring and even more ignoring the error messages using `@`. – ino Dec 30 '17 at 07:07