linux - 如何使用 bash 命令将文件名列表写入 .lst 文件

标签 linux bash php

我是 linux 的新手并尝试在我尝试编写的 PHP 脚本中使用 bash 命令,我需要从我正在使用 DirectoryIterator () 迭代的目录中写入每个 PDF 文件名在 PHP 中添加到我位于另一个文件夹中的 .lst 文件,并将每个文件名列在文本文件的新行中。

.lst 文件应如下所示:

1.pdf
2.pdf
3.pdf
...

我希望这是有道理的。任何帮助/指导将不胜感激。

这是我到目前为止想出的代码:

// Use the command line cp to copy each PDF file in the sourceDir to the destinationDir.
foreach ($srcDir as $entity) {

    /**
     * @var DirectoryIterator $entity
     */
    /*$result = rename($sourceDir . '/' . $entity->getFilename(), $destinationDir .'/' . $entity->getFileName());
    if(!$result)
    {
        throw Exception('Could not copy file ' . $entity->getFilename() . 'to destination directory ');
    }*/

    $cpString = 'cp ' . $sourceDir . '/' . $entity->getFilename() . ' ' . $destinationDir .'/' . $entity->getFileName() . ' 2<&1';
    passthru($cpString, $returnVar);

    if($entity->isFile() && strtoupper($entity->getExtension()) == 'PDF')
    {
        $cpString = 'cp ' . $sourceDir . '/' . $entity->getFilename() . ' ' . $destinationDir .'/' . $entity->getFileName();
        if ($counter = 1) {
            $catString = 'cat ' . $destinationDir . '/' . $batchNum . '.lst';
        }
        $cpString = ''
    }
    $counter++;
}

最佳答案

$fh = fopen("foo.lst", "w");

foreach ($srcDir as $entity)
    fwrite($fh, $entity->getFilename() . "\n");

fclose($fh);

关于linux - 如何使用 bash 命令将文件名列表写入 .lst 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12504152/

相关文章:

linux - bash 脚本 : ssh create file; sleep 3m; rm file;

php - 谷歌索引同一页面的两个 URL - 有和没有 index.php

php - 如何检查用户输入的 IP 地址是否有效?

linux - 如何检查 CUDA 内核中的 malloc() 故障?

python - 从 Linux 任务栏隐藏窗口

linux - 如何使用fw1-loggrabber客户端

linux - 基于 Bash 的热重载实现

bash - `ulimit -t` 在 shell 和操作系统之间完全不可移植?

bash - 从 bash shell 脚本连接到 mongoDB

php - 如何将列表回显到文本区域?