1

For starters, I'm not the most advanced scripter. I was asked to create a script that would search through a directory for print files older than 14 days and move them to an archive directory. There are several directories that will have corresponding archive directories with them. To test this, I created two text files that would have the source directory and the destination directory. Things went fine when there was only one directory path in each file. However, when I added another directory path to each file, the script moved the first archive folder into the second archive folder.

The files found in the first line of printdirs.txt should be moved to the path found in the first line of archivedirs.txt. Then the loop runs again and the files found in the path on the second line of printdirs.txt should be moved to the path found on the second line of archivedirs.txt and so forth.

Here is what the script looks like:

#!/usr/bin/ksh printlist=/u/lawson/stage/Scripts/printdirs.txt archivelist=/u/lawson/stage/Scripts/archivedirs.txt # Checking the archive list and moving files for i in $(cat $printlist) ; do cd $i /usr/bin/find . -mtime +14 -type f -exec mv "{}" $(cat $archivelist) \; done 

Here is what the text files contain:

$ cat printdirs.txt /u/lawson/law/print/lawson/tim/1 /u/lawson/law/print/dgfinance/monday190/1 $ cat archivedirs.txt /u/lawson/law/print/archive /u/lawson/law/print/archive2 

How can I get the script to loop for each corresponding line and not copy the "archive" folder into the "archive2" folder?

UPDATE: Updated the script with terdon's help. Here is the output after adding the echo command:

# ./printarchive2.ksh /usr/bin/find /u/lawson/law/prin -mtime +14 -type f -exec mv /lawson/tim/1 /u/lawson/law/print/archive {} + /usr/bin/find /u/lawson/law/prin -mtime +14 -type f -exec mv /dgfinance/monday190/1 /u/lawson/law/print/archive2 {} + 

The script terdon wanted me to use is removing the "t" from the directory paths. I tested it out by changing the "t" to an "n". It removed the letter "n" from my directory paths. Is there another way to signify a tab field separator that Korn shell can read because apparently this does not work in KSH.

0

    1 Answer 1

    2

    I would use paste here. It's a nifty tool that can be used to combine files:

    $ paste printdirs.txt archivedirs.txt /u/lawson/law/print/lawson/tim/1 /u/lawson/law/print/archive /u/lawson/law/print/dgfinance/monday190/1 /u/lawson/law/print/archive2 

    As you can see above, it will print successive lines from both files, separated by tabs. The tab-separation is very useful if your directory names can contain spaces. Now, you can iterate over the paste output to do your thing:

    #!/usr/bin/ksh printlist="/u/lawson/stage/Scripts/printdirs.txt" archivelist="/u/lawson/stage/Scripts/archivedirs.txt" paste "$printlist" "$archivelist" | while IFS=$'\t' read source dest; do /usr/bin/find "$source" -mtime +14 -type f -exec mv -t "$dest" {} + done 

    Note that I i) removed the cd, there's no need for it, find can be run on the target from anywhere; ii) removed the for i in $(cat foo) syntax, that should generally be avoided and iii) replaced -exec ... \; with -exec + which is more efficient (it will try and combine mv calls, which is why the -t "$dest" is needed).

    If your mv doesn't support the -t option, change the find command back to:

    /usr/bin/find "$source" -mtime +14 -type f -exec mv "$source" "$dest" \; 
    7
    • I ran the script. Here is my output: # ./printarchive2.ksh find: bad status-- /u/lawson/law/prin find: cannot execute :: No such file or directory find: bad status-- /u/lawson/law/prin find: cannot execute :: No such file or directoryCommentedJun 29, 2016 at 16:31
    • @UncleJayRazz does /u/lawson/law/print exist?
      – terdon
      CommentedJun 29, 2016 at 16:40
    • (1) Don't quote {} in find commands. (2) The -t option of mv is a GNU-ism.CommentedJun 29, 2016 at 16:56
    • @SatoKatsura 1) D'oh! Thanks, copied that directly from the OP; 2) Fair enough, answer edited.
      – terdon
      CommentedJun 29, 2016 at 16:58
    • @terdon There is no directory called "/u/lawson/law/prin". It should be "/u/lawson/law/print". What is removing the "t" in print from the script?CommentedJun 29, 2016 at 18:29

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.