This is the 12th post of a series for teaching cyber-security in a coding-club. Read [part 11]
Copying files
I need the same file in different directories
As we have learned, bash commands allow us to move and rename files and directories.
You may be wondering: is it possible to copy them? Yes it is!
Introducing the cp
command
Remember our test-website folder?
Let's perform a slightly more complex command in order to demonstrate bash's cp
command power.
Make sure you are on the ~/mystuff
directory and execute:
cp style.css mystuff/test-styles.css
Not only did the cp
command copy the ~/mystuff/style.css
file together with its content, but also changed its name:
style.css file content:
/*
* I have some CSS content!
*/
test-styles.css file content:
/*
* I have some CSS content!
*/
How would you look inside the file style.css
? Remember it has something to do with a cat !
Copying directories
Directories can be copied and renamed too
Let's copy some ~/mystuff
directories into the test-website
directory.
Execute the following command from the ~/mystuff directory path:
cp -R config test-website
NOTE: The cp -R
option needs to be specified when working with non-empty directories. It tells the copy
command to copy the stuff inside the directory as well.
Command: cp <option> <source> <destination>
Definition: The
cp
command copies files and directories from an existing source file or directory name to an existing destination directory name.
Additionally, the destination path can include a file name to be assigned instead of the existing one without altering its content.
NOTE: Destination directories must exist in order for the command to work.
Removing
The remove (delete) command
This command is both important and dangerous!
At some point you may need to delete files. For example, if you can get into the server, you may want to write down the Bitcoin address (to receive the mili-Bitcoin) and then delete it so that nobody else can claim it before you!
The command to delete files (or directories) is called remove
:
Command: rm
Definition: The
rm
command orremove
command deletes a file. The option-r
will also remove directories. So for example,rm -r mystuff/
will remove the directorymystuff
and everything inside it.
WARNING: This command will not ask are you sure?
or keep a copy in the bin.
In the shell rm
means REMOVE, period. GONE! FOREVER. You cannot undo or recover the file. If you use the option rm -r folder/
it will delete everything in that folder including programs, drivers, files, EVERYTHING!!
Go on. Go back to your home directory:
cd ~/
And delete the mystuff
folder!
Ok, that was some serious stuff. Let's do something more lighthearted in [Part 13].