Find the answer to your Linux question:
Results 1 to 6 of 6
Im trying to do if the files that are 10 days or older to move to another directory. i know the command ls -la , lets you see the files ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    7

    bash help

    Im trying to do if the files that are 10 days or older to move to another directory.
    i know the command ls -la , lets you see the files info like dates, size and name...
    How would i would i move files that are 10days or older into another directory? Any hints or clues helps alot....
    I know the move command but i don't know what is the command to check the files that are 10days or older and move it.

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    You can mv all the files whose last modification time is, let's say, greater than 10 days, by doing this:

    Code:
    find /path/to/search/into -mtime +10 -exec mv '{}' /new/path/ \;

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    7
    Thanks, i tried that but it say it cant -exec can't be done.. why? i did find /path/to/search/into -mtime +10 -exec mv {file}

    and gave me an error msg about exec.. Do need to have the /new/path/ \;

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by horent135 View Post
    Thanks, i tried that but it say it cant -exec can't be done.. why? i did find /path/to/search/into -mtime +10 -exec mv {file}

    and gave me an error msg about exec.. Do need to have the /new/path/ \;
    The '{}' part is necessary, quotes included. You must use the command I posted above textually, you only need to change the "/path/to/search/into" (which is the dir where you want to seach) and the "/new/path/", which is the place where you want to put the files you are moving.

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    7
    if i wanted to move all the files in my computer that x number of days old. Do i type that one simple command under root?

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, let's explain exactly what is going on.

    The find utility does exactly what you expect: it finds all files in the subtree starting at the given directory. If you say /home/user, it will find all files under /home/user. If you say /, it will find every file on the system.

    Now then, you usually don't want to find every file. You want to find files that meet some criteria. For instance, "-iname" allows you to do a case-insensitive match on the filename. "-mtime" allows you to find files that were last modified in a certain time period, etc. etc.

    Finally, what do we do when we find a file? By default, we just print the path of the file. However, you can specify some command to run by using "-exec". "-exec" will treat everything that follows it until it sees a semicolon as the command to run. If it sees the literal "{}", it will replace that with the filename.

    So technically, this _should_ work:
    Code:
    find old_dir/ -exec mv {} new_dir/ ;
    It does not, however. There are two problems in the above, relating not to find, but to Bash. In Bash, curly brackets are used to denote blocks, and semicolons are used to separate statements. Therefore, if we want the "{}" and ";" to actually get sent to find (and not removed by Bash), we must tell Bash not to do anything with them. We do this by quoting the "{}", and by escaping the ";". The final command, therefore, looks like this:
    Code:
    find old_dir/ -exec mv '{}' new_dir/ \;
    Which is exactly what i92 proposes.

    Does this make sense? You can see the man page for more details: just run the command "man find".
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...