Monday, June 3, 2013

Not able to delete files in Linux                                               Find command with mtime and atime options

Find command in linux is most important for linux administrators as well as shell script developers. It is really very fast in execution speed and it is useful because of availability of wide variety options with find command.

Find has many options available like ctime i.e. creation time, type – type of file, newer – compare files and decide, it is newer or older as compared to one file, inum- inode number.
Examples:
  • Search files by inode numbers with find command: find inum
root@hello:~/ctier/examples# find . -inum 5770071
./README.txt
root@hello:~/ctier/examples#
  •  Search files newer as compared to given file: find newer
root@hello:~/ctier/examples# find . -type f -newer filename
root@hello:~/ctier/examples# find . -type f -newer README.txt

Newer option helpful to get new files as compared to given file timestamp.
  • Search files older as compared to given file: find newer
root@hello:~/ctier/examples# find . -type f !-newer README.txt

To reverse the output of 2nd example, we need to put !mark before the –newer option. It will negate the meaning of command after the mark.
  •  Search files by uid: find uid
root@hello:~#find . -type f -uidNumeric USERID
root@hello:~#find . -type f -uid 1002

  •  Search files whose creation time is older than 30 days

root@hello:~#find . -type f -ctime +30
  •  Search files whose creation time is within last 30 days

root@hello:~#find . -type f -ctime -30
  •  Search log files whose creation time is older than 30 days and delete it

In this case, we need to search files whose creation time is older than 30 days and need to provide this list of files to the rm command with the help of –exec option. We can run any file manipulation command after exec option e.g. ls –lrt, cat, wc etc.

root@hello:~#find . -type f -name "*.log" -ctime +30 -exec rm -rf {} \;


If you need more examples or facing issues with some option then please write us at linuxconcepts@gmail.com.

How to install Wine Application                                              How to install cygwin and configure

Posted by Machindra Dharmadhikari On 6/03/2013 08:30:00 PM 1 comment

1 comment:

  1. Hey Brother,

    Hot! That was HOT! Glued to the find command in linux examples of ctime your proficiency and style!

    Is is true the number of people with Unix skills is really declining?
    A lot of Microservices, serverless computing bandwagon is seen on the market, does it means Unix/Linux and servers will be non-existent in future?

    Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.

    Ciao,
    Irene Hynes

    ReplyDelete

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner