This article will explore the basic Linux commands and how to use them.
- 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.
- 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 description of any linux command like this:
- info : An alternative to obtain a comprehensive user documentation for a given program is to invoke info instead of man:
- 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:
- mv : The mv command is used to move or rename files.
- mkdir : mkdir command is used to create a subdirectory in your current working directory type.
- 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.
- rmdir : Now, that you are in the home directory and try to remove the directory called practice, rmdir will produce an error message:
- rm : rm removes each specified file like this:
- 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:
- less : Here, 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:
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 coloured, you can call ls with the option –color:
$ ls --color
$ man ls
Here, 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).
$ 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.
$ 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 it thinks are related to renaming. On your computer this command might (and probably will) display more information but it’s very likely to include the entries shown.
$ 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 newname. So, as a safe-guard, always use `-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
$ 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.
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
$ 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.
$ 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!
$ cat myspeech.txt Friends, Coders, Linux-lovers! This is an article in GeeksForGeeks.
$ less myspeech.txt
Just as with using man, use the arrow keys to navigate, and press q to quit.
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.