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.
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.
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.
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.
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.
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.
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/.
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.
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/).
The mv command is used in Linux and Unix-based systems to move (relocate) files or directories or rename them.
mv [options] source destinationsource: The file or directory to be moved or renamed.destination: The location where the file or directory should be moved, or the new name for renaming.
-
Move a file to a directory:
Example:
mv file.txt /home/user/Documents/
๐ Moves
file.txtto the/home/user/Documents/directory. -
Rename a file:
Example:
mv old_name.txt new_name.txt
๐ Renames
old_name.txttonew_name.txt. -
Move and rename a file:
Example:
mv file.txt /home/user/Documents/new_file.txt
๐ Moves
file.txtto/home/user/Documents/and renames it tonew_file.txt.
-
-i(Interactive Mode) ๐: Prompts before overwriting a file.Example:
mv -i old_file.txt /home/user/Documents/
๐ If
old_file.txtalready exists in the destination, confirmation is required before overwriting. -
-f(Force Mode) โก: Overwrites files without prompting.Example:
mv -f old_file.txt /home/user/Documents/
๐ If
old_file.txtexists at the destination, it is overwritten without confirmation. -
-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.
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.
rename 's/.txt$/.bak/' *.txtโ
Changes:
๐ file1.txt โ ๐ file1.bak
๐ notes.txt โ ๐ notes.bak
rename 'y/A-Z/a-z/' *โ
Changes:
๐ File.TXT โ file.txt
๐ Document.DOC โ document.doc
rename 's/^/old_/' *.jpgโ
Changes:
๐ผ๏ธ photo.jpg โ old_photo.jpg
๐ผ๏ธ image.jpg โ old_image.jpg
rename 's/ /_/g' *โ
Changes:
๐ my file.txt โ my_file.txt
๐ new document.doc โ new_document.doc
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
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