This article will explore the basic Linux commands and how to use them.
1. ls: How would we know what a folder contains? With a graphical interface, you’d do this by opening a folder and inspecting its contents. From the command line, you use the command ls instead to list a folder’s contents.
By default, ls will use a very compact output format. Many terminals show the files and subdirectories in different colors that represent different file types. Regular files don’t have special coloring applied to their names. Some file types, like JPEG or PNG images, or tar and ZIP files, are usually colored differently, and the same is true for programs that you can run and for directories. Try ls for yourself and compare the icons and emblems your graphical file manager uses with the colors that ls applies on the command line. If the output isn’t colored, you can call ls with the option –color:
$ ls --color
2. man: You can learn about options and arguments to be used in any command in Linux. man(man is short for manual) is used to give a description of any Linux command like this:
$ man ls
Here, the man is being asked to bring up the manual page for ls. You can use the arrow keys to scroll up and down in the screen that appears, and you can close it using the q key (for quit).
3. info: An alternative to obtaining comprehensive user documentation for a given program is to invoke info instead of man:
$ info ls
This is particularly effective to learn how to use complex GNU programs. You can also browse the info documentation inside the editor Emacs, which greatly improves its readability. But you should be ready to take your first step into the larger world of Emacs. You may do so by invoking:
$ emacs -f info-standalone
That should display the Info main menu inside Emacs (if this does not work, try invoking emacs without arguments and then type Alt + x info, i.e. by pressing the Alt key, then pressing the x key, then releasing both keys, and finally typing info followed by the Return or Enter key). If you type then m ls, the interactive Info documentation for ls will be loaded inside Emacs. In the standalone mode, the q key will quit the documentation, as usual with man and info.
4. apropos: If you don’t know what something is or how to use it, the first place to look is its manual and information pages. If you don’t know the name of what you want to do, the apropos command can help. Let’s say you want to rename files, but you don’t know what command does that. Try apropos with some word that is related to what you want, like this:
$ apropos rename ... mv (1) - move (rename) files prename (1) - renames multiple files rename (2) - change the name or location of a file ...
Here, apropos searches the manual pages that man knows about and prints commands. On your computer, this command might (and probably will) display more information but it’s very likely to include the entries shown.
5. mv: The mv command is used to move or rename files.
$ mv oldname newname
Depending on your system configuration, you may not be warned when renaming a file will overwrite an existing file whose name happens to be a newname. So, as a safeguard, always use the `-i’ option when issuing mv like this:
$ mv -i oldname newname
If the last argument happens to be an existing directory, mv will move the file to that directory instead of renaming it. Because of this, you can provide mv more than two arguments:
$ mv first_file second_file third_file ~/stuff
If ~/stuff exists, then mv will move the files there. If it doesn’t exist, it will produce an error message, like this:
$ mv first_file second_file third_file ~/stuff mv: target 'stuff' is not a directory
6. mkdir: mkdir command is used to create a subdirectory in your current working directory type.
$ mkdir practice
To see the directory practice you have just created, type ls. If you wish to create a subdirectory (say the directory bar) inside another directory (say the directory foo) but you are not sure whether this one exists or not, you can ensure to create the subdirectory and (if needed) its parent directory without raising errors by typing:
$ mkdir -p ~/foo/bar
This will work even for nested sub-sub-…-directories.
7. cd: The command cd directory means change the current working directory to ‘directory’. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.
To change to the directory you have just made, type:
$ cd practice
Now, if you go back to your home directory, type
$ cd ..
Note: There is a space between cd and the dot
8. rmdir: Now, that you are in the home directory and try to remove the directory called practice, rmdir will produce an error message:
$ cd .. $ rmdir practice rmdir: failed to remove 'practice': Directory not empty
If the directory you wish to remove is not empty, rmdir will produce an error message and will not remove it. If you want to remove a directory that contains files, you have to empty it.
9. rm: rm removes each specified file like this:
$ rm practice/fstab practice/hosts practice/issue practice/mod
And now you can try removing the directory again:
$ rmdir practice
And now it works, without showing any output. But what happens if your directories have directories inside that also have files, you could be there for weeks making sure each folder is empty! The rm command solves this problem through the amazing option -R, which as usual stands for “recursive”. In the following example, the command fails because foo is not a plain file:
$ rm ~/foo/ rm: cannot remove `~/foo/`: Is a directory
So maybe you try rmdir, but that fails because foo has something else under it:
$ rmdir ~/foo rmdir: ~/foo: Directory not empty
So you use rm -R, which succeeds and does not produce a message.
$ rm -R ~/foo/
So when you have a big directory, you don’t have to go and empty every subdirectory. But be warned that -R is a very powerful argument and you may lose data you wanted to keep!
10. cat: You don’t need an editor to view the contents of a file. What you need is just to display it. The cat program fits the bill here:
$ cat myspeech.txt Friends, Coders, Linux-lovers! This is an article in GeeksForGeeks.
11. less: Here, the cat just opens the file myspeech.txt and prints the entire file to your screen, as fast as it can. However, if the file is really long, the contents will go by very quickly, and when cat is done, all you will see are the last few lines of the file. To just view the contents of a long file (or any text file) you can use the less program:
$ less myspeech.txt
Just as with using man, use the arrow keys to navigate, and press q to quit.
12. pwd: When have to work with the multiple directories on the Linux system and we go on moving from one directory to another, then to check out our current working directory this pwd command is very useful. On executing the pwd command on the terminal the absolute path of the current working directory is printed on the terminal window.
13. cal: This is a very useful command in the Linux systems to view the calendar of any particular month or whole year on the Linux terminal window.
$ cal # prints calender of current month $ cal 2021 # prints calender of whole year (2021)
14. date: This command is frequently used to print the current day, date, time, and time zone on the terminal window. By default, the time zone is the one on which the Linux system is configured.
This article is contributed by Adiya Nihal Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.