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.
foo_1and another calledfoo_2? Where should the filefoogo 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.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$1and don't explain where that takes its values from. The syntax error is the space afterfilename=. You need to show us the exact script you are using.