LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Tar returns: Is a directory (https://www.linuxquestions.org/questions/programming-9/tar-returns-is-a-directory-4175636120/)

m0rt 08-10-2018 03:31 PM

Tar returns: Is a directory
 
Hi, I'm learning to use "tar" and when I run my script it sends me this error:

Code:

Fecha=`date +'%Y%m%d%H%M%S'`

`tar -cvPf /tmp/resp_${USER}_${Fecha}.tar /home/${USER}`

Code:

is a directory
I don't know what the cause is since I run it from the terminal and here it doesn't mark this message.

jsbjsb001 08-10-2018 04:15 PM

Quote:

Originally Posted by m0rt (Post 5890318)
Hi, I'm learning to use "tar" and when I run my script it sends me this error:

Code:

Fecha=`date +'%Y%m%d%H%M%S'`

`tar -cvPf /tmp/resp_${USER}_${Fecha}.tar /home/${USER}`

Code:

is a directory
I don't know what the cause is since I run it from the terminal and here it doesn't mark this message.

It's telling you what the "cause" is. /home/${USER} is the currently logged in user's "home" folder/directory. It's that simple.

m0rt 08-10-2018 04:29 PM

Quote:

Originally Posted by jsbjsb001 (Post 5890325)
It's telling you what the "cause" is. /home/${USER} is the currently logged in user's "home" folder/directory. It's that simple.

So for it to work I have to disconnect, but I ran it on another server and I didn't make a mistake.

How can I make it work for me?

jsbjsb001 08-10-2018 04:30 PM

Type this into a terminal and read it:

Code:

man tar
(you need to specify a file at the end of the path)

m0rt 08-10-2018 04:40 PM

Quote:

Originally Posted by jsbjsb001 (Post 5890330)
Type this into a terminal and read it:

Code:

man tar
(you need to specify a file at the end of the path)

What I'm trying to do is to back up the users' folders I have on my server, and when they run it to generate the backup in the /tmp path, I'm learning to make scripts.

mina86 08-10-2018 04:44 PM

You probably don’t want -P. You also want to quote your variables. You probably want to use $( … ) instead of backticks.

Back to your problem, the issue is that you’ve surrounded tar execution in backticks. Get rid of them.

jsbjsb001 08-10-2018 04:51 PM

From tar's man page:

Code:

tar [OPTION...] [FILE]...
Once again, read it.

scasey 08-10-2018 04:54 PM

Quote:

Originally Posted by m0rt (Post 5890318)
I don't know what the cause is since I run it from the terminal and here it doesn't mark this message.

I'm unable to replicate the OPs problem. Their tar command creates a tar file of everything in the user's home directory with no "errors" or problems...

m0rt When you get the message, is the tar file not created?

m0rt 08-10-2018 05:05 PM

Quote:

Originally Posted by scasey (Post 5890341)
I'm unable to replicate the OPs problem. Their tar command creates a tar file of everything in the user's home directory with no "errors" or problems...

m0rt When you get the message, is the tar file not created?

Yes, it does generate the file for me but my doubt is because it sends me the error, because I ran this script in SUSE and it doesn't send me errors, but in Ubuntu I get the message.

m0rt 08-10-2018 05:10 PM

Quote:

Originally Posted by mina86 (Post 5890335)
You probably don’t want -P. You also want to quote your variables. You probably want to use $( … ) instead of backticks.

Back to your problem, the issue is that you’ve surrounded tar execution in backticks. Get rid of them.

the -P is for me to respect the absolute paths otherwise it sends me another error message of the character "/"

scasey 08-10-2018 05:32 PM

Quote:

Originally Posted by m0rt (Post 5890343)
Yes, it does generate the file for me but my doubt is because it sends me the error, because I ran this script in SUSE and it doesn't send me errors, but in Ubuntu I get the message.

I can see that would cause a question.
Couple more suggestions:
  1. Compare the version and the man pages on the two machines. Look for a difference in what they say about the [FILE]...
    (Although my man page seemed to say the last option had to be a file or list of files, but worked with ${USER} just fine.)
  2. List the contents of the tar file where you get the "error" (I think it's only a warning if it's creating the file) to confirm that it contains all it should...maybe create a test user so it's not some huge collection of files to check.

rknichols 08-10-2018 06:26 PM

Quote:

Originally Posted by m0rt (Post 5890318)
Code:

Fecha=`date +'%Y%m%d%H%M%S'`

`tar -cvPf /tmp/resp_${USER}_${Fecha}.tar /home/${USER}`
^                                                      ^
^                                                      ^

Code:

is a directory

What on Earth are those marked backtics supposed to be doing? What is happening is that the shell runs the tar command and then tries to interpret the verbose output as another command. The first line of that output will be the user's home directory, and the shell is complaining that a directory cannot be a command.

mina86 08-10-2018 06:51 PM

Quote:

Originally Posted by m0rt (Post 5890345)
the -P is for me to respect the absolute paths otherwise it sends me another error message of the character "/"

Yes, that’s because typically having absolute path names in tar archives is a bad idea and may be dangerous. What you want is:
Code:

tar -C /home -cvf "/tmp/resp-$USER-$Fecha.tar" "$USER"
or slightly less magical:
Code:

cd /home; tar -cvf "/tmp/resp-$USER-$Fecha.tar" "$USER"
Unless you really want full path in the archive in which case:
Code:

tar -C / -cvf "/tmp/resp-$USER-$Fecha.tar" "/home/$USER"
Quote:

Originally Posted by scasey (Post 5890349)
I can see that would cause a question.
Couple more suggestions:
  1. Compare the version and the man pages on the two machines.

No need for such tedious tasks. tar --version would very likely show that both systems use the same tar.

Quote:

Originally Posted by jsbjsb001 (Post 5890338)
From tar's man page:

Code:

tar [OPTION...] [FILE]...
Once again, read it.

Perhaps OP has read it and noticed:
Code:

      For  example,  the c option requires creating the archive, the v option
      requests the verbose operation, and the f option takes an argument that
      sets  the  name of the archive to operate upon.  The following command,
      written in the traditional style, instructs tar to store all files from
      the  directory /etc into the archive file etc.tar verbosely listing the
      files being archived:

      tar cfv a.tar /etc

Emphasis mine. So yeah…

(But what confused me the most is why people continued discussing this after I’ve pointed out the problem).

m0rt 08-10-2018 11:06 PM

Quote:

Originally Posted by mina86 (Post 5890366)
Yes, that’s because typically having absolute path names in tar archives is a bad idea and may be dangerous. What you want is:
Code:

tar -C /home -cvf "/tmp/resp-$USER-$Fecha.tar" "$USER"
or slightly less magical:
Code:

cd /home; tar -cvf "/tmp/resp-$USER-$Fecha.tar" "$USER"
Unless you really want full path in the archive in which case:
Code:

tar -C / -cvf "/tmp/resp-$USER-$Fecha.tar" "/home/$USER"

No need for such tedious tasks. tar --version would very likely show that both systems use the same tar.



Perhaps OP has read it and noticed:
Code:

      For  example,  the c option requires creating the archive, the v option
      requests the verbose operation, and the f option takes an argument that
      sets  the  name of the archive to operate upon.  The following command,
      written in the traditional style, instructs tar to store all files from
      the  directory /etc into the archive file etc.tar verbosely listing the
      files being archived:

      tar cfv a.tar /etc

Emphasis mine. So yeah…

(But what confused me the most is why people continued discussing this after I’ve pointed out the problem).

This happened to me to run my script. It came out as follows:
Code:

tar -C / -cvf "/tmp/resp-$USER-$Fecha.tar" "/home/$USER"
Output:
Quote:

tar: Removing leading `/' from member names
worked perfectly for me, thank you

mina86 08-11-2018 12:51 PM

Quote:

Originally Posted by m0rt (Post 5890406)
This happened to me to run my script. It came out as follows:
Code:

tar -C / -cvf "/tmp/resp-$USER-$Fecha.tar" "/home/$USER"
Output:


worked perfectly for me, thank you

Use home/$USER rather than /home/$USER (notice removal of leading slash) and the warning will go away.


All times are GMT -5. The time now is 11:08 AM.