Menu Close

Moving, Copying, and Removing Files in Linux..-..Fcukthecode

Commands for moving, copying, and deleting files are fairly straightforward.

To change the location of a file, use the mv command.
To copy a file from one location to another, use the cp command.
To remove a file, use the rm command.

Moving Files/Directories

These commands can be used to act on individual files and directories or recursively to act on many files and directories at once. Here are some examples:

$ mv  code  thedef
$ mv  thedef  ~
$ mv  /home/Desktop/anime/  /home/Videos/

The first mv command moves the file “code” to the file “thedef” in the same directory
(essen-tially renaming it), whereas

The second moves the file thedef to your home directory ( ~ ).
The next command moves the anime directory (and all of its contents) to the
/home/Videos directory.

By default, the mv command overwrites any existing files if the file you are moving to exists. However, many Linux systems alias the mv command so that it uses the -i option
(which causes mv to prompt you before overwriting existing fi es). Here’s how to check if that is true on your system:

$ alias mv
alias mv='mv -i'
Illustration using python 3 CLI (for mv command)

Copying Files/Directories

Here are some examples of using the cp command to copy files from one location to another:
# # # # Assuming we are inside the Home Directory as usual when terminal opens. : )

$ cp  code  thedef
$ cp  code  ~
$ cp  -r  Desktop/anime/konosuba*  Videos/anime_1/
$ cp  -ra  Desktop/anime/konosuba*  Desktop/college/

The first copy command (cp) copies file “code” to the new name “thedef” in the same directory,
whereas

The second copies file code to your home directory ( ~ ), keeping the name code.
The two recursive (-r) copies copy the konosuba directory, and all files it contains, first to new Videos/anime_1/ and Desktop/college directories.

If you run ls -l on those two directories with the archive (-a) option, the date/time stamps and permissions are maintained by the copy, but without the -a, current date/time stamps are used and permissions are determined by your umask.

The cp command typically also is aliased with the -i option, to prevent you from accidentally overwriting files.

Illustration using python 3 CLI (for cp command)

Removing Files/Directories

As with the cp and mv commands, rm is also usually aliased to include the -i option.

This can prevent the damage that can come from an inadvertent recursive remove (-r) option. Here are some examples of the rm command:

$ rm  code1
$ rm  *

The first remove command deletes the code1 file;
The second removes all the files in the current directory (except that it doesn’t remove directories and or any files that start with a dot).

If you want to remove a directory, you need to use the recursive (-r) option to rm or, for an empty directory, you can use the rmdir command. Consider the follow- ing examples:

$ rmdir  /home/Desktop/anime/
$ rm  -r  /home/Document/pdf/
$ rm  -rf  /home/Videos/Fallen/

The rmdir command in the preceding code only removes the directory (anime) if it is empty.

The rm -r example removes the directory pdf and all its contents (files and multiple levels of sub-directories), but will prompt you before each is removed.

By adding the force option ( -f ) with ( -r ), the Fallen directory and all its contents are immediately removed, without prompting.

Illustration using python 3 CLI (for rm command)

IMPORTANT

Any time you override the -i option on the mv, cp, and rm commands, you risk removing some (or lots) of files by mistake. Using wildcards (such as * ) and no
-i makes mistakes even more likely. That said, there are times when you don’t want to be bothered to step through each file you delete.

You have other options:

■ As noted with the -f option, you can force rm to delete without prompting. An alternative is to run rm, cp, or mv with a backslash in front of it ( \rm animedir ). The backslash causes any command to run unaliased.

■ Another alternative with mv is to use the -b option. With -b, if a file of the same name exists at the destination, a backup copy of the old file is made before the new file is moved there.

ILLUSTRATION USING PYTHON3 CLI ( ALL COMMANDS EXECUTED )

Executed using python3