Pages

Friday, December 18, 2015

find command one liners - Part02

Home

Find Files Based on Access / Modification / Change Time

You can find files based on following three file time attribute.
-          Access time of the file. Access time gets updated when the file accessed.
-          Modification time of the file. Modification time gets updated when the file content modified.
-          Change time of the file. Change time gets updated when the inode data changes.
In the following examples, the difference between the min option and the time option is the argument.
min argument treats its argument as minutes.
min 60 = 60 minutes (1 hour)

time argument treats its argument as 24 hours.
time 2 = 2*24 hours (2 days)

While doing the 24 hours calculation, the fractional parts are ignored so 25 hours is taken as 24 hours, and 47 hours is also taken as 24 hours, only 48 hours is taken as 48 hours. To get more clarity refer the -atime section of the find command man page.

Find files whose content got updated within last 1 hour
To find the files based upon the content modification time, the option -mmin, and -mtime is used. Following is the definition of mmin and mtime  
-mmin n # File’s data was last modified n minutes ago
-mtime n # File’s data was last modified n*24 hours ago

Find files in the current directory and sub-directories, whose content got updated within last 1 hour (60 minutes)
# find / -mmin -60

Find all the files (under root file system /) that got updated within the last 24 hours (1 day).
# find / -mtime -1

Find files which got accessed before 1 hour
To find the files based up on the file access time, the option -amin, and -atime is used. Following is the definition of amin and atime
-amin n # File was last accessed n minutes ago
-atime n # File was last accessed n*24 hours ago

Find files in the current directory and sub-directories, which got accessed within last 1 hour (60 minutes)
# find / -amin -60

Find all the files (under root file system /) that got accessed within the last 24 hours (1 day).
# find / -atime -1

Find files which got changed exactly before 1 hour
To find the files based up on the file inode change time, the option -cmin, and -ctime is used. Following is the definition of cmin and ctime
-cmin n # File’s status was last changed n minutes ago.
-ctime n # File’s status was last changed n*24 hours ago.

Find files in the current directory and sub-directories, which changed within last 1 hour (60 minutes)
# find / -cmin -60

Find all the files (under root file system /) that got changed within the last 24 hours (1 day).
# find / -ctime -1

The following find command displays files that are accessed in the last 30 minutes.
# find /etc/sysconfig -amin -30
Note: The above output contains both files and directories

# find /etc/sysconfig -amin -30 -type f
Note: The above output contains only files

Restricting the search only to unhidden files. (Do not display hidden files in find output)
# find / -mmin -15 \( ! -regex ".*/\..*" \)

Find files which are modified after modification of a particular FILE
# find -newer /etc/passwd

26.          Find files which are accessed after modification of a specific FILE
# find -anewer /etc/hosts

Find files whose status got changed after the modification of a specific FILE.
# find -cnewer /etc/fstab

Syntax to perform any operation on files found from find command
# find <CONDITION to Find files> -exec <OPERATION> \;
Where,
The OPERATION can be anything such as:
rm # command to remove the files found by find command.
mv # command to rename the files found.
ls -l # command to get details of the find command output files.
md5sum # on find command output files
wc # command to count the total number of words on find command output files.

To ls –l in find command output. Long list the files which are edited within the last 1 hour.
# find / -mmin -60
# find / -mmin -60 -exec ls -l {} \;

Searching Only in the Current Filesystem
# find / -name "*.log"
This will search for the file only in the current file system. Following is the xdev definition from find man page: -xdev don’t descend directories on other filesystems.
Following command will search for *.log files starting from / (root) and only in the current file system. i.e If you have multiple partitions mounted under / (root), the following command will NOT search all those mounted partitions.
# find / -xdev -name "*.log"

Using more than one { } in same command
# find -name "*.txt" cp {} {}.bkup \;

You can simulate it by writing a shell script as shown below.
# mv "$1" "`basename "$1" .htm`.html"

These double quotes are to handle spaces in file name. And then call that shell script from the find command as shown below.
# find -name "*.html" -exec ./mv.sh '{}' \;

Redirecting errors to /dev/null
# find -name "*.txt" 2>>/dev/null

Substitute space with underscore in the file name

Having space in the file name is not so good for Linux kind of systems. You can use the find and rename command combination as shown below to rename the files, by substituting the space with underscore.

# find . -type f -iname “*.txt? -exec rename “s/ /_/g” {} \;

Back To Top
Home

No comments:

Post a Comment