Deleting, Moving and Copying
Shivm Soaini
Math Major Turned AI Enthusiast: Mastering Computer Vision | Building Robust Python Code | Delving Deep into the Math of AI | Sharing Knowledge Through Writing | Ready to Transform Ideas into Reality
The rm command
We use the rm (remove) command to remove files from our machine.
For example, rm app.js would remove the app.js file.
Note: rm DELETES FILES, there is no undo or recycling bin to retrieve them from. They are gone!
rm <filename>
To delete empty folders, we need to use the -d option with rm or you can just use rmdir followed by the folder name.
For example, rm -d cats or rmdir cats would remove the cats directory (only if it's already empty).
To delete folders that are NOT empty, use the -r option.
For example, rm -r chickens would delete the chickens directory whether it's empty or not.
You definitely want to be careful when deleting directories!
rm -r <foldername>
In this example, the directory "Mares" is empty.
Moving Stuff
Use the move command mv to move files and directories from one location to another.
When we specify a file or files as the source and a directory as the destination, we are moving the files into the directory.
For example, mv app.css styles/ will move the app.css file into the styles directory.
mv <source> <destination>
You can also move multiple files in one go.
mv <file1> <file2> <file3> <file4> <destination>
Moving folders is also relatively straightforward too.
All you need to take care is that the destination folder you are providing must exist, otherwise you'll end up renaming the source folder.
领英推荐
You can see what happens when the destination folder you provide doesn't exist, it just renames your source folder.
Renaming with mv
We can also use the move command to rename files and folders.
If we specify a single file as the source and a single file as the destination, it will rename the file. For example, to rename the chickens.txt file, we could run
mv chickens.txt roosters.txt
If we specify a single folder as the source and the destination doesn't yet exist, it will rename the folder. If the destination does exist, it will move our source folder into the destination.
mv <currentname> <newname>
Copying with cp
We can use the copy command cp to create copies of files and folders.
Suppose we want to create a copy of the file sheep.txt with name dolly.txt, all we need to run is cp sheep.txt dolly.txt
cp <source> <destination>
To copy multiple files into another directory, use
cp <file1> <file2> <directory>
To copy folders, you just need to use the recursive option -r with the cp command.