Deleting, Moving and Copying

Deleting, Moving and Copying

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.



要查看或添加评论,请登录

Shivm Soaini的更多文章

  • Pipelines

    Pipelines

    Pipes are used to redirect a stream from one program to another program. We can take the output of one command and…

  • The Standard Streams and Redirection

    The Standard Streams and Redirection

    The three standard streams are communication channels between a computer program and its environment. They are:…

  • Working with Files

    Working with Files

    The cat command The cat command concatenates and prints the contents of files. cat filename will read the contents of…

  • The Shortcuts

    The Shortcuts

    We can speed up our command-line experience by taking advantage of the many built-in shortcuts. These shortcuts are…

  • The Nano Text editor

    The Nano Text editor

    Nano is a simple text editor that we can access right from the terminal. It's far more accessible than other popular…

  • Making Files and Folders

    Making Files and Folders

    The touch command To create a new file from the command line, use the touch command. Provide a filename, and that file…

  • Navigating the CLI Sea

    Navigating the CLI Sea

    The root directory The starting point for the file system is the root folder. We call it the root, but its actual…

  • Getting Help in Command Line Interface

    Getting Help in Command Line Interface

    Sometimes it's difficult to wrap our head around some of the commands we encounter. In this scenario, man pages can…

  • First Step in Command Line

    First Step in Command Line

    Some Familiar Terms A Shell is a computer interface to an operating system which expose the OS's services to the human…

  • A Brief History of Linux

    A Brief History of Linux

    The world of operating systems has many residents, but they can be segregated into two tribes: The Microsoft NT…

社区洞察

其他会员也浏览了