Skip to content

Latest commit

ย 

History

History
250 lines (183 loc) ยท 6.52 KB

File metadata and controls

250 lines (183 loc) ยท 6.52 KB

Linux Commands: cp and mv ๐Ÿš€

1. cp Command (Copy Command) ๐Ÿ“‚

The cp command is used to copy one or more files from one location to another. It allows copying files from a single or multiple sources to a single destination.

Basic cp Command Example โœจ

You can copy a file to another file using the cp command.
Example:

$ cp Nikhilpatidar1 nikhilpatidar2

๐Ÿ“Œ This command will copy the file Nikhilpatidar1 and save it as nikhilpatidar2.


cp -v Command (Verbose) ๐Ÿ”

Using the -v (verbose) option, you can get detailed output of files being copied.
Example:

$ cp -v nikhil Patidar indore d1/

๐Ÿ“Œ This command copies the files nikhil, Patidar, and indore into the d1 directory while displaying details of the operation.


Copying Multiple Files to a Single Destination ๐Ÿ“

You can copy multiple source files to a single destination using the cp command.
Example:

$ cp -v /etc/password /etc/group /etc/gsadow root/desktop/d1/

๐Ÿ“Œ This command copies /etc/password, /etc/group, and /etc/gsadow files to the root/desktop/d1/ directory.


Copying All Files from a Directory ๐Ÿ“‚

The command cp -v /var/log/* root/desktop tries to copy all files from a directory but does not copy subdirectories.
Example:

$ cp -v /var/log/* root/desktop

๐Ÿ“Œ This command copies all files from the /var/log/ directory to root/desktop, but not the directories inside.


Copying with Recursive Option (-r) ๐Ÿ”„

If you need to copy a directory along with its subdirectories, use the -r (recursive) option.

Example:

$ cp -vr /var/log/* .

๐Ÿ“Œ This command copies all files and directories from /var/log/ to the current directory (.).
๐Ÿ“ข The -v (verbose) option provides details of each copied file and directory.


Copying Files to a Specific Directory ๐Ÿ“ค

The . symbol represents the current directory. If used as the destination, files will be copied to the current directory.

Example:

$ cp -vr /var/log/* ./desktop/d4/

๐Ÿ“Œ This command copies all files and directories from /var/log/ to ./desktop/d4/.


Copying to the Root Directory ๐Ÿ 

If you want to copy files to the root directory (/), root permissions are required.

Example:

$ cp -vr /var/log/ /

๐Ÿ“Œ This command copies all files and directories from /var/log/ to the root directory.


Copying to the Parent Directory Using .. ๐Ÿ”™

The .. symbol represents the parent directory. It is used to copy files one level up.

Example:

$ cp -vr /etc/passed ../tmp/

๐Ÿ“Œ This command copies /etc/passed to the parent directory (../tmp/).


2. mv Command (Move Command) ๐Ÿšš

The mv command is used in Linux and Unix-based systems to move (relocate) files or directories or rename them.

Syntax:

mv [options] source destination
  1. source: The file or directory to be moved or renamed.
  2. destination: The location where the file or directory should be moved, or the new name for renaming.

Basic Usage: ๐Ÿ“

  1. Move a file to a directory:

    Example:

    mv file.txt /home/user/Documents/

    ๐Ÿ“Œ Moves file.txt to the /home/user/Documents/ directory.

  2. Rename a file:

    Example:

    mv old_name.txt new_name.txt

    ๐Ÿ“Œ Renames old_name.txt to new_name.txt.

  3. Move and rename a file:

    Example:

    mv file.txt /home/user/Documents/new_file.txt

    ๐Ÿ“Œ Moves file.txt to /home/user/Documents/ and renames it to new_file.txt.


Additional Options: โš™๏ธ

  1. -i (Interactive Mode) ๐Ÿ›‘: Prompts before overwriting a file.

    Example:

    mv -i old_file.txt /home/user/Documents/

    ๐Ÿ“Œ If old_file.txt already exists in the destination, confirmation is required before overwriting.

  2. -f (Force Mode) โšก: Overwrites files without prompting.

    Example:

    mv -f old_file.txt /home/user/Documents/

    ๐Ÿ“Œ If old_file.txt exists at the destination, it is overwritten without confirmation.

  3. -u (Update Mode) ๐Ÿ”„: Moves the file only if the source is newer or the destination file does not exist.

    Example:

    mv -u file.txt /home/user/Documents/

    ๐Ÿ“Œ The file is moved only if the source is newer than the existing destination file or if the destination file does not exist.


Here's your updated version with more emojis for better readability! ๐Ÿ˜Š


The rename command in Linux is used to rename multiple files at once using a pattern. Unlike mv, which renames a single file at a time, rename is useful for batch renaming.

๐Ÿ“Œ Syntax:

rename [options] 's/old_pattern/new_pattern/' files
  • ๐Ÿ” s/old_pattern/new_pattern/ โ†’ This is a Perl-style substitution expression.
  • ๐Ÿ“‚ files โ†’ The list of files to rename.

๐Ÿ“Œ Examples:

1๏ธโƒฃ Rename .txt files to .bak

rename 's/.txt$/.bak/' *.txt

โœ… Changes:
๐Ÿ“„ file1.txt โ†’ ๐Ÿ“ file1.bak
๐Ÿ“„ notes.txt โ†’ ๐Ÿ“ notes.bak


2๏ธโƒฃ Convert all filenames from UPPERCASE to lowercase

rename 'y/A-Z/a-z/' *

โœ… Changes:
๐Ÿ“„ File.TXT โ†’ file.txt
๐Ÿ“„ Document.DOC โ†’ document.doc


3๏ธโƒฃ Add a prefix to all .jpg files

rename 's/^/old_/' *.jpg

โœ… Changes:
๐Ÿ–ผ๏ธ photo.jpg โ†’ old_photo.jpg
๐Ÿ–ผ๏ธ image.jpg โ†’ old_image.jpg


4๏ธโƒฃ Replace spaces with underscores in filenames

rename 's/ /_/g' *

โœ… Changes:
๐Ÿ“‚ my file.txt โ†’ my_file.txt
๐Ÿ“‚ new document.doc โ†’ new_document.doc


๐Ÿ›  Alternative: mmv Command

If rename is not installed, you can use mmv:

mmv "*.txt" "#1_backup.txt"

โœ… Changes:
๐Ÿ“„ file1.txt โ†’ file1_backup.txt
๐Ÿ“„ notes.txt โ†’ notes_backup.txt


๐Ÿ“ฅ Installing rename

If rename is not available, install it:

  • ๐Ÿง Debian/Ubuntu:
    sudo apt install rename
  • ๐ŸŽฉ CentOS/RHEL:
    sudo yum install prename
  • ๐Ÿน Arch Linux:
    sudo pacman -S perl-rename
โšก