简体   繁体   English

使用Bash列出具有完整路径的当前目录中的文件

[英]List files in current directory with full path using Bash

Situation 情况

I have the following Structure: 我有以下结构:

+ Folder 1
--20140410.txt
--20140409.txt
--20140408.txt
--20140407.txt
--20140406.txt
--20140405.txt
--20140404.txt

+ Folder 2
--20140410.txt
--20140409.txt
--20140408.txt
--20140407.txt
--20140406.txt
--20140405.txt
--20140404.txt

I need the "newest" (depending on the file name) 5 Files from that directory, including Path. 我需要“最新”(取决于文件名)该目录中的5个文件,包括Path。 Currently i'm using the following code to do so: 目前,我正在使用以下代码来做到这一点:

for i in `find /mydirectory/ -type d` ; do cd $i && ls *.* | sort -n -r | head -5 >> /mydirectory/myfile.txt ; done

So for every directory I find, I list the files, inverse-sort them by name and cut after line 5. 因此,对于找到的每个目录,我都会列出文件,然后按名称对它们进行反排序,并在第5行后剪切。

Here's the result: 结果如下:

20140410.txt
20140409.txt
20140408.txt
20140407.txt
20140406.txt
20140410.txt
20140409.txt
20140408.txt
20140407.txt
20140406.txt

How do i write the current working directory "before" the file name? 如何在文件名“之前”写入当前工作目录?

代替解析ls ,再次使用find

for i in $(find /mydirectory/ -type d); do cd $i && find $PWD -type f -name "*.*" | sort -nr | head -5 >> /mydirectory/myfile.txt; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索目录中的所有文件,并使用bash脚本获取其完整路径 - search all files in a directory and get their full path using bash script 获取当前目录或文件夹名称(没有完整路径) - Get current directory or folder name (without the full path) 使用 Bash 将给定当前目录的绝对路径转换为相对路径 - Convert absolute path into relative path given a current directory using Bash 查找目录的完整路径(如果存在)并使用bash脚本存储在变量中 - Finding the full path of a directory if it exists and store in a variable using bash script bash完成完整路径文件 - bash completion of full path files 如何在bash函数中使用正则表达式查找当前目录路径的一部分 - how to find a section of current directory path using regex in a bash function 仅当Linux bash中存在子文件夹时,才使用文件名获取当前目录(不是完整路径) - Get current directory (not full path) with filename only when sub folder is present in Linux bash 如何使用bash脚本重命名当前目录及其子目录中的文件? - How to rename files in current directory and its subdirectories using bash script? bash脚本列出目录中的文件 - bash scripts list files in a directory bash更新文件名的完整路径列表 - bash update file list of filenames with full path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM