Beginner’s Guide Linux Command Line Basics

1. What is a home directory in Linux?
Linux is a multi-user operating system, which means that more than one user can access the OS at the same time. To make things easy, each user is assigned a directory where they can store their personal files. This directory is known as a user’s home directory.
Home directories are found under the home directory. For example, my home directory is /home/himanshu. Please note that a user’s home directory has the same name as their login name. If you are a Windows user, you can think of a Linux home directory as a user specific directory usually present inside C:\Documents and Settings or C:\Users.
Users have complete control over their home directory as well as all its sub-directories. This means that they can freely perform operations like create and delete files/directories, install programs, and more, inside their home directory.
2. How to check the present working directory?
Whenever you open a command line shell in Linux, you start at your home directory. This is your present working directory, which changes as you switch to some other directory. Use the pwd command to check the complete path of your present working directory at any point of time.

3. How to switch directories?
Use the cd command to navigate through the Linux filesystem. This command requires either a directory name or its complete path depending upon where the directory is present.
For example, if your present working directory is /home/himanshu/pictures, and you want to switch to /home/himanshu/pictures/vacations, then you can simply run the command: cd vacations. In this case, the command line shell will search for the vacations directory inside pictures. A path relative to the present working directory is also known as relative path.
But in case you want to switch to /home/techspot, you’ll have to run the command: cd /home/techspot. The complete path, that begins with a forward slash (/), to a directory is also known as its absolute path. To quickly switch to the previous directory in the tree, run: cd .., or if you want to switch to the previous working directory run cd –
4. How to view directory contents?
Use the ls command to list the contents of a directory. If the command is run without any argument, it displays the contents of the present working directory.
5. How to view the contents of a file?
Use the cat command to view the contents of a file. This command expects a filename as an argument. As you can see in the screenshot below, the cat command displayed the contents of the arg.c file. However, there is a limitation. If the file is large, the output might be too big for the command line shell screen to accommodate.
6. How to create a new file?
Use the touch command to create a new file. The command requires a filename as argument. For example, to create a file named test.log in the present working directory, just run the command: touch test.log.
To create a new file at a location other than the present working directory, use the absolute path. For example, touch /home/himanshu/practice/test.log.
Tip: To write anything into a newly created file, use a command line editor like Vi or Vim.
7. How to rename/copy/delete a file?
Use the mv command to rename a file. For example, to rename log.txt to new_log.txt, run the command: mv log.txt new_log.txt. As always, if the file is not present in the present working directory, use the absolute path.
You can also use the mv command to move a file from one location to other. This is the equivalent of a cut-paste operation via GUI. For example, to move log.txt (present in current directory) to /home/himanshu, run the command: mv log.txt /home/himanshu.
To copy a file from one directory to another, use the cp command. Like the mv command, cp also requires a source and a destination. For example, cp log.txt /home/himanshu would create a copy of log.txt (with the same name) in the /home/himanshu directory.
To remove a file, use the rm command. This command expects a filename as an argument. For example, rm log.txt will remove the text file, if present in the current directory, while rm /home/himanshu/practice/log.txt will remove the text file present inside the practice directory.
To remove directories, use the -r command line option with the rm command. For example, rm -r /home/himanshu/practice/ would delete the practice directory with all its subdirectories and files.
8. How to search for files?
To search for files within a given directory, use the find command. The command requires directory path and filename as argument. For example, to search for a file named inheritance.cpp in the /home/himanshu/ directory.
9. How to search text within files?
To search text within files, use the grep command. The command expects a keyword and a filename as arguments, and outputs lines that contain the keyword. For example, to search all the lines in the file /home/himanshu/practice/wazi/gdb/test.c that contain the keyword ptr, use the grep command in the Use the -n command line option in case you want grep to display line numbers in output.
Use the -n command line option in case you want grep to display line numbers in output.
Tip: To search a keyword in all the files present in the current directory, use the * wildcard character as the filename.
Please note that unlike the find command, the grep command doesn’t search within subdirectories by default. However, you can enable this functionality by using the -R command line option while running the grep command.
10. What is the auto-complete feature?
While working on the Linux command line, typing long paths, file names, and more can feel like a burden. Use the tab key to auto complete these long names and paths easily. For example, to write /home, you can just write /ho and press tab. The command line shell will auto complete the name for you.
In the example above, it was easy for the shell to guess the name home because there was no other similar candidate in the / directory. But in case the shell encounters similar names while auto completing, it will display those names and you’ll have to write a few more letters for the shell to know the correct name.
Here is an example:
linux tutorial command line
The shell displayed all the names that it can use for auto completion. If, for example, you wanted to write techspot, then you’ll have to type in at least c to resolve the ambiguity. Once done, you can hit the tab key again to autocomplete the name.

11. What is root?
Root is the only user that has control over the entire Linux system. It is capable of doing what normal users can’t, such as, changing ownership of files, adding or removing files from system directories, and more. As you can guess, the root account is mostly used by system administrators only.
The top level directory in a Linux system, represented by forward slash (/), is known as root directory. It is the same directory that contains the home directory, which further contains user specific directories. However, you shouldn’t confuse / with the root user’s home directory, which is located under / by the name of root.
12. What are man pages?
To learn more about Linux commands, you can head over to their respective man (or Manual) pages that come preinstalled with Linux. To open a man page, just run the man command followed by the command name. For example, run man rm to open the manual page of the rm command. You can find a lot of useful information about Linux commands this way.
linux tutorial command line
We’ve barely scratched the surface here, as the Linux command line has much to offer.

(Visited 2 times, 1 visits today)
Like Love Haha Wow Sad Angry

One Reply to “Beginner’s Guide Linux Command Line Basics

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.