How do I move the oldest or newest file in a directory to another with Mac OS X Terminal

后端 未结 4 650
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 06:19

I\'m trying to us the following command on Mac OSX 10.6 Terminal but it does not work..Any idea what I may be doing wrong?

These work...

List most recent

4条回答
  •  攒了一身酷
    2021-01-25 07:17

    I found the another answer for the problem. The best to use find command instead of using ls command. It would help to find the oldest file and can use mv command to move to another directory.

    mv $(find /home/balaji/work/ -type f -printf '%T+ %p\n' | sort | head -1) /home/balaji/regular_archieve

    find – Search for files in a directory hierarchy. /home/balaji/work/ – Search location. /home/balaji/regular_archieve/ – Target location. type -f – Searches only the regular files. -printf ‘%T+ %p\n’ – Prints the file’s last modification date and time in separated by + symbol. (Eg. 2015-07-22+13:42:40.0000000000). Here, %p indicates the file name. \n indicates new line. sort | head -n 1 – The sort command sorts the output and sends the output to head command to display the oldest file. Here, -n 1 indicates only one file i.e oldest file.

提交回复
热议问题