2

I can get the latest modified/created file in a directory using the command

ls -Art | tail -n 1

I would like to rename the file returned by the above command. How could i do that ?

I also tried something very rudimentary like this

ls -rt | xargs | awk {'print $1'} | xargs cat >> newfile.txt   

Sometimes it works but mostly it just writes an empty file

4

3 回答 3

3

尝试以下操作:

ls -ltr | awk '{ field = $NF }; END{ print field }' | xargs -I '{}' mv '{}' newfile.txt
于 2013-05-27T08:15:14.767 回答
2

用于find识别最近修改的文件而不是解析ls

find /path/to/search -type f -printf ‘%TY-%Tm-%Td %TT %p\n’ | sort -r | head -n1 | xargs -I '{}' mv '{}' newfile.txt
于 2013-05-27T08:17:44.123 回答
0
find ./ -maxdepth 1 -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1 | xargs -I '{}' mv '{}' newfile.txt

这将在当前目录中工作。对于不同的目录,在 . 之后更改路径find

如果文件名有空格,则此方法有效。

它也忽略目录。

于 2022-01-21T19:10:32.097 回答