Unix / Linux: Check New Files In File System /var/www/uploads/

by on February 2, 2014 · 5 comments· LAST UPDATED February 4, 2014

in , , ,

Recently, I switched from MS-Windows based web-server to CentOS Linux based Apache web-server. All user uploaded files are stored in /var/www/uploads/ directory. Is there command that can give me a list of files that have been added to the filesystem at /var/www/uploads/ in last 7 days on Linux/Unix-like oses?

You need to use the following commands:

Tutorial details
DifficultyIntermediate (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m
  1. date command - Get the system date.
  2. touch command - Create a file and set file timestamps using date command.
  3. find command - Search for files in file system as per given condition.

Step #1: Get the current date

Type the following date command to get the date as per your requirements:

## get old date i.e. if today is 27/Jan get 20/Jan in $d ##
d=$(date +"%Y-%m-%d" --date="7 days ago")
echo "$d"
 

Sample outputs:

2014-01-20

Step #2: Create a new file

Type the following touch command:

 
file="/tmp/test.txt.$$"
touch --date "$d" "$file"
echo "$file"
ls -l "$file"
 

Sample outputs:

/tmp/test.txt.17697
-rw-r--r--. 1 nixcraft nixcraft 0 Jan 20 00:00 /tmp/test.txt.17697

Step #3: List newer files

To find files in the /var/www/upload/ directory tree that are newer than the $file (/tmp/test.txt.17697 file), use the find command as follows:

find /var/www/upload/ -newer $file

OR

find /var/www/upload/ -type f -newer $file

OR

find /var/www/upload/ -type f -iname "*.jpg" -newer $file

OR

find /var/www/upload/ -iname "*.jpg" -newer $file -ls

OR bsd/unix safe options:

find /var/www/upload/ -name "*.jpg" -newer $file -exec ls -l {} \;

Sample outputs:

15728917   20 -r--r--r--   1 cyberciti cyberciti    18144 Jan 27 06:47 ./01/last-command-output-300x118.jpg
11534726   92 -r--r--r--   1 cyberciti cyberciti    91370 Jan 27 06:47 ./01/last-command-output.jpg
11534720   12 -r--r--r--   1 cyberciti cyberciti     9691 Jan 27 03:44 ./01/who-command.jpg
11534721  104 -r--r--r--   1 cyberciti cyberciti   104077 Jan 27 04:08 ./01/who-command-output.jpg

A shell script to check new files in the file system

#!/bin/bash
# A quick shell script to show new files added to the file system
# Syntax ./script /path/to/dir days 
# Defaults ./script $PWD 3 
# Author: nixCraft <webmaster@cyberciti.biz> under GPL v2.x+
# -----------------------------------------------------------------
_pwd="$(pwd)"
_now=$(date +"%Y-%m-%d" --date="${2:-3} days ago")
_d="${1:-$_pwd}"
 
# a bad idea but I'm too lazy
_f="/tmp/thisfile.$$"
 
touch --date "$_now" "$_f"
find "$_d" -type f -newer "$_f"
/bin/rm -f "$_f"

Sample outputs:

./script
/home/nixcraft
/home/nixcraft/.viminfo
/home/nixcraft/.lesshst
/home/nixcraft/.bash_history
./script 7 /var/www/uploads/
/var/www/uploads/who-command-150x119.jpg
/var/www/uploads/last-command-output-150x150.jpg
/var/www/uploads/who-command-output-150x150.jpg
/var/www/uploads/ubuntu-find-ip-address-ip-command-300x64.png
/var/www/uploads/redhat-rhel-version-release-command.png

find command mtime option

Pass the -mtime n option to find command to get file's data was last modified n*24 hours ago, so:

## List files uploaded in last 3 days directly using find command ###
## GNU/Linux specific example ##
find /var/www/uploads/ -iname "*.jpg" -type f -mtime -3 -ls
 

OR try the following bsd/unix specific example:

## list files uploaded in last 3 days directly using find command ###
find . -iname "*.jpg" -type f -mtime -3 -print0 | xargs -I {} -0 ls -l "{}"
 
Recommended readings
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 Ben Stokes February 2, 2014 at 9:12 am

Based on your original ask “list of files that have been added to the filesystem at /var/www/uploads/ in last 7 days”, this would also work:

find /var/www/upload/ -type f -mtime -7

Reply

2 Nix Craft February 2, 2014 at 9:53 am

Yes, I should have included an example of find command. I appreciate your post.

Reply

3 Bảo Nam February 5, 2014 at 8:13 am

Thanks, it’s very helpful

Reply

4 Emad February 6, 2014 at 8:47 am

Very usefull, thank you.

Reply

5 Anderson Deluiz February 12, 2014 at 5:21 pm

If you upload a file by SFTP or FTP, for example, and that fiile was created more than 7 days ago mtime option may not work. It’s better to use ctime instead

# find /var/www/upload/ -type f -ctime -7

Also take a look at “stat” command.

Reply

Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: