livialima / linuxupskillchallenge

Learn the skills required to sysadmin a remote Linux server from the commandline.
https://linuxupskillchallenge.org/
Other
1.73k stars 226 forks source link

Day 16 - feature request [always use -f as last argument with tar while creating archive] #51

Closed kmmon closed 3 months ago

kmmon commented 7 months ago

Using -f with tar while creating archive is essential. The document uses the following:

tar -cvzf myinits.tgz /etc/init.d/

I accidently used tar -cvfz ssh.tgz /etc/ssh and the the archive file named z

~ # tar -cvfz hello.tgz /etc/ssh/
tar: hello.tgz: Cannot stat: No such file or directory
tar: Removing leading `/' from member names
/etc/ssh/
/etc/ssh/ssh_host_ecdsa_key
tar: Removing leading `/' from hard link targets
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub
/etc/ssh/ssh_import_id
/etc/ssh/sshd_config.d/
/etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/sshd_config
/etc/ssh/moduli
/etc/ssh/ssh_config
/etc/ssh/ssh_config.d/
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ed25519_key
tar: Exiting with failure status due to previous error

~ # file z
z: POSIX tar archive (GNU)

So, I think we can add a NOTE that the -f considers anything after -f as the name of the archive that needs to be created. So, we should always use -f as the last flag while creating an archive.

matt-kita commented 5 months ago

I know this one is already addressed and closed, but I just have only one small remark to this statement: "So, we should always use -f as the last flag while creating an archive." - this is not necessarily true, as e.g. tar -f archive_name -czv *.txt would still work fine, and as you may have noticed -f is not the last flag used in this command. I'd say understanding the tar command usage is essential, and more important than enforcing any particular order of flags.

kmmon commented 5 months ago

Thanks for the pointer @matt-kita I agree with your suggestion above.

I think we can rephrase the sentence that -f flag should not be mixed with other flags and if you're mixing it in an automated script then consider keeping -f as last given what comes next is the argument of -f flag.

livialima commented 3 months ago

@kmmon I've updated the note about -f in a previous commit. Thanks for all the input!