Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to achieve the following using a shell script. When a script is called with a file as a parameter, it should move the file based on a portion of its name.

For example, "Car insurance quotations_zz21.pdf" to sort into 21.Car insurance (a sub-folder of FilingCab) "2012-09-01 Home insurance quote_zz20.pdf" to sort into 20.Home insurance (a sub-folder of FilingCab)

Logically I think it should read something like this. I did programming long long time ago, so I think have the algorithm, but do not have the commands or the syntax.

Thanks a bunch for help!


FileToMove = $1
FolderNo = (The number after zz and before a .)
ParentFolder = (~/FilingCab)
FolderList = (List of all folders in ParentFolder) 
TargetFolder = (Folder among FolderList that begins with FolderNo) - use grep?

if TargetFolder <> not empty
         cd ParentFolder
         mv FileToMove to TargetFolder
end with success
else
         do nothing
endif

end script
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try something like this:

#! /bin/bash

# Config variable(s)
ParentFolder="~/FilingCab"

# arg processing (minimalist :)
FileToMove="$1"

# use sed to extract folder number from filename.
FolderNo=$(echo "$FileToMove" | sed -r -e 's/.*zz([0-9]+)\.pdf/\1/')

# use find to search for subdirectories of parent folder that begin
# with the folder no followed by a '.'
TargetFolder=$(find "$ParentFolder" -maxdepth 1 -type -a -d -name "${FolderNo}.*")

NumFolders=$(echo "$TargetFolder" | wc -l)

if [ "$NumFolders" -eq 1 ] ; then 
 mv "$ParentFolder/$FileToMove" "$TargetFolder/" 
else
  echo "Error: $NumFolders beginning with "$FolderNo." found" >&2
  exit 1
fi

Note the double-quotes around all the variable names. It's always the safe/correct thing to do but in your case it's essential because the filenames and directory names you gave as examples have space characters in them.

The -maxdepth 1 in the find command only searches for direct subdirectories of $ParentFolder (i.e. ignores subdirs of subdirs). If that's not what you meant, just remove that part. This find searches for any subdirectories of $ParentFolder that begin with $FolderNo.

I'm not entirely sure what you mean by "if TargetFolder is not equal to not empty", i'm assuming you meant "is the string $TargetFolder non-empty". Ordinarily, I'd check if $TargetFolder existed and was a directory but since we're getting the directory name from a find -type d, it can only be empty or a directory. or maybe more if there's more than one beginning with "$FolderNo."

I think it's better to check if we get one and exactly one result from the find. Any other value is an error, and we can't move the file. 0 means no matching folders found. 2 or more means more than one folder beginning with "$FolderNo." was found.

The warning msg to stderr is optional. so is the exit 1, but recommended. It allows you to call this script from another script and check whether it was successful or not.

share|improve this answer
    
BTW, you could easily make the script process multiple files at once by changing FileToMove="$1" to for FileToMove in "$@" ; do, and adding done to terminate the for-loop as the last line of the file. –  cas Sep 1 '12 at 15:17
    
Wow. This looks like will work. I will try it out and report back! Thank you for the help! Btw, I am doing this on a Mac, and had the option of trying to figure out using AppleScript, but since Unix commands are so much more capable (I feel). And your script shows how compact this can be! I will try this out and also read sed manual again. –  jim70 Sep 1 '12 at 16:07
    
Craig, the -r option in sed does not seem to work on a Mac. I looked up the manual for sed and it appears that -r option is "less portable". I will look up the sed manual for Mac. Looked it up here and no -r option. developer.apple.com/library/mac/#documentation/Darwin/Reference/… Any ideas as to what to do? MEanwhile, I will study up the Mac sed and see what I can do with it. –  jim70 Sep 1 '12 at 17:23
    
Upon more research it appears -E might be the flag on Mac to replace -r. So -E worked. Now to debug find etc. Will do and update. –  jim70 Sep 1 '12 at 17:37
    
The script now works. Thank you for the help and for the tips as to why you included double-quotes. And your thinking about how find should return only one answer is spot on! –  jim70 Sep 1 '12 at 20:15
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.