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


My destination directory


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}
    if [ -f "${path}" ]
        filename="$(<<< "${file}" awk -F'.')"
        if [ -n "${filename}" ]
            cp "${path}" "/path/${1}/"

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
    Commented Apr 5, 2016 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
    Commented Apr 6, 2016 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
    Commented Apr 6, 2016 at 10:05
  • @terdon Question was edited with additional information. The above script is the one which I am using it exactly.
    – Mangoski
    Commented Apr 6, 2016 at 10:18

1 Answer 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 
    cp -v "$file" /path/"$fname"*/; 

The result is:

$ ls /path/*



  • 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
    Commented Apr 6, 2016 at 10:57
  • It worked like awesome. Thank you man. You saved my day.
    – Mangoski
    Commented Apr 6, 2016 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
    Commented Apr 6, 2016 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
    Commented Apr 6, 2016 at 11:11
  • Question updated with filenames and foldernames
    – Mangoski
    Commented Apr 6, 2016 at 11:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .