如何使用for循环打印目录中所有文件的绝对路径?

26 浏览
0 Comments

如何使用for循环打印目录中所有文件的绝对路径?

这个问题已经有回答了:

如何基于通配符匹配,在当前文件夹和子文件夹中递归查找所有文件

我想找到当前目录中每个文件的绝对路径

现在,当我尝试打印出目录中的所有文件时,使用如下命令

#!/bin/bash
for x in *; do
        echo $x
done

我得到如下结果:

cd
file1
file1.txt
file2.txt
path
readfile
testfile

这是正确的,但是,当我通过改变原有的代码以获取路径,使代码变成如下形式:

#!/bin/bash
for x in *; do
        echo $PATH
done

我得到了如下输出:

    Files/Amazon Corretto/jdk11.0.16_9/bin:/mnt/c/Program Files/Amazon Corretto/jdk17.0.4_9/bin:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/Users/User/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/User/AppData/Roaming/npm:/mnt/c/Users/User/AppData/Local/Programs/MiKTeX/miktex/bin/x64/:/snap/bin
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/Amazon Corretto/jdk11.0.16_9/bin:/mnt/c/Program Files/Amazon Corretto/jdk17.0.4_9/bin:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/Users/User/AppData/Local/Microsoft/WindoFiles/Amazon Corretto/jdk11.0.16_9/bin:/mnt/c/Program Files/Amazon Corretto/jdk17.0.4_9/bin:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/Users/User/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/User/AppData/Roaming/npm:/mnt/c/Users/User/AppData/Local/Programs/MiKTeX/miktex/bin/x64/:/snap/bin
   Files/Amazon Corretto/jdk11.0.16_9/bin:/mnt/c/Program Files/Amazon Corretto/jdk17.0.4_9/bin:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/Users/User/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/User/AppData/Roaming/npm:/mnt/c/Users/User/AppData/Local/Programs/MiKTeX/miktex/bin/x64/:/snap/bin
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/Amazon Corretto/jdk11.0.16_9/bin:/mnt/c/Program Files/Amazon Corretto/jdk17.0.4_9/bin:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/Users/User/AppData/Local/Microsoft/WindoFiles/Amazon Corretto/jdk11.0.16_9/bin:/mnt/c/Program Files/Amazon Corretto/jdk17.0.4_9/bin:/mnt/c/ProgramData/Oracle/Java/javapath:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/nodejs/:/mnt/c/Users/User/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/User/AppData/Roaming/npm:/mnt/c/Users/User/AppData/Local/Programs/MiKTeX/miktex/bin/x64/:/snap/bin

重复了7次(这是目录中存在的文件数量)。但是,这根本不像一个绝对路径,因为输出的路径中甚至没有提到文件名。

我应该改变什么以使它工作?

admin 更改状态以发布 2023年5月21日
0
0 Comments

如何获取文件的完整路径?

#!/bin/bash
for x in *; do
        readlink -f "$x"
done

0