I am trying to test if the tar file I am about to create already exists. If it doesn't then I want to create the tar file (this part of the code works). However the if statement testing if that file name for the tar file exists, doesn't work (doesn't actually test if the tar file exists, just skips straight to the else statement even if the tar file does already exist). Any ideas?
Note: fire_dir_name is the name of the file we are trying to tar ie.) myfile1.txt
#create variable for new name of file/dir
date=$(date +"%Y%m%d")
new_name="$file_dir_name.$date"
file_name="$new_name.tar"
echo $file_name
#check if tar file exists already
if [ -f "$file_name" ]
then
echo "$file_name" exists
else
# create tar file
tar -cvf $dir_path/$new_name.tar $file_dir_name -C $dir_path
file_dir_name
anddir_path
, for example. If you use"$dir_path/$new_name.tar"
in place of"$file_name"
in the test, does it work? – Kusalananda♦ Oct 7 '20 at 13:29tar
is supposed to operate. I propose you learn pax, it's easier, modern, and is expected to be more portable, and I'll provide an answer based on pax. – DannyNiu Oct 7 '20 at 13:30$file_dir_name
in the$dir_path
directory? Or is it in the current directory (assuming the current working directory is not$dir_path
)? I'm asking because you are creating$new_name.tar
in$dir_path
and testing for the existence of the$file_name
file in the current working directory, without telling us if the two directories are supposed to be the same one. – fra-san Oct 7 '20 at 14:19