move, copy, remove (mv, cp, rm)

What is move (mv)

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?

# syntax
mv <SOURCE> <DESTINATION>

##### create a directory 
mkdir testdir

##### create a file
touch testfile.txt

##### create some content for the file
echo "hello, this is a simple example of the linux command mv" > testfile.txt

##### examples
mv testdir /root # succes, move the directory "testdir" from /etc/testdir to /root/testdir
mv ./testdir /root # succes, move the directory "testdir" from /etc/testdir to /root/testdir

mv /testdir /root # Fail, since the directory is specified from the root directory (/)
# file
mv testfile.txt /root/testdir # moves the file testfile.txt to the directory /root/testdir/testfile.txt
mv ./testfile.txt /root/testdir # moves the file testfile.txt to the directory /root/testdir/testfile.txt

mv /testfile.txt /root/testdir # Fail, since the directory is specified from the root directory (/)
# directory and file
mv /root/testdir /etc # move the directory and file from /root to the directory /etc

# Extra
mv -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.

# syntax
mv <source> <destination>

# create a directory 
mkdir testdir

# create a file
touch testfile.txt

# create some content for the file
echo "The simpsons has had many predictions that...." > testfile.txt

mv testfile.txt simpsons.txt # renames the file from testfile.txt to simpsons.txt
mv testdir simpsonsdir # 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

# syntax
cp <source> <destination>

# create a directory 
mkdir testdir

# create a file
touch testfile.txt

# create some content for the file
echo "The simpsons has had many predictions that...." > testfile.txt

cp testdir /root # success, directory "testdir" is now present in the /etc and in the /root dir
cp /etc/testdir /root # success, directory "testdir" is now present in the /etc and in the /root dir
cp ./testdir /root # success, directory "testdir" is now present in the /etc and in the /root dir

cp 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"
cp etc/testdir /root #fail, the root dir is not specified (/)

# copying a directory with files and other maps within the directory

cp -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

# syntax
rm <file>
rm -d <empty directory>
rm -rd <filled directory>
rm -rf <anything> # NOTE THAT THIS IS DANGEROUS!

# create a directory 
mkdir testdir

# remove it with rm
rm -d testdir

# create a testfile
touch testfile.txt

# remove it with rm
rm testfile.txt

# create dir and testfile in the dir
mkdir testdir
touch testdir/testfile.txt

rm testdir # fail, directory is filled
rm -d testdir # fail, directory is filled
rm -rd testdir# success, directory and contents are deleted
rm -rf testdir# success, directory and contents are deleted

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!

Last updated