2

I want a bash script that copies all the folders and files in the desktop to a folder named 'new' in desktop. Suppose , whenever I make a file in Desktop that file should be automatically be moved to the folder named 'new' in Desktop.

2

You can use the handy tool inoticoming to monitor your ~/Desktop folder for incoming files and automatically perform an action on each event, like moving them to a different location.

Note that this command only pays attention to created or modified files only.
It will ignore when you create a new directory in there. It will also ignore files created or modified in any subdirectories.

I tried making a command that also reacts on directories, but this seems impossible unless you are okay with the side effect that copying directories into the watched location fails randomly because inotifywatch moves the target directory before copying of all files has completed.

inoticoming does not come preinstalled with Ubuntu, but you can simply install it by once running

sudo apt-get install inoticoming

Then you type this command below and see the magic happen:

inoticoming --foreground --initialsearch ~/Desktop --chdir ~/Desktop mv -- '{}' ~/Desktop/new  \;

Parameters explained:

  • --foreground makes the tool run in foreground and print log information to the terminal whenever it does something. This is good for initial testing and debugging.

    Once you're sure it does what you want, you can omit this argument and let it fork a background daemon, the command will return immediately then and produce no output. You would do that if you want to autostart the command e.g. during login. It might make sense to configure logfiles in that case, please see man inoticoming for more information about that.

  • --initialsearch performs an additional scan of all files in the directory that are already present when you start the command. Without it, only files that are created or modified while it is running are processed.
  • ~/Desktop the directory to watch
  • --chdir ~/Desktop change the working directory to ~/Desktop before performing the specified action each time when a file appears or gets modified
  • mv -- '{}' ~/Desktop/new \; the actual action command you want to call on each event. {} will be replaced with the name of the file that triggered the event. Note how it is enclosed in single quotes and preceded by -- to protect it from getting split up or being interpreted by a shell or as keyword argument of mv. The trailing \; denotes the end of the action command and is required.

When inoticoming is running in foreground mode, you can exit it by hitting Ctrl+C in the terminal.

To kill its background daemons if you launched it without --foreground, the simplest method is to run killall inoticoming. Beware that this kills all running instances of inoticoming. If you want to make sure you kill only one specific instance, it might make sense to let it store its PID (process ID) in a file. See man inoticoming for more information about that.

  • Thank you so much for such an amazing piece of answer . You rock , dude . God bless you ! You deserve an upvote . – codedbrain Dec 6 '16 at 3:56
  • @codedbrain If this answer solved your question, please consider accepting it by clicking the grey round check button on the left of it. You can learn more about how this site works if you invest only two minutes into taking the short tour. – Byte Commander Dec 6 '16 at 15:21

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.