Linux combine sort files by date created and given

2019-07-21 00:45发布

I need to combine these to commands in order to have a sorted list by date created with the specified "filename".

I know that sorting files by date can be achieved with:

ls -lrt

and finding a file by name with

find . -name "filename*"

I don't know how to combine these two. I tried with a pipeline but I don't get the right result.

[EDIT] Not sorted enter image description here

4条回答
该账号已被封号
2楼-- · 2019-07-21 01:10

My best guess would be to use xargs:

find . -name 'filename*' -print0 | xargs -0 /bin/ls -ltr

There's an upper limit on the number of arguments, but it shouldn't be a problem unless they occupy more than 32kB (read more here), in which case you will get blocks of sorted files :)

查看更多
迷人小祖宗
3楼-- · 2019-07-21 01:12
find . -name "filename" -printf '%TY:%Tm:%Td %TH:%Tm %h/%f\n' | sort

Forget xargs. "Find" and "sort" are all the tools you need.

查看更多
在下西门庆
4楼-- · 2019-07-21 01:24

Check the below-shared command:

1) List Files directory with Last Modified Date/Time To list files and shows the last modified files at top, we will use -lt options with ls command.

$ ls -lt /run
output
total 24
-rw-rw-r--.  1 root utmp 2304 Sep  8 14:58 utmp
-rw-r--r--.  1 root root    4 Sep  8 12:41 dhclient-eth0.pid
drwxr-xr-x.  4 root root  100 Sep  8 03:31 lock
drwxr-xr-x.  3 root root   60 Sep  7 23:11 user
drwxr-xr-x.  7 root root  160 Aug 26 14:59 udev
drwxr-xr-x.  2 root root   60 Aug 21 13:18 tuned

https://linoxide.com/linux-how-to/how-sort-files-date-using-ls-command-linux/

查看更多
来,给爷笑一个
5楼-- · 2019-07-21 01:29
find . -name "filename" -exec ls --full-time \{\} \; | cut -d' ' -f7- | sort

You might have to adjust the cut command depending on what your version of ls outputs.

查看更多
登录 后发表回答