DEV Community

Cover image for Command Line Basics to Fun
Dibas Dauliya
Dibas Dauliya

Posted on • Edited on

Command Line Basics to Fun

Command line is important if we want to manage files and folders, do programming, publish it, upload code in the GitHub, GitLab, bitbucket or any other providers and many more. It is a text-based interface. We can also use the GUI method to do these things but it's easy and fast. Highly recommend giving it a try.

It can be referred to by the name as terminal, bash , or shell. They are the app which runs the commands. It is recommended to use terminal for Mac and Linux users and Git Bash for Windows users.

Please ignore the $ sign; it is not the part of the command but just the convection to distinguish other code and typical command line's commands. I've also used the same convection in my first article - NextJS Blog with Strapi.

Commands:

$ pwd 
Enter fullscreen mode Exit fullscreen mode

It stands for Print Working Directory and it shows the pathname of the working directory.

$ ls
Enter fullscreen mode Exit fullscreen mode

List command shows all the files and folders based on where we are. For instance, if we are in the root folder (~) which we usually are when we open the bash or terminal then it will show all the files and folders of the root. For hidden files and folders, we can use -a flag ($ ls -a).

$ cd folderName
Enter fullscreen mode Exit fullscreen mode

cd stands for change directory. We can use ls to see all the files and folders and cd to go inside the folder. For instance, we are in the root folder and if we want to go to the desktop folder we can use $ cd desktop.
Furthermore, we can use $ cd .. to go back to the previous folder. Let's assume we have a long or complicated folder name or we are too lazy to type, we can just type a few words of that
folder and hit tab to autocomplete that name.

$ mkdir folderName
Enter fullscreen mode Exit fullscreen mode

mkdir stands for make directory. As its full form clarifies, we can use it to create a new folder. For example, If we want to make a folder named hello then we can do it by using the $ mkdir hello command.

$ rmdir folderName
Enter fullscreen mode Exit fullscreen mode

rmdir stands for remove directory. We made a folder named hello using the mkdir command and we can delete it using $ rmdir hello. We can use $ rmdir * command to remove all the empty folders of the working folder.
However, if you want to delete the non-empty folder then you may want to use $ rm -r folderName command with -r recursive flag.

$ touch fileName
Enter fullscreen mode Exit fullscreen mode

It creates the new file. If we want to make a text file named hi then we can do it using the $ touch hi.txt command.

$ nano fileName
Enter fullscreen mode Exit fullscreen mode

We can add or edit the content of the file name using this command. nano is the command line text editor. After running this command we can enter our text content and press ctrl + x to exit. Before exit it will ask to save the file or not, we can choose the option and hit enter.

Also, there is another popular command line text editor named vim. We can open our file in it using $ vim hi.txt and start entering the text content. Now, here is the confusing part. How to exit? First of all, press ESC key to enter normal mode. After that type : to enter command line mode and then type q! to quit without saving and qw to quit by saving the content. You can learn about it from this stackoverflow answer.

$ cat fileName
Enter fullscreen mode Exit fullscreen mode

cat stands for concatenate and this command is used to read the content of the file and merge the content of multiple files to a single file. For example we created a file named hi.txt, now we can read the content of the file using the $ cat hi.txt command.
Also, if we have another two files named hi2.txt and final.txt, we can put the text of both files into final.txt file using $ cat hi.txt hi2.txt > final.txt

$ rm fileName
Enter fullscreen mode Exit fullscreen mode

rm stands for remove and this command removes the file from the system. We make a text file named hi.txt; if we want to remove it then we can run the $ rm hi.txt command. Furthermore, we can run $ rm * to remove all the files of the working directory.

$ open/start fileName/folderName
Enter fullscreen mode Exit fullscreen mode

It will open the file, folder, or working folder based on the second value of the command. We should use open or xdf-open at Mac and Linux and start at Windows followed by file or folder name to open it.
For example, we can use $ start index.html to open the file in the browser, $ start hi.txt to open the file in the default text editor, $ start hello to open the folder named hello, and $ start . to open the working folder in windows.

$ Attrib +h +s +r hello
Enter fullscreen mode Exit fullscreen mode

It will hide the folder named hello. We can use $ Attrib -h -s -r hello to make that folder visible. It can also be used for files.

Thank you. πŸ˜€

Top comments (0)