-1

Iam using centos 7

If I want to find files that have specific name and specific date then moving these files to another folder iam issuing the command

find -name 'fsimage*' | xargs ls -ali | grep 'Oct 20' | -exec mv   {}  /hdd/fordelete/  \;

with the following error

-bash: -exec: command not found xargs: ls: terminated by signal 13
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
oula alshiekh
  • 843
  • 5
  • 14
  • 40
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Oct 29 '17 at 14:40

2 Answers2

1

As another answer already explains, -exec is an action for find, you can't use it as a shell command. On contrary, xargs and grep are commands, and you can't use them as find actions, just like you can't use pipe | inside find.

But more importantly, even though you could use ls and grep on find's result just to move files older than some amount of time, you shouldn't. Such pipeline is fragile and fails on many corner cases, like symlinks, files with newlines in name, etc.

Instead, use find. You'll find it quite powerful.

For example, to mv files modified more than 7 days ago, use the -mtime test:

find -name 'fsimage*' -mtime +7 -exec mv '{}' /some/dir/ \;

To mv files modified on a specific/reference date, e.g. 2017-10-20, you can use the -newerXY test:

find -name 'fsimage*' -newermt 2017-10-20 ! -newermt 2017-10-21 -exec mv '{}' /some/dir/ \;

Also, if your mv supports the -t option (to give target dir first, multiple files after), you can use {} + placeholder in find for multiple files, reducing the total number of mv command invocations (thanks @CharlesDuffy):

find -name 'fsimage*' -mtime +7 -exec mv -t /some/dir/ '{}' +
randomir
  • 17,989
  • 1
  • 40
  • 55
  • Given as GNU extensions (to `find`) are already in use, you might consider using a GNU extension to `mv`, and making it `-exec mv -t /some-dir {} +` -- that way `find` needs to call far fewer `mv` invocations. – Charles Duffy Oct 29 '17 at 16:31
-1

the -exec as you wrote it is quite meaningless, moreover it seems you are mixing find syntax with shell oe (-exec as you wrote it should be passed to find)

there are probably more concise ways of doing, but this should do what you expect:

 find  -name 'fsimage*' -type f  | xargs ls -ali  | grep 'Oct 20' | awk '{ print $NF }' | while read file; do  mv "$file" /hdd/fordelete/  ; done

nevertheless, you should take care of not just copy/paste things you do not really understand from the web, you may wreck you system...

OznOg
  • 4,440
  • 2
  • 26
  • 35
  • iam new to centos but able to understand this script thanks alot for your advice – oula alshiekh Oct 29 '17 at 15:13
  • 1
    Parsing `ls` in this way is quite buggy. Any filename with spaces you'll only get the last piece from; filenames with newlines or other nonprintable characters have even less-sensible behavior. See http://mywiki.wooledge.org/ParsingLs for an in-depth discussion. – Charles Duffy Oct 29 '17 at 16:29