1

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
  • 2
    Welcome to the site. If you say "it doesn't work", can you be more specific? If possible, reproduce actual output from the script so we can see how it doesn't work as expected. – AdminBee Oct 7 '20 at 13:20
  • Thank! edited :) – Natalie Lloyd Oct 7 '20 at 13:29
  • 2
    There are a number of variables used that you never show the definition of. file_dir_name and dir_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:29
  • You already asked the essence of this question as this, obviously you didn't understand how tar 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
  • 1
    Is $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
0

If we ignore the part where you missed the fi clause, the essence of the problem revolves around just before your invocation of tar.

Assume you want to

  • create a tarball named $newname

  • for the directory entry $file_dir_name

  • inside $dir_path

You should first cd to $dir_path. I'm not sure if the -C flag is portable, but the newer pax command don't have it for the obvious reason of reducing complexity.

Using -C flag has another problem: if $dir_path is relative, it'd create the tarball in the wrong place.

The reason your file existence check fail is, I guess, you didn't change to the directory that $file_name is supposed to exist.

Next, specify the output destination as filename of the tarball, and the $file_dir_name as operands to the tar/pax command.

Improved command:

...

#check if tar file exists already
cd "$dir_path"

if [ -f "$file_name" ]
then echo "$file_name" exists
else
  pax -w -f "$file_name" "$file_dir_name"
  ## alternatively, with tar:
  # tar cf "$file_name" "$file_dir_name"
fi

At this point, I'd like to point out the variable name $file_name is a bit too generic, it can be changed to something like $tarball.

As a bonus, you may consider shortenning your variable names:

  1. $wd is a good name for your expected working directory, it collides with neither cd nor pwd

  2. I often use just $f for files or just any "directory entry"; likewise, $d for directory operands.

0

The key here is that you must test for the same filename you are creating

#check if tar file exists already
if [ -f "$file_name" ]
then
        ...
else
        # create tar file 
        tar -cvf "$file_name" ...
        ...
fi

Additionally, there is no reason file_name can't be a full path.

file_name="$dir_path/$new_name.tar"

It is probably less error prone to put the directory into the variable name. It may be better style to call the variable tar_name or tar_path or something similar.

  • Thanks! but what is I want to create the tar file to a specific directory (ie. dir_path)? – Natalie Lloyd Oct 7 '20 at 17:46
  • @NatalieLloyd Is there anything that prevents you from running if [ -f "$dir_path/$new_name.tar" ]; then ...; else tar -cvf "$dir_path/$new_name.tar" ...; fi? This is the whole point of this Q/A: since you are not changing the working directory, you can, and probably should, simply use the same path expression when performing the -f test and when creating the tar archive. – fra-san Oct 7 '20 at 18:49
-1
#!/bin/bash
echo "enter the filename to be checked"
read k
o=$(find . -maxdepth 1 -type f -iname "$k"|wc -l)
if [ "$o" -eq 0   ]
then
echo "File $k doesnt exsists"
else
echo "File $k already exsists"
(required commands as per requirement)
fi

Used find command to perform operation

Python

#!/usr/bin/python
import sys
import os
p=raw_input("enter the file to be checked")
if os.path.isfile(p):
    print "{0} file already exsists".format(p)
else:
    print "{0} file doesnt exsists".format(p)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.