Moving, Copying and Deleting files

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.

Here are some examples:

$ mv abc def
$ mv abc ~
$ cp abc def
$ cp abc ~
$ rm abc
$ rm *

Of the two move (mv) commands, the first moves the file abc to the file def in the same
directory (essentially renaming it), whereas the second moves the file abc to your home
directory (~).

The first copy command (cp) copies abc to the file def, whereas the second
copies abc to your home directory (~).

The first remove command (rm) deletes the abc file, the second removes all the files in the current directory (except those that start with a dot).

NOTE: For the root user, the mv, cp, and rm commands are aliased to each be run with the -i option.

This causes a prompt to appear asking you to confirm each copy and removal, one file at a time. For file moves, the -i option will prompt you if the move would overwrite a file, but you may still
unintentionally move a file, so be careful. This is done to prevent the root user from messing up a large group of files by mistake.

To temporarily get around an alias, type the full path to the command (for example,
/bin/rm -rf /tmp/junk/*).
how to copy file:
# cp command is used to copy a file
syntax # cp <source> <destination>
Example 1 : cp /root/file1 /usr
This command will copy file1 file to /usr directory with same name.
Example 2 :  cp /root/file1 /root/one/two/three/file123
this command will copy file1 file to three directory with file123 name.
Example  3 : # cp a* /var
This command will copy all files starting with the alphabet a to /var directory.
By default cp command will not copy directory we have to use -r (recursively) switch to copy directory
Example 4 : cp -r /root/dir2 /usr
This command will copy dir2 directory to /usr location.
Example 5 : # cp -r /root/dir1/* /var
This command will copy all the contents of dir1 directory to var directory

Note:- we can use wildcard characters

* means multiple characters
? means single character
To delete a file:
rm command is used to delete a file/directory
syntax # rm < file name >
Ex 1: # rm /root/file1
it will ask for confirmation, press y to delete file
Ex 2: # rm -f /root/file2
it will not ask for confirmation
to remove a directory also we have to use -r option
Ex 3: # rm -r /root/dir1
this command will delete dir1 directory with all its contents,
with confimation.
Ex 4: # rm -rf /root/dir2
this command will delete dir2 directory with all its contents,
without confimation. f for (forcefully)

rmdir
  –  to delete an empty dir

syntax –  rmdir < dir name>

Example 1: # rmdir one error…… Directory not empty
If the directory is not empty u will get this message.

Example 2 :  rmdir one/two/three
Example 3 :  mrmdir one/two
Example 4 :  rmdir one

mv – to move file/dir this command is also used to rename file/dir
syntax – mv < source > < destination >
Example 5 :  mv /root/file1 /boot
this comamnd will cut file1 file and paste it in /boot directory
Example 6 :  mv /root/file2 /root/newfile
this command will rename file2 file to newfile

Note : if we provide a newfile name it will rename a file/dir

If we provide a existing file name or directory name, it will move the file.

Nautilus is a tool with which we can check the files/dirs in GUI mode. run the command in terminal in gui mode.
# nautilus /usr To list the contents of usr dir in gui mode.
# nautilus / To list the contents of / dir in gui mode.
# nautilus & will list the contents of pwd.

Author: Khaja Ehteshamuddin

I am a Linux admin who believes in Hard work, Likes to share knowledge with others. At present I work as Sr. System Administrator at Hyderabad

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s