find
supports a lot of date input formats. The simplest format to obtain is YYYYMMDD HH:MM:SS. You already have the digits in the right order, all you have to do is extract the first group (${timestamp%??????}
: take all but the last 6 characters; ${timestamp#????????}
: take all but the first 8 characters), and keep going, appending punctuation then the next group as you go along.
timestamp=20130207003851
timestring=${timestamp%??????}; timestamp=${timestamp#????????}
timestring="$timestring ${timestamp%????}"; timestamp=${timestamp#??}
timestring="$timestring:${timestamp%??}:${timestamp#??}"
In bash (and ksh and zsh), but not in ash, you can use the more readable ${STRING_VARIABLE:OFFSET:LENGTH}
construct.
timestring="${timestamp:0:8} ${timestamp:8:2}:${timestamp:10:2}:${timestamp:12:2}"
To sort files by date, print out the file names preceded by the dates and sort that, then strip the date prefix. Use -printf
to control the output format. %TX
prints a part of the modification time determined by X
; if X
is @
, you get the number of seconds since the Unix epoch. Below I print three tab-separated columns: the time in sortable format, the file name, and the time in human-readable format; cut -f 2-
removes the first column and the call to expand
replaces the tab by enough spaces to accommodate all expected file names (adjust 40 as desired). This code assumes you have no newlines or tabs in file names.
find -maxdepth 1 -type f \
-newermt "$timestring" -printf '%T@\t%f\t%Tb %Td %TH:%TM\n' |
sort -k1n |
cut -f 2- |
expand -t 40
for f in *.jpeg; do
[ -e "${f%.*}.jpg" ] && echo rm -- "$f"
done
(remove echo
if happy).
With zsh
and one rm
invocation:
echo rm -- *.jpeg(e'{[ -e $REPLY:r.jpg ]}')
(change *
to **/*
to do that recursively, add the D
glob qualifier, if you also want to consider hidden files or files in hidden directories).
Best Answer
you can use the
--attributes-only
switch ofcp
for this purpose, eg.From the
man
page ofcp
:This will create empty files with all attributes of the original file preserved but no contents.