Beyond the abilitiy to navigate through your file structure which is covered here There are three basic functions needed when interacting with your files and directories;
mv – move (or rename) files or directories.
cp – copy files or directories.
rm – remove files or directories.
Table of Contents
mv
The mv command is how we can move a file or directory to another location within the file structure. The syntax is simply: mv $source $destination so if I wanted to move a file named test.file in my working directory, I could simply use the command; mv test.file /path/to/destination/. Similarly, I can specify the full path of the file to interact with files outside of my working directory. For example, let’s say I wanted to move the same file back to my working directory, the command would become mv /path/to/destination/test.file /path/to/working/directory/.
In linux systems, there is a shortcut of . which represents the working directory, similarly to how .. represents the relative parent directory. We can use this to make the above example more intuitive, such as: mv /path/to/destination/test.file . to perform the same operation without having to input a full path.
The mv command can also be used to rename a file or directory by stipulating a different name in the $destination variable. For example, if I wanted to rename test.file and not move it, I would simply navigate to the directory it is stored then and use: mv test.file renamed.file.
The two functions can be combined. If I wanted to move test.file from it’s current directory and also rename it the command could be as follows: mv /path/to/location/test.file /path/to/destination/renamed.file.
cp
There are many instances where I might want to create a copy of a file or directory instead of simply moving it. For these instances, I can use the exact same syntax but replace the mv command with cp. If I want to make a copy of test.file I can simply use cp /path/to/location/test.file . which will create an exact copy, name it test.file and store in my working directory.
Similarly, if I wanted to create a copy and then rename it, I do not need to combine the two commands. I can simply define the new name in the cp command. Let’s use cp /path/to/location/test.file ./copied.file which will make a copy of the contents of test.file and store them in a file named copied.file in my working directory.
Although the syntax does not change, if copying a directory, you will need to add the -r operator to your command to copy all the directory contents, for example; cp -r /original/path/to/source /different/path/to/destination would copy the source directory to the /different/path/to/ file tree and rename it destination.
rm
The rm command deletes the files or directories specified. It’s important to note that there is no “recover” command so it’s always a good idea to check which directory you’re working in with the pwd command before deleting things. In the context of your webserver, your host should be taking regular backups so it is important to familiarize yourself with this process before deleting things when using the command line.
The command itself is very simple, if I wanted to delete test.file and it was stored in /path/to/directory/, I could input rm /path/to/directory/test.file from anywhere in the file structure. Similar to cp if deleting a directory, you must use the -r operator.
You can also use the -f operator to force the deletion of all files, regardless of prompts etc. and use the wildcard * to delete all the contents of a directory. Let’s say you had a trash/ directory which you had been simply mv‘ing to for months/years without any kind of sorting. Eventually, the storage became an issue and you just wanted to clear it out but leave the directory itself intact. You could use rm -rf /path/to/trash/* to forcibly remove any and all contents. NOTE: I would not recommend getting in the habit of adding -f to your rm commands but did want to include it for the purpose of this article.
Read more:
Shortcuts and Wildcards in Linux.
https://explainshell.com/explain/1/mv