2

sorry if this is asked before but, I was curious about what is the equivalent Linux command of forfiles.exe in Windows? This came to my mind when I saw this question

Serdar Dalgic
  • 149
  • 1
  • 5
  • 1
    If you included a description of exactly what forfiles.exe does, those of us who know Linux very well will find helping you much easier. – MikeyB Sep 18 '09 at 21:01
  • sorry, It's my bad not to explain what forfiles.exe does in windows; but a simple google search gives satisfying results on what forfiles.exe does. Anyway, I'm still ashamed of myself why "find" didn't come to my mind before asking this silly questions ;) – Serdar Dalgic Sep 18 '09 at 21:26
  • @MikeyB, you could have followed the link to the other question. There was a forefiles description there. I updated the question to include a link directly to the technet docs. – Zoredache Sep 18 '09 at 21:46

4 Answers4

4

find is the full-powered replacement, but for simple operations on files in the current directory this sh-script can be pretty useful (and easier to read/write) as well:

for file in *.jpg; do
  echo "do something with $file"
done
Joachim Sauer
  • 840
  • 5
  • 19
3

The find command does what you want. Here's an example:

find /mnt/Pictures -name '*.jpg' -mtime +7 -exec rm {} \;

This will delete all the jpg files from /mnt/Pictures, which have not been modified in the last 7 days (168 hours) or more. If you don't care about the case of the filenames use -iname instead of -name.

Here's a correspondence between the parameters of forfiles and the parameters of find:

  • /p → the path is the first argument of find.
  • /s → by default find searches in all subdirectories. To disable this use the -maxdepth option with a value of 1, i.e. -maxdepth 1.
  • /c-exec. Also replace @file with {} and don't forget to end the command with \; (many beginners are bit by this error).
  • /d-mtime n. File's data was last modified n*24 hours ago; use +n for greater than n and -n for less than than n.

In case you need to negate a condition, use !. Because ! is a special character under some shells, including Bash, it needs to be escaped or quoted, therefore you'll write \! most of the time.

Cristian Ciupitu
  • 6,396
  • 2
  • 42
  • 56
  • What does the `\!` flag after `-name` do? – VELFR Feb 25 '20 at 09:06
  • 1
    Your use of -mtime doesn't seem to match the documentation you linked to. You say that `\! -mtime 7` gives files that have not been modified within 7 days. But I think it should actually give all files that were not last modified exactly 7 days ago. It seems from the documentation that what you should want is `-mtime +7`. – A. R. Apr 28 '23 at 13:14
  • @AndrewRay well, it's ambiguous :-) If you want exactly 7 days, `!` is the way to go. If you want 7 days or more, `+7` is the way to go. Anyway, your made a good point, so I've edited my answer to make it more clear and perhaps more useful. – Cristian Ciupitu Apr 30 '23 at 09:38
0

I think find provides the functionality that forfiles offers.

Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • Ah yes, thanks for reminding ;) I searched google a little bit and I think this should be the answer: http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/ – Serdar Dalgic Sep 18 '09 at 20:41
-4

You can create a cron job that uses find with the appropriate arguments, e.g. -mtime +7

HTTP500
  • 4,833
  • 4
  • 23
  • 31