4

I want to list all the files I've modified this year and back those up. Does anyone know if there is a command to list them? Thank you in advance.

  • 1
    I don't know that that in concept is the best idea. Modified will find a ton of files on your system that you would need to omit. Are you trying this on a user home directory? Or the entire system? – user508889 Nov 22 '16 at 18:10
17

If you want to find files, the find command is a powerful tool. You can look through a directory and print out the paths of all files that match some tests:

# find all files in /some/directory whose name starts with 'project_b'
find /some/directory -iname 'project_b*'

# find all files in /some/directory which are owned by user 'joe'
find /some/directory -user joe

# find all files in /some/directory whose name starts with 'project_b'
# but which are *not* owned by user 'joe'
find /some/directory -iname 'project_b*' -and -not -user joe

To get the date of the last modification of a file (or more precise, the last modification of a file's content), you can check for the mtime timestamp. find has a test for mtime:

# find all files, whose mtime is less than 365 days back
find /some/directory -mtime -365

That so far gives you a list of all files you want to backup. Now for the backup itself. find brings an option called -exec that applies a command to every file it finds:

find /some/directory -iname '*.txt' -exec cp {} /somewhere/else \;

If the find command finds test.txt, other.txt and something.txt, the -exec part would execute:

cp test.txt /somewhere/else
cp other.txt /somehwere/else
cp something.txt /somewhere/else

As you probably can see, the {} is replaced with the file in question.

EDIT: You probably need to find a better solution than just cp for the backup itself, because cp would just copy every file to /somewhere/else without keeping the directory structure.

Overall, a dedicated backup program might be a better choice.

  • I appreciate the help! Thank you very much and take care. – Peacemaker5 Nov 22 '16 at 20:02
  • 3
    For such task, it's a good idea to use --parents flag with cp command, as it preserves the file hierarchy structure and backups those files sharing the same name. – Tianren Liu Nov 22 '16 at 20:48
  • @Tianren Thanks for the help! I appreciate the extra info. Take care. – Peacemaker5 Nov 22 '16 at 21:51
  • 4
    If you want to maintain the directory structure of whatever find… um… finds you should probably use something like find ... -print0 | cpio -p0dv /destination. cp doesn't maintain the directory structure (unless the source object is a directory). Also, the cp variant could be written better as -exec cp -t /somewhere/else {} + which doesn't create a new process for every match. – David Foerster Nov 23 '16 at 0:15
  • 1
    You could also redirect the output of find to a file, then tell tar or rsync to use that file as the list of source files – Mark K Cowan Nov 23 '16 at 13:58
5

Assuming you want to keep the date confined to the current year you could use -newermt with the date.

This will look in the current directory for any files after November 1st, 2016 and copy them to /target while maintaining directory structure.

find . -newermt "2016-11-01 00:00:00" -exec cp --parents {} /target \;

The --parents added to the cp command allows you to copy the files maintaing the folders it exists within.

For example...

ubuntu@ubuntu-xenial:~$ ls
percona-release_0.1-4.xenial_all.deb  t  t2  testdir  testfile2.vm  testfile.vm
ubuntu@ubuntu-xenial:~$ cp --parents t/output.txt testdir/
ubuntu@ubuntu-xenial:~$ ls testdir/
directory2  t

When I copied t/output.txt to testdir/ it created the folder t within testdir/ and then copied the file.

  • Just to clarify, what does the "--parents" do? And thank you for both of your responses. – Peacemaker5 Nov 22 '16 at 19:57
  • updated, @Peacemaker5 – user508889 Nov 22 '16 at 20:17
  • What will happen if you enter the command without the "--parents"? Thanks again. – Peacemaker5 Nov 22 '16 at 20:56
  • 1
    It will copy the entire list of files into /target without creating subfolders – user508889 Nov 22 '16 at 21:20
  • Thank you for the helpful replies! I appreciate the info. [Though being a newbie, I don't understand some of it. :-) ] – Peacemaker5 Nov 22 '16 at 21:35
2

To list all the files modified this year along with its details:

find / -mtime -365 -ls

This will list everything from last 365days.

To show only regular files add -type f to the above command.

To get a list of all the modified files(with full path):

find / -mtime -365

You may back-up these files anywhere using cp or scp etc.

Add -printf "%f\n" to end of the command to print only filenames.

  • "and back those up" – Rinzwind Nov 22 '16 at 18:27
  • Just to clarify, what are "regular files"? (And thank you for your quick response.) – Peacemaker5 Nov 22 '16 at 18:48
  • @Peacemaker5 I meant non-directory files.. – Ani Menon Nov 22 '16 at 18:56
  • @Rinzwind just copy the files to where ever... – Ani Menon Nov 22 '16 at 19:00
  • 1
    It makes the output for each file long (with a lot of information similar but not the same as 'ls -l') - test it and see what it looks like in a wide terminal window (more than 80 characters wide, for example full screen). – sudodus Nov 22 '16 at 19:57
1

I use some aliases

alias last-24-hours='sudo find * -ctime -1 -type f'
alias last-week='sudo find * -ctime -7 -type f'

and you can make an alias

alias last-year='sudo find * -ctime -365 -type f'

but it will probably produce a very long list.

-o-

If you want a graphical overview, you can use baobab, to see where there is a lot of data (and where you could 'house-clean' before the backup).

-o-

An alternative is to use a tool, that can make an incremental backup, for example rsync. It will automatically find what needs to be added or replaced in the backup.

  • I appreciate the help! Thank you very much for both your replies, and take care. – Peacemaker5 Nov 22 '16 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.