有没有办法用 ls + 获取路径/到/文件awk、sed、grep 或类似的工具?

发布于 2024-10-06 10:21:28 字数 845 浏览 12 评论 0原文

我想递归地搜索一个目录,并输出:

文件名 日期 路径大小

我得到了除了路径之外的所有内容...这是一个 $$$$buster....

到目前为止,这是我的命令:

ls -lThR {DIRECTORY_NAME_HERE} | awk '/^-/ {print $10 " " $6 " " $7 " " $8 " " $5}'

我希望有一种方法将该命令与: 结合使用:

find ./{DIRECTORY_NAME_HERE} -type f 

它只显示 /path/to/filename 本身...没有其他元数据据我所知。

有什么想法......希望不需要编程语言吗?

编辑:假设文件为 5 个字节,这是我正在寻找的确切输出:

myfile.txt Dec 2 10:58 /path 5

更新:这是我最终使用的命令:

find ./{DIRECTORY_NAME_HERE} -type f -ls | 
while read f1 blocks perms blocks owner group size mon day third file; 
do echo `basename $file` `ls -lrt $file | tr -s " " | cut -d" " -f6-8` `dirname $file` `ls -lrt $file | tr -s " " | cut -d" " -f-5`; done

如果有人可以改进它,那就太好了,但这可行......

I'd like to recursively search a directory, and output:

Filename Date Path Size

I got everything but Path...which is a $$$$buster....

Here's my command so far:

ls -lThR {DIRECTORY_NAME_HERE} | awk '/^-/ {print $10 " " $6 " " $7 " " $8 " " $5}'

I wish there was a way to combine that command with:

find ./{DIRECTORY_NAME_HERE} -type f 

which just shows /path/to/filename itself...no other metadata afaik.

Any ideas...hopefully without needing a programming language?

EDIT: Here's the exact output I was looking for assuming file is 5 bytes:

myfile.txt Dec 2 10:58 /path 5

UPDATE: Here's the command I wound up with:

find ./{DIRECTORY_NAME_HERE} -type f -ls | 
while read f1 blocks perms blocks owner group size mon day third file; 
do echo `basename $file` `ls -lrt $file | tr -s " " | cut -d" " -f6-8` `dirname $file` `ls -lrt $file | tr -s " " | cut -d" " -f-5`; done

If someone can improve it, that'd be great, but this works...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最偏执的依靠 2024-10-13 10:21:29

我将使用 Perl 来完成此任务:

#!/opt/local/bin/perl -w

use File::Find;
use POSIX qw(strftime);

find(\&wanted, ($DIRECTORY_NAME_HERE));
sub wanted {
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = stat;
  printf("%s %s %s %d\n", $_, 
    strftime("%b %e %H:%M %Y", localtime($mtime)),
    $File::Find::dir,
    $size);
}

I'd use Perl for this task:

#!/opt/local/bin/perl -w

use File::Find;
use POSIX qw(strftime);

find(\&wanted, ($DIRECTORY_NAME_HERE));
sub wanted {
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = stat;
  printf("%s %s %s %d\n", $_, 
    strftime("%b %e %H:%M %Y", localtime($mtime)),
    $File::Find::dir,
    $size);
}
舟遥客 2024-10-13 10:21:28

您是否尝试过 find ./delete -t​​ype f -ls (注意 -ls - 这是关键:-))?然后,您应该能够通过 awk 通过管道传输结果来过滤出您想要的字段。

编辑...
另一种方法是使用 while 循环,例如:

find ./delete -type f -ls | while read f1 blocks perms blocks owner group size mon day third file
do
    echo `basename $file` `dirname $file`
done

并将所需的位添加到其中。

Have you tried find ./delete -type f -ls (note the -ls -- that's the key :-) )? You should then be able to pipe the results through awk to filter out the fields you want.

Edit...
Another way you could do it is with a while loop, e.g.:

find ./delete -type f -ls | while read f1 blocks perms blocks owner group size mon day third file
do
    echo `basename $file` `dirname $file`
done

and add the bits you need into that.

毁梦 2024-10-13 10:21:28

您还可以使用 find 的 -printf 功能来显示所需文件的正确属性:

find {DIRECTORY_NAME_HERE} -type f -printf  '%f %Tb %Td %TH:%TM %h %s\n'

我得到如下结果:

config Nov 10 10:02 /etc/w3m 1185
mailcap Nov 10 10:02 /etc/w3m 44
hosts.allow Apr 29 05:25 /etc 580
rsyslog.conf Feb 24 10:26 /etc 1217
user-dirs.conf Apr 16 15:03 /etc/xdg 414
user-dirs.defaults Apr 16 15:03 /etc/xdg 418

You can also use the -printf feature of find to show just the right properties of a file that you want:

find {DIRECTORY_NAME_HERE} -type f -printf  '%f %Tb %Td %TH:%TM %h %s\n'

I get results like this:

config Nov 10 10:02 /etc/w3m 1185
mailcap Nov 10 10:02 /etc/w3m 44
hosts.allow Apr 29 05:25 /etc 580
rsyslog.conf Feb 24 10:26 /etc 1217
user-dirs.conf Apr 16 15:03 /etc/xdg 414
user-dirs.defaults Apr 16 15:03 /etc/xdg 418
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文