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:
How to stop command
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 "."

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum