Look for file ksh - unix

I am trying to learn unix. I wanted to set up a sort of file watcher that looks for files and returns the file names so i can move them from source to processing folder to process. It echos File Found I just cannot figure out how to capture the file name.
#determines if file exists
if [ -f * ]; then
echo "File found"
else
echo "File not Found"
fi
# returns file to array
#Needs name still
NewFiles[0] =
#output what what found in 0 index
echo "Found File"
echo NewFiles[0]

Assuming the files have no fancy names (embedded spaces or similar), you might use that approach:
set -- *
[ $# -gt 0 ] && {
echo Found file
echo $1
}

Related

is there a way to check if a file exists with the glob * in Unix Shell Scripting?

Suppose I have the file "FILE_${RUNDATE}_0957_PROD.csv" to check if it doesn't exist, it must bypass the IF statement. If I setenv the variable $FILENAME with the full name it works with the code below, but using the '*' reg-ex ("0957" represents a timestamp which I don't know the exact value) it enters into the IF statement.
#!/bin/csh -f
set RUNDATE = `date +'%Y%m%d'`
setenv FILENAME "DATA_${RUNDATE}_*_PROD.csv"
if ( ! -eq "$FILENAME" ) then
/bin/echo "File NOT Found Locally! \n"
endif

How to get everything after an extension unix

I have a file name like
filename.txt.zip_20180202_30291_233
In script variable i will have filename in a variable and extension in variable
like
echo $fileprefix
filename
echo $filesuffix
.txt.zip
How do i rename the file to below one ? Moving everything after extension to before extension ? (Note: there could be any numbers after extension )
filename_20180202_30291_233.txt.zip
#!/bin/sh
prefix='filename'
suffix='.txt.zip'
name='filename.txt.zip_20180202_30291_233'
newname="$prefix${name#$prefix$suffix}$suffix"
echo mv "$name" "$newname"
This would output
mv filename.txt.zip_20180202_30291_233 filename_20180202_30291_233.txt.zip
The parameter substitution ${name#$prefix$suffix} removes $prefix$suffix, i.e. filename.txt.zip, from the start of $name, which gives you _20180202_30291_233. This is then prepended with $prefix and appended with $suffix to create $newname.
Looping over all files that matches "$prefix$suffix"* in the current directory and renaming all:
for name in "$prefix$suffix"*; do
newname="$prefix${name#$prefix$suffix}$suffix"
echo mv "$name" "$newname"
done
The echo is there for protection. Remove it once you are certain the code will do the right thing.
file_orig=filename.txt.zip_20180202_30291_233
fileprefix=filename
filesuffix=.txt.zip
file="${file_orig#$fileprefix}"
file="${file#$filesuffix}"
file="$fileprefix$file$filesuffix"
echo "$file"
filename_20180202_30291_233.txt.zip
https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

Unix determine if a file is empty

I am attempting to make a script that will check to see if there is any tyext within a file. I have developed the following script. I have made it check to see if there is exactly 2 arguments, see if the file exists, but I am having trouble checking the file for text within it. The code is as follows:
#!/bin/ksh
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage $0 inputfile outputfile"
exit 1
fi
#check if input file exists
if [ ! -f $1 ]; then
echo "$1 not found!"
exit 1
fi
#Check if input file is null
#This next block of code is where the issue is
if [ grep -q $1 -eq 0 ]; then
echo "$1 must have text within the file"
exit 1
fi
Any help would be appreciated
test's "-s" option checks if the file is empty -- see manual. So your last chunk would become
#Check if input file is null
#This next block of code is where the issue is
if [ ! -s $1 ]; then
echo "$1 must have text within the file"
exit 1
fi
Try using stat
stat -c %s filename

UNIX - Moving a deleted file back to it's original directory

All I want to do is move a file from my created recycle bin to it's own original path.
My code is as follows:
#!/bin/bash
restore ()
{
base=`basename $restore_file | cut -d"_" -f1`
echo "Your file $base has been restored"
mv deleted/$restore_file /$HOME/$base
}
restore_file=$1
if [ $# -eq 0 ]
then
echo "You have not entered a file"
elif [ -e deleted/$restore_file ]
then
restore
grep -v $(basename $restore_file) $HOME/.restore.info >> $HOME/.restore.inf o.tmp
cp $HOME/.restore.info.tmp $HOME/.restore.info
rm $HOME/.restore.info.tmp
else
echo "Your file "$restore_file" does not exist"
fi
I'm sure the error is in on line 7, just not sure how to rewrite. Any ideas?
Do you need to put a full path in before deleted?
You don't need the slash before $HOME, but that shouldn't be a problem.

unix shell script creating backup.sh

How to write a shell script named "backup.sh" which accepts one parameter, which would be a filename/directory.
Create a backup copy of that with the .bak appended to its name.Show message on success.
If the file/directory does not exist, show a proper message.
i did up to this point.please help me to figure this out
#!/bin/sh
#create_backup.sh
And add a .bak
bak="${backup.sh}.bak"
if [ "$#" -eq 0 ]
then
exit 1;
echo "File Succesfully backuped"
fi
cp ${1} "${1}.back"
echo "File is not found "
exit 0
#!/bin/bash -e
directory=$1
cp -r $directory $directory.bak
echo "Success"
obvious caveats with pathing/error codes/etc

Resources