-1

I need an .sh shell/bash script that will automatically tar a directory with the same name of the directory when I move the target directory into another directory (destination directory),

So for example I have directory /home/user/MyFiles/ and I move /MyFiles/ dir inside of /home/user/new/, I need it to create MyFiles.tar inside /home/user/new/ with every file inside /home/user/MyFiles included in the MyFiles.tar.

EDIT: Something I forgot to mention is the directories that I need to .tar sometimes contain the characters () in the directory name so I need the script to take that into account, because I know it gives an error if you don't use the quotes in the tar command and () characters are present, for example tar -cvf "Example-(folder).tar" "Example-(folder)"

closed as too broad by Christopher, Stephen Rauch, Timothy Martin, mdpc, Scott Sep 14 '17 at 1:06

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Are you asking for a script to move the directory and create the archive, or are you asking for a service which will watch a directory for newly-created directories and act upon them? – DopeGhoti Sep 13 '17 at 18:34
  • I'm asking about the first one that you said but now that you mention it. The second script you mentioned would help me a lot as well. So both? Sorry if it's too much to ask. – skar Sep 13 '17 at 18:36
  • Right now I am manually doing the command tar -cvf directory_name.tar directory_name and then I am moving them it over to the destination folder. I would like to just drag a directory into another directory and have it create the tar archive automatically, with the same directory name. – skar Sep 13 '17 at 18:39
  • Something I forgot to mention is the folders that I need to .tar sometimes contain the characters () so I need the script to take that into account, because I know it gives an error if you don't use the quotes in the tar command, for example tar -cvf "Example-(folder).tar" "Example-(folder)" – skar Sep 13 '17 at 18:48
  • Linux don't have folders but directories – Basile Starynkevitch Sep 13 '17 at 18:58
1

You probably want to use inotify(7) facilities (assuming a native, non-networked, file system like ext4 etc...)

You probably want incron. See e.g. this. I guess that using incron for IN_CREATE and IN_MOVED events should often be enough. See incrond(8) and incrontab(5)

But you should explain your motivations and high-level goals (your question smells badly like some XYproblem). It seems you want some version control system (or perhaps some backup?). I recommend git

Read more about inodes. Remember that a file (in fact, an inode in a filesytem) can have several paths (or none of them) and appear in several directories (e.g. using hard links, thru the link(2) system call and ln(1) command).

I need an .sh shell/bash script that will automatically tar a directory with the same name of the directory when I move the target directory into another directory (destination directory),

This goal is ambiguous (because a file -which is really an anonymous inode- can have several names and appear in several directories; also a file can be written, created, deleted, truncated and/or read simultaneously by several processes).

Perhaps you might run some other script periodically (e.g. with crontab(5), maytbe some cron job running some make or ninja command which takes into account the modification time of inodes ....)

BTW, this site is not a "write my script" service. You should expect hints (like above), not other people to code your own script.

I recommend reading Advanced Linux Programming to understand more your Linux system (from the inside point of view).

P.S. Characters like ( in filenames are not an issue (use appropriately double quotes and braces around variable names in your shell scripts). But files and file systems on Linux don't behave like e.g. on Windows. Understanding the notion of inode is essential.

  • No. It's my server. I just want to drop a dir, into another dir and have the moved dir become a .tar file with the original dir name as the .tar – skar Sep 13 '17 at 18:57
  • Notice that folders don't really exist on Linux (they are directories), and that a file is mostly an inode (and can have several names, i.e. belong to several directories) – Basile Starynkevitch Sep 13 '17 at 19:00
  • You need to edit your question to motivate it. – Basile Starynkevitch Sep 13 '17 at 19:01
  • Yup. I changed folders to directories, to make it more clear. Thanks. – skar Sep 13 '17 at 19:02
  • But that is not clear enough. You forgot that a file can have several names or paths (or even none of them). – Basile Starynkevitch Sep 13 '17 at 19:03

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