File manipulation via CLI

One of the most common tasks a techy person has to deal with is manipulation of files vi aa command line.

Why a command line? There is a GUI (Graphical User Interface)! If you are assigned in the backend development where you will be primarily working in the server there is no GUI for you there. Pure command line. So knowing your command line will save you and your time and also you will know the benefits of using a CLI for some tasks which is thousand times faster than GUI.

Start working with files through your command line

So, let’s look at some of the things we can do to manipulate files via command line. We will look at two of the major file manipulation tasks like searching and deleting etc. For the very starter for this practice, let’s make a folder in one of your drive and copy random files and drop them inside this new folder. Watch out, do not move or cut/paste your important files here, since we will be playing with fire here and you may lose all of your files on the process.

The wildcard in rescue

Imagine you have 1000 files in your directory and you need to see how many of .txt files are in the directory. How much time and your mental and physical energy would it take to detect all of the .txt files one by one, even if you are using dir/p for paging? Nightmare! This where the wildcard come in handy and save you from this nightmare.

A wildcard can be two special character or symbol, like an asterick (*) and a question mark (?). You can use either of this mark in place of the filename or part of the filename. This is done so that the command line acts on more than one file at a time. This wildcard works with all sort of command line command that works with a filename.

Using a wildcard: an example

One of the great examples we can look at is the dir command. When we are running a dir command, what does it do? It finds all of the files and folders in the directory and shows us. That is cool. Yet, you can cut it short with the filename. Meaning, if you know a filename and you are looking for that file. Let’s say we are looking for a GAMESLIST.txt file in a directory. Navigate to the directory where it may reside and type dir GAMESLIST.txt and hit enter. The txt file specified will be shown to you.

This is great if you know the directory and filename but what if you have a folder full of mixed extension files and you forgot the name was “Gameslist” but you remember the extension was .txt. In this scenario, a wild card will save you from this mess! The way you would do this, again, navigate to the directory where you suspect the file may be and run this command dir *.txt and hit enter. See what shows up? There should be bunch of .txt files with various names preceded the extension will show up. This is extremely useful and efficient in such scenario mentioned.

More usage for a wildcard

Not only is it useful  for the example above mentioned but you can also replace part of the name or extension with wildcards. Continuing from the above example, let’s say we only remember our file’s first letter G, in this scenario we can apply dir g*.txt or dir g*.* if we even forgot the extension. Surprisingly, this will show you all of the filename that started with the letter ‘G’ with the former example and ‘G’.various extension type that started with G with the latter example, will be presented to you.

This search method applies to the mac and Linux operating systems as well with exception of ls. Mac and Linux use ls to view directory not dir like Windows. So, on these operating systems, you would do ls g*.txt and so, to find that gameslist.txt file.

Remember that this wildcard works with any command that works with files and folders and not limited to dir and ls only.