This will delete all the files with a name ending in .swp
, ~
, .netrwhist
, .log
or .bak
anywhere under your home directory. No prompt, no confirmation, no recovery, the files are gone forever.
find ~ -type f \( -name '*.swp' -o -name '*~' -o -name '*.bak' -o -name '.netrwhist' \) -delete
(I purposefully omit *.log
because it sounds dangerous, this is not a common extension for temporary files and there are plenty of non-temporary files with that name.)
If your OS isn't Linux, replace -delete
by -exec rm {} +
.
You should perhaps configure Vim to put its swap files in a single directory by setting the directory
option:
set dir=~/tmp/vim-swap-files//,/var/tmp//
Create the directory first. The //
at the end makes the swap file name include the directory location of the original file, so that files with the same name in different directories don't cause a crash.
You can do the same thing for backup files with the backupdir
option, though it makes a lot less sense.
If you use Emacs, set auto-save-file-name-transforms
to point every file to a single directory.
(setq auto-save-file-name-transforms
'("\\`.*\\'" "~/tmp/emacs-auto-save-files/\\&" t))
You want
logrotate http://linuxcommand.org/man_pages/logrotate8.html.
It is probably already on your system. You just need to configure it. However it is mainly for the purpose of purging old log files and I don't know if you can configure it to keep one file.
What you can do
create several directories log
, log.weekly
, log.monthly
, and log.yearly
log
being where all the log files go. Create
- a weekly cron job that copies the latest log file from
log
to log.weekly
,
- a monthly cron job that copies the latest log file from
log
to log.monthly
, and
- a yearly cron job that copies the latest log file from
log
to log.yearly
.
Then configure logrotate appropriately for the different directories.
#!/bin/bash
NOW=$(date +%
ls -rt1 ${LOG} | while read FILE
do
TVAL=$(stat --printf %W ${LOG}/${FILE})
if [ $(ls -1 ${LOG.WEEKLY} | wc -l) ] -eq 0 ]
then
cp ${LOG}/${FILE} ${LOG.WEEKLY}/${FILE}
else
LAST_WEEKLY=$(ls -t1 ${LOG.WEEKLY} | head -n 1 | stat --printf %W)
if [ $((${TVAL}-${LAST_WEEKLY})) -gt $((60*60*24*7)) ]
then
cp ${LOG}/${FILE} ${LOG.WEEKLY}/${FILE}
fi
fi
# repeat the above logic for month and year
rm ${LOG}/${FILE}
done
Best Answer
Most filesystems don't store the creation time of a file, so the best you can do is check the last time the file was modified.
If you have a recent version of GNU find (e.g. on Linux or Cygwin) or FreeBSD or OSX, you can directly compare the date of a file with that of another file. In addition, these versions of find can use the file creation time (called its birth time, indicated with a
B
) if it's available on your system. ReplaceB
bym
below to use the modification time rather than the birth time.Run it once without
-delete
first to make sure these are the files you want. The command above will also delete files in subdirectories; if this is not desired, add-mindepth 1 -maxdepth 1
after the directory name.If your version of
find
doesn't have the-newerXY
primary, you'll need to create timestamp files to mark the boundaries of the range of times you want to match.Zsh's glob qualifiers can match files in a time interval, but the boundaries can only be indicated relative to the current date (e.g. N days ago).
You can also use timestamp files for precise dates, but you lose in terseness.