A
A
Alex2016-04-17 13:11:49
linux
Alex, 2016-04-17 13:11:49

How to delete old files using the console command?

How to delete those files and subfolders that have not been opened during the last month using console commands ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Bogachev, 2016-04-17
@Kozack

Linux itself can only store the creation time of a file, the time it was last modified, and the time it was last accessed (access time). With find , you can find all files that were accessed more than 30 days ago and delete them, like this:
But it's worth noting that "the file was accessed" and "it was opened in some program" are not the same .e. the file access time may change as a result of other actions that you would not call the word "open".

J
jcmvbkbc, 2016-04-17
@jcmvbkbc

Like this (for files):
find -type f -atime +30 -delete
For this to work, the file system must support atime.

V
vampire333, 2016-04-17
@vampire333

Deleting files older than N days
$ find /dir/ -atime +N | xargs rm -f
also:
$ find /dir/ -atime +N -delete
you can also do this:
$ find /dir/ -name "*.jpg" -mtime +N -exec rm -f {} \;
Switches:
-name — search by file name; when using wildcard patterns, the parameter is enclosed in
quotation marks.
-type - type of search: f=file, d=directory, l=link.
-user - owner: username or UID.
-group - owner: user group or GID.
-perm — access rights are specified.
-size - size: specified in 512-byte blocks or bytes (the sign of bytes is the "c" character behind the number).
-atime is the time the file was last accessed.
-ctime Time when the owner or permissions of the file were last changed.
-mtime - last modified
time -newer other_file - look for files created later than other_file.
-delete - delete found files.
-ls - generates output like the ls -dgils command.
-print - displays the found files on the screen.
-exec command {} \; - executes the specified command on the found file; pay attention to the
syntax.
-ok - before executing the command specified in -exec, issues a prompt.
-depth - start the search from the deepest nesting levels, and not from the directory root.
-prune is used when you want to exclude certain directories from the search.
N is the number of days.
stolen from linux-notes.org

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question