Master command line: deleting, copying and moving files

Now when we learned how to locate files through the command line interface, how about deleting files? Let’s look at how we can delete files as it is equally important.

Note – deleting is irreversible

Straight getting started with command laid out to you, you use del to delete any files on Windows and rm in macOS and Linux. Remember one major thing before you proceed with these commands that is deleting files and folders is convenient with the GUI by right-clicking or hitting that del key on the keyboard and later you can restore them from the recycle bin if you have to. Yet the same is not true if you deleting files from the command line. Once it is done, there is no turning or getting back.

How can you delete a file through the command line?

Deleting is simple, simply type in del <filename> . For instance, to delete the GAMESLIST.txt we run del GAMESLIST.txt and hit enter. The file will be deleted.

You do the same thing on mac and Linux operating systems but with rm instead of del and the filename. Like, rm GAMESLIST.txt and it is gone. Note that there will be no response message or anything from the system, and the command will move on the new empty line waiting for your input. To verify for the file deletion, you can simply run a dir or ls respective to their system to see if the file is deleted.

Typically if you get no response or message, means the file is deleted but if you get a response while deleting a file other than the assurance check whether you are sure you want to delete the file is any error message. That is a different scene where the file may not exist or you made a typo etc.

Using a wildcard

Note that you can also utilize the mighty wildcard here as well. For instance, if you wanted to delete every file of any extension of text, you could do so by putting the wildcard and the file extension. Something like, del *.txt. This will delete every instance of text file you have in the directory without having to go and select all of the random text file in random places.

Pay double attention when you are using CLI to delete something or prepare to have one fine gloomy day.

Copying and moving files via command line

Lots of tasks are done via command line includes major tasks like navigating, exploring, making/creating files, folders, deleting files, copying, moving files/folders etc. These are the very basic tasks that are performed on daily routine. Unfortunately, the nature of copying and moving files via command line can be one hell of workout at first due to having lot of options coming along with these commands, especially for the users who have been using drag /drop via GUI all throughout their life.

Yet, you do not have to you worry, we will use a five-step methods to make copying and moving files easier for you.

The command for copying and moving files on Windows is copy and move and on macOS and Linux, you have cp and mv. Note that this is not the only way, you can discover various of ways to do it. As you will become confident over time, hit the copy/? or move/? or man cp or man mv on macOS and Linux, to see the other options and switches you can use for your need.

The summary of five steps you can use

Now let us explore the five-step method that has just been mentioned:

  1. Move to the directory where you have the files that you want to copy or move.
  2. Now type copy or move in Windows or cp or mv in macOS and Linux.
  3. Type the name or names of the files that you want to move or copy and put a space at the end.
  4. Now type the path name of the location for the file or files.
  5. Simply hit enter.

Wildcard for copying and moving files

Know that you are also allowed to use a wild card. If you do not know what that is, wildcard could be something like an asterisk (*) or question mark (?). An example of the use case of such a wildcard is the situation in which you are looking for or wanting to target all the files whose extension has .txt in it.

You can do so by typing something like *.txt to target all the text with this extension. This is the simplest way to describe what a wildcard is used for. Did you also see earlier how we used copy/?

To get all of the options that comes along with such copy command. So, yeah, feel free to use wildcard for a bulk copy or moving task.