HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page
  • Copying Files
  • Moving and Renaming Files
  • Removing Files

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Getting to Know Linux
  5. Navigating the Linux File System (LFS)

Copying, Moving, and Removing Files

Commands for moving, copying, and deleting files are fairly straight forward:

  • cp - Used to copy a file

  • mv - Used to move (or rename) a file

  • rm - Used to remove (delete) a file

These commands can be used to act on individual files and directories, or recursively to act on many files and directories at once.

Copying Files

Here are some examples of using the cp command to copy files from one location to another:

Command

Result

cp abc def

Copies abc to the new name def in the same directory

cp abc ~

Copies abc to your home directory (~), retaining the name abc

cp -r /usr/share/doc/bash-completion* /tmp/a/

Copies the bash-completion directory, and all of the files it contains, to /tmp/a/

On copying, current date/time stamps are used, and permissions are determined by your umask

cp -ra /usr/share/doc/bash-completion* /tmp/b/

Copies the bash-completion directory, and all of the files it contains, to /tmp/b/

Thanks to the archive (-a) option, the date/time stamps and permissions are unmodified by the copy, and retain the details of the original files

The cp command is typically aliased with the -i option in order to prevent you from inadvertently overwriting files.

Moving and Renaming Files

To move or rename a file, we leverage the mv command. When we "rename" a file in Linux, what we are really doing is moving the file to the same directory it currently sits in, and calling the newly moved file by a new name. This gives the illusion that the original file has simply been modified, when this is not technically accurate.

The following table has some examples of the mv command in action:

Command

Result

mv abc def

Renames abc to the new name def in the same directory

mv abc ~

Moves abc to your home directory (~), retaining the name abc

mv /home/chris/mymemos/ /home/chris/Documents/

Moves the/home/chris/mymemos directory (and all its contents) to the /home/chris/Documents directory

By default, the mv command overwrites any existing files if the file to which you are moving 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 files. Here’s how to check if that is true on your system:

$ alias mv

Removing Files

Here are some examples of the rm command:

Command

Result

rm abc

Deletes the abc file

rm *

Removes all of the files in the current directory; 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, or use the alternative command rmdir - not that the directory must be empty for this to work.

Consider the following examples:

Command

Result

rmdir /home/chris/nothing

Only removes the directory (nothing) if it is empty

rm -r /home/chris/bigdir

Removes the directory bigdir and all of its contents (files and multiple levels of subdirectories), but it prompts you before each is removed

rm -rf /home/chris/hugedir

As before, but minus the prompts - make sure you are SURE!

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.

PreviousChange File Ownership with chownNextFinding Files

Last updated 1 year ago

Was this helpful?