The Linux command-line tool mv can be used for 2 purposes. One of them is moving files/directory, another is renaming a file/directory. As for now, we are in the directory : /etc
How to use mv?
# syntaxmv<SOURCE><DESTINATION>##### create a directory mkdirtestdir##### create a filetouchtestfile.txt##### create some content for the fileecho"hello, this is a simple example of the linux command mv">testfile.txt##### examplesmvtestdir/root# succes, move the directory "testdir" from /etc/testdir to /root/testdirmv./testdir/root# succes, move the directory "testdir" from /etc/testdir to /root/testdirmv/testdir/root# Fail, since the directory is specified from the root directory (/)# filemvtestfile.txt/root/testdir# moves the file testfile.txt to the directory /root/testdir/testfile.txtmv./testfile.txt/root/testdir# moves the file testfile.txt to the directory /root/testdir/testfile.txtmv/testfile.txt/root/testdir# Fail, since the directory is specified from the root directory (/)# directory and filemv/root/testdir/etc# move the directory and file from /root to the directory /etc# Extramv -n <source> <destination> # when using the -n option, it will NOT overwrite files that are equal to each other (by file or directory name)
Rename a file or directory
Some examples of renaming a file or a directory will be shown here. as said above, the mv command line tool can be used for these 2 purposes. Moving and renaming.
# syntaxmv<source><destination># create a directory mkdirtestdir# create a filetouchtestfile.txt# create some content for the fileecho"The simpsons has had many predictions that....">testfile.txtmvtestfile.txtsimpsons.txt# renames the file from testfile.txt to simpsons.txtmvtestdirsimpsonsdir# renames the directory from testdir to simpsonsdir
What is cp?
cp is used to copy files, this can be done in the same directory or to copy files/directories to another directory.
the syntax of the cp command is equal to the mv command. The only thing it differs in, is that it cannot copy a directory recursive without extra parameters, this means that it will not automatically copy all the files that are in a directory. Some parameters are needed to make use of this functionality. In the commands that are provided underneath, some examples will be shown.
How to use cp
# syntaxcp<source><destination># create a directory mkdirtestdir# create a filetouchtestfile.txt# create some content for the fileecho"The simpsons has had many predictions that....">testfile.txtcptestdir/root# success, directory "testdir" is now present in the /etc and in the /root dircp/etc/testdir/root# success, directory "testdir" is now present in the /etc and in the /root dircp./testdir/root# success, directory "testdir" is now present in the /etc and in the /root dircp testfile.txt /root/testdir # success, the file testfile.txt is now present in the directory /root/testdir/testfile.txt
cp /etc/testfile.txt /root/testdir # success, the file testfile.txt is now present in the directory /root/testdir/testfile.txt
cp ./testfile.txt /root/testdir # success, the file testfile.txt is now present in the directory /root/testdir/testfile.txt
cp/testdir/root#fail, the root dir does not contain the directory "testdir"cpetc/testdir/root#fail, the root dir is not specified (/)# copying a directory with files and other maps within the directorycp-r<source><destination># copy a directory with all files/directories in it (recursive)cp -r -v <source> <destination> # copy a directory with all files/directories in it (recursive) and output all steps to the terminal
What is rm?
remove does what it says, it removes files, directories, directories with files etc. rm is implemented in every distribution of Linux.
How to use rm
# syntaxrm<file>rm-d<emptydirectory>rm-rd<filleddirectory>rm-rf<anything># NOTE THAT THIS IS DANGEROUS!# create a directory mkdirtestdir# remove it with rmrm-dtestdir# create a testfiletouchtestfile.txt# remove it with rmrmtestfile.txt# create dir and testfile in the dirmkdirtestdirtouchtestdir/testfile.txtrmtestdir# fail, directory is filledrm-dtestdir# fail, directory is filledrm-rdtestdir#success,directoryandcontentsaredeletedrm-rftestdir#success,directoryandcontentsaredeleted
How to fuckup your Linux system
In order to fuck up your whole linux system (something you should never do unless it is for testing purposes), use the following command. This command will delete the entire root system.
# ! NEVER DO THIS UNLESS IT IS FOR TESTING PURPOSES!rm-rf--no-preserve-root/# ! NEVER DO THIS UNLESS IT IS FOR TESTING PUPROSES!