Skip to main content
added 78 characters in body
Source Link

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/
adbs.spec
dmsxlogstash-avi-core.spec
fomru32logstash-core.spec
gendftpcore.spec

My destination directory

/path
/adbs_32
/dmsx_ty_32logstash-avi-core-2.2.0
/fomru_ts_tdlogstash-45core-2.1.1
/gend_monitorftpcore.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.

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/
adbs.spec
dmsx.spec
fomru32.spec
gend.spec

My destination directory

/path
/adbs_32
/dmsx_ty_32
/fomru_ts_td-45
/gend_monitor

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.

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.

added 327 characters in body
Source Link

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/
adbs.spec
dmsx.spec
fomru32.spec
gend.spec

My destination directory

/path
/adbs_32
/dmsx_ty_32
/fomru_ts_td-45
/gend_monitor

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= "$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.

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/
adbs.spec
dmsx.spec
fomru32.spec
gend.spec

My destination directory

/path
/adbs_32
/dmsx_ty_32
/fomru_ts_td-45
/gend_monitor

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

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/
adbs.spec
dmsx.spec
fomru32.spec
gend.spec

My destination directory

/path
/adbs_32
/dmsx_ty_32
/fomru_ts_td-45
/gend_monitor

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.

Source Link

How to move a file if the folder name contains the filename

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/
adbs.spec
dmsx.spec
fomru32.spec
gend.spec

My destination directory

/path
/adbs_32
/dmsx_ty_32
/fomru_ts_td-45
/gend_monitor

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