How to stop/Interrupt cp or mv Linux or Unix command

How do I stop the cp command or mv command Linux or Unix command? What happens if the mv or cp command is interrupted on live system?

A user can stop or interrupt any command such as cp, mv and others by pressing CTRLc (press and hold ctrl key followed by c) during the execution of a command or bash/perl/python script. Another option is to use the kill command to kill process by PID:

Get process id for firefox by running the following command (open another terminal and type the command):
$ ps aux | grep -i firefox

USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
veryv            31241   2.8  9.0  5806852 752084   ??  S    11:08AM  31:35.18 /Applications/ -foreground
veryv            55647   0.0  0.0  2442020   1788 s002  S+    3:50AM   0:00.00 grep -i firefox

To kill PID # 31241 (from second column), run:
$ kill -15 pid
$ kill -TERM 31241
$ kill -15 31241

If everything is failed try passing the kill signal:
$ kill -9 31241
Verify that firefox has gone with the ps command along with the grep command:
$ ps aux | grep -i firefox
You can replace the firefox command with cp command or any other command of your choice.

More about CTRL-C

When you press CTRL-C the current running command or process get Interrupt/kill (SIGINT) signal. This signal means just terminate the process. Most commands/process will honor the SIGINT signal but some may ignore it. You can press Ctrl-D to close the bash shell or open files when using cat command. You can press Ctrl-Z to suspend the current foreground process running in bash shell.

What happens if cp command is interrupted?

If you interrupt the cp the already transferred files are copied from the source directory to the destination directory and rest of the operation is aborted.

What happens if mv command is interrupted?

From the thread:

If you move a directory on the same file system you only move the directory entry from one location in the file system to another one. E.g. mv /source/dir /target/dir will delete the directory entry of dir from /source and create a new one in /target. That’s done by one atomic system call (i.e. uninterruptable). The inode containing the directory entries of dir as well as the actual content of the directory itself is not affected.

If you move the directory from one file system to another the files are transferred one-by-one (as Ignacio mentions in his answer), i.e. if you interrupt the mv the already transferred files are removed from the source directory.

How to trap ctrl-c in Bash shell script

Use the trap command to trap/capture ctrl+c and disable it:

# trap ctrl-c and call ctrl_c()
trap message INT
function message() {
        echo "** CTRL-C pressed, cleaning up can be done in message()...."
        # command for clean up e.g. rm and so on goes below
echo "** Press CTRL-C to capture key."
for i in {1..100}; do
    # TODO: Insert work here #
    sleep 1
    echo -n "."

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts