Unix Shell scripting for copying file from one folder to another - shell

Hi this is my shell script to copy files from one directory to another directory with timestamp.But my script shows too many arguments.I want to copy files from one directory to another.What error in my code.
Date_Val="$(date +%Y%m%d%H%M%S)";
cd /etl_mbl/SrcFiles/
if [ -f /etl_mbl/SrcFiles/SrcFiles_TEMP*.csv ]
then
cp /etl_mbl/SrcFiles/SrcFiles_TEMP/*.csv /etl_mbl/SrcFiles/Archive/*_$Date_Val.csv
fi

The reason you got "too many arguments" error is that the wildcard in the "if" statement expands to a multitude of files. Please also note that you cannot have wildcards in the destination of a "cp". You probably want something like this:
#!/bin/bash
Date_Val="$(date +%Y%m%d%H%M%S)";
for file in ./src/*.csv; do
filename=${file##*/}
basename=${filename%.*}
cp $file ./archive/$basename\_$Date_Val.csv
done

Related

Bash Shell Script if statement issue and cp/date command issues

I am new to UNIX and am having some issues with my bash script and would like some help. My script is designed to copy files from one directory to a target directory while outputting which files are being copied, the current date each file is being copied, and an error message if not enough parameters/input from the user.
So far, I have three parameters/arguments. 1)sourcePath which is the directory that has files. 2)backupPath, the directory the user wants to copy files to from sourcePath (if backupPath is not a directory, then it will automatically be made), and 3)filePrefix which prompts the user to enter a certain letter or pattern and any file from sourcePath that starts with that letter or has that pattern will be copied. Couple of issues so far are that the files being copied are not being displayed, so the user doesn't know which files were copied until they cd into the backupPath directory and ls to see. The second problem is the current date not showing. The third problem is that I have an if statement for if not enough paramteters have been passed that will echo a certain message, but that message is echoed no matter what, even if all parameters have been given. Code is below:
read -r -p "sourcePath: " sourcePath
read -r -p "backupPath: " backupPath
read -r -p "filePrefix: " filePrefix
if [ $# -lt 3 ]; then
echo "Create backup files in a target directory given
the target directory name and a file name prefix.
Only files found in the specified source directory
whose name begins with the supplied prefix will
be copied. In addition, all copied echo files will
have a datestamp suffix added.
syntax: ./dobackup.bash sourcePath backupPath filePrefix"
fi
mkdir -p /home/public/"$backupPath"
cp -v /export/home/public/"$sourcePath/$filePrefix"* `(date +%y%m%d)`
So as said above, "cp -v" seems to not be working properly as it lists every file in the backupPath and sourcePath directories when it is to only display files being copied.
My if statement outputs whenever the script is executed, when it should only output if not all of the parameters/inputs have been met, or typed in by the user. I understand this is a lot of help to ask for but I am still new to UNIX and scripting. I know how to do all of this if I was to just input the separate commands myself but am having difficulties here. All help is appreciated.

Searching a list of files in a folder in bash

I have a text file that has a list of filenames.Now I want to see if these files are present in a specific folder using bash script. I have no experience in writing bash scripts.So please help me with this
a simple for loop can do what you need :
for file in $(cat yourfilelist.txt); do ls -l /folder/to/check/"$file"; done
for missing files you'd get No such file or directory

Copying files from one folder to another using a shell script

I have a script to copy the files from one location to another where i am passing the first location as parameter to the script
#!/bin/bash
locatn=$1
echo $locatn
cp -r /locatn/ /ws/priyapan-rcd/workspace/automation/
but when i run this code this throws error as
cp: cannot stat `locatn': No such file or directory
what could be the issue
Formatting looks a bit weird but as #Patick Trentin said you simply forgot a $ making your script always copy the files to the same location ignoring the given parameter.
#!/bin/bash
locatn=$1
echo $locate
cp -r /${locatn}/ /ws/priyapan-rcd/workspace/automation/

Bash script to execute only on new files in directory

I'm currently running a Bash script called (log2csv) that runs against a specified .log file. Sitting in the desired directory I can type in terminal:
log2csv Red1_1.log
This will create Red1_1.csv
This is my current bash script:
#!/bin/bash
for path
do
base=$(basename "$path")
noext="${base/.log}"
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done
This script is actually running a perl script on the specified log and putting the results in a CSV output.
I can alternatively run in terminal:
log2csv *.log
This will run the script against all .log files in the current directory and create .csv files for every one.
What I would like the script to do is only run on .log files that haven't had .csv files created for them. After doing some research I think I possibly can use inotifywait to achieve this, but I'm unsure how to make this work in my script? I also have read that this may be an issue if you overwrite a file. Any help or ideas would be most appreciated!
What I would like the script to do is only run on .log files that haven't had .csv files created for them.
Simply skip those .log files whose corresponding .csv files already exist:
for path
do
base=$(basename "$path")
noext="${base/.log}"
[ -e "${noext}.csv" ] && continue # <---------------
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done

shell "if" statement

I am new to unix and am practicing a simple script to unzip a load of files within a specified directory. I need the program to move the zipped file into another folder when it is done unzipping it (I called this oldzipped folder). For simplicity, I have removed the part of the code unzipping the file and currently have the program working for a specific file rather than the *tar.7z file extention. For some reason, the mv statement is not working. Unix is saying the following when I try to run the script. Could someone give me a hand with this? Again, I know this is the long way of doing things, but I want practice writing a script. Please be nice, as I am very new to Unix :(
unzip5: line 14: [ASDE0002.tar.7z]: command not found
#!~/bin/bash
# My program to try to unzip several files with ending of tar.7z
# I have inserted the ability to enter the directory where you want this to be done
echo "What file location is required for unzipping?"
read dirloc
cd $dirloc
mkdir oldzippedfiles
for directory in $dirloc
do
if
[ASDE0002.tar.7z]
then
mv -f ASDE0002.tar.7z $dirloc/oldzippedfiles
fi
done
echo "unzipping of file is complete"
exit 0
[ is the name of a (sometimes built-in) command which accepts arguments. As such you need to put a space after it as you would when invoking any other program. Also, you need a test. For example, to determine if the file exists and is a file, you need to use -f:
if [ -f ASDE0002.tar.7z ]
then
mv -f ASDE0002.tar.7z $dirloc/oldzippedfiles
fi
Here are some other possible tests.

Resources