1

I am very new to shell scripting in linux.

I have a bunch of files in source directory. I have to move the files to the target directory where the folder should contains the file name(not exact match).

Ex: My source directory

/var/backup/
logstash-avi-core.spec
logstash-core.spec
ftpcore.spec

My destination directory

/path
/logstash-avi-core-2.2.0
/logstash-core-2.1.1
/ftpcore.spec-2.0

I have written a script based on the reference but my script is not working. The files are not get copied to the target directory

for path in /var/backup/${1}
do
    if [ -f "${path}" ]
    then
        filename="$(<<< "${file}" awk -F'.')"
        if [ -n "${filename}" ]
        then
            cp "${path}" "/path/${1}/"
        fi
    fi
done

Thanks in advance

Update as per the clarification in the comments: I have mentioned the target directory "/path/${1}/" in cp "${path}" "/path/${1}/". I want to read the filename from the source directory and I have to search the foldername containing filename and need to move the file to the matched folder. This is my requirement.

Update2 I have modified filenames and directory names.

  • How can the script know the target directory? Can we simply take the first letters until the first non-letter character and look for a directory name starting with that? What if you have a directory called foo_1 and another called foo_2? Where should the file foo go to? Please edit your question and clarify your requirements. Also, the script you show has syntax errors, please always include the errors you get in your question. – terdon Apr 5 '16 at 15:29
  • @terdon. Please find the response for your questions. 1. I have mentioned the target directory in cp. 2. My directory name is unique it wont be foo_1 and foo_2. 3. i couldn't find the syntax error while I am running it doesn't show any errors. Since I am newbiw to scripting I couldn't figure. Can you please provide your guidance to alter my script – Mangoski Apr 6 '16 at 9:48
  • Please edit your question to add extra information. It is hard to read and easy to miss in the comments. Also, comments can be deleted with no warning. That said, where have you mentioned the target directory? You only mention $1 and don't explain where that takes its values from. The syntax error is the space after filename=. You need to show us the exact script you are using. – terdon Apr 6 '16 at 10:05
  • @terdon Question was edited with additional information. The above script is the one which I am using it exactly. – Mangoski Apr 6 '16 at 10:18
1

If I understand correctly, you want to take the first characters of each file name until the first non-letter, and match them against the first characters of the target directory until the first _. Assuming that each such pattern only matches once, you can do:

for file in /var/backup/*; do 
    fname=${file##*/}; 
    fname=${fname/.*/}; 
    cp -v "$file" /path/"$fname"*/; 
done

The result is:

$ ls /path/*
/path/adbs_32:
adbs.spec

/path/dmsx_ty_32:
dmsx.spec

/path/fomru_ts_td-45:
fomru32.spec

/path/gend_monitor:
gend.spec
  • My comparison didn't depend on the non letter. For example my file name is rty-ion.spec my target folder name rty-ion-2.2.2. Then my file rty-ion.spec should move to the folder rty-ion-2.2.2. My filename should compare with folder name omitting the numeric part in the folder. – Mangoski Apr 6 '16 at 10:57
  • It worked like awesome. Thank you man. You saved my day. – Mangoski Apr 6 '16 at 11:04
  • But some of the file got omitted. For eg: my filename logstash-core-avi.spec. Didnt move to the folder logstash-core-avi-2.1.3. How can I handle this. – Mangoski Apr 6 '16 at 11:07
  • @Mangoski then edit your question and explain that. You have a file called fomru32.spec and a directory called /fomru_ts_td-45. That's why I assumed that you wanted only the alphabetical part of the file name. – terdon Apr 6 '16 at 11:11
  • Question updated with filenames and foldernames – Mangoski Apr 6 '16 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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