A Beginner’s Guide to the Linux Command Line Part 1

The Linux Command Line is a text-based interface that allows users to manipulate the operating system through a series of commands.

Linux is an open-source operating system, which means it relies on the input of people who are willing to contribute their time and work. The Linux Command Line has become very popular among developers because it offers them the freedom to create their own apps without having to rely on an app store. Similarly, it offers freedom for coders and sysadmins as well. However, if you’re new to this, then this guide will help you get started with the basics of how to use this simple tool.

Beginners Guide: The command line is one of the most powerful tools in any developer’s arsenal because it gives them complete control over what they can

 

The command line can be an antiquated way to interact with your computer. But think again — it doesn’t make it any less efficient or presentable. Linux has an unparalleled command line that gives it the power and flexibility that other operating systems lack. You can use it for tasks like searching and deleting all .tmp files in a directory (and its subdirectories) using many more steps than if you were to do it via GUI.

In this article, we will first discuss basic Linux commands such as navigating directories and doing file operations, and then we will go deeper into details of file metadata in the second half.

 

1. What is a home directory in Linux?

Linux is a multi-user operating system that allows more than one user to access it at the same time. Each user receives their own directory and files where they can keep their personal data safe and sound.

For example, my home directory is /home/dct. Please note that a user’s home directory has the same name as their login name. If you are a Windows user, you might know that there’s a user-specific directory inside “C:\Documents and Settings” or “C:\Users.” Linux system directories usually exist in the same area.

Regardless of where it is configured to reside in the file system, users have complete control over their home directory. They can freely perform operations like creating and deleting files/directories, installing programs, and more, inside their home directory.

 

2. How to check the present working directory?

Linux gives you the ability to switch between multiple work directories when in a terminal. As to why the PWD command will help you find out your present working directory. Just run pwd in order to check it.

 

pwd

3. How to switch directories?

To navigate in the Linux filesystem, it’s easy to use cd. This command either requires a directory name or its complete path.

For example, if you’re using a current directory of /home/dct/pictures and want to switch to /home/dct/pictures/vacations, then you can simply run the command: cd vacations. At the command line, you can search for all pictures within a directory by using the find command with relative paths in its options.

So, whenever you want to switch to a different directory or file, you will type the absolute pathname. Note that a forward slash (/), which starts a directory, begins with a forward slash and hides your username from it. To quickly switch to the previous directory in the tree, run ” cd ..” or if you want to switch back to your old working directory run ” cd -“.

 

4. How to view directory contents?

Just type ls in your terminal to list the contents of your directory. If you don’t run ls without any arguments, it will display the contents in the present working directory.

ls

 

You can specify the name of any subdirectory with the ls command, or enter its full directory path.

If you watch closely, the output of the ls command is in color. These different colors represent different types of files, making it easy to visually identify them. Some of the basic colors that you should know are Blue (Directories), White (Text files), Red (Archives), Cyan (Links), Green (Executables), and Pink (Images).

5. How to view the contents of a file?

Use the cat command with the filename as an argument to view its contents. However, there is a limitation. It can take too much time for the command line shell screen to handle large files.

cat agg.c
cat [filename] | less

You can use the less command with the cat command to view text files one screen at a time. The pipe symbol ( / ) tells the cat command to send its output straight to the less command. Since your cursor is already in text mode, you will be able to navigate the file’s content using the arrow keys of your keyboard. You can also quit using ctrl+d or typing q

 

6. How to create a new file?

Tap on the touch command to create a new file. Creating a file named ‘test.log’ in the present working directory would be as simple as running the command touch test.log

To create a new file at a location that’s different from the present working directory, specify the full path. For example, touch /home/dct/practice/test.log

touch /home/dct/practice/test.log

Some people use a command-line editor like Vi or Vim to create new files. It’s a great tool because you can make changes and save the file right away.

 

7. How to rename/copy/delete a file?

Rename files using the mv command. For example, to rename log.txt to new log.txt, run the command: mv log.txt new log.txt (Use an absolute path if the file is not in our current directory).

mv log.txt newlog.txt

You can move files from one location to another with the mv command. This is an easy copy-and-paste operation. For instance, to move log.txt (present in the current directory) to /home/dct, run the command: mv log.txt /home/dct.

mv log.txt /home/dct

Copy files from one directory to another! To copy a file, use the cp command. As with the mv command, it also requires a source and destination. For example, cp log.txt /home/dct would create a copy of log.txt (with the same name) in his home directory

cp log.txt /home/dct

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/dct/practice/log.txt will remove the text file present in dct’s home directory or practice directory

rm /home/dct/practice/log.txt

To remove directories with the rm command, use the -r option. You can also use it with specific files or directories by including their names after the -r for example, rm /home/dct/practice/

rm -r /home/dct/practice/

8. How to search for files?

To search for files within a given directory, use the find command. The command requires a directory path and filename as arguments. For example, to search for a file named inheritance.c in the /home/dct/ directory, use the find command

find /home/dct/ -name inheritance.c

By default, find searches in the present working directory. To change this behavior, use the –path parameter

You can also use wildcards with the find command to find specific files. For example, if you want to search all .c files present in the /home/dct/practice directory, type. The ‘*’ character is a wildcard that can represent any number of characters. For example, tech* can represent tech, TechSpot, tech report, and more.

find /home/dct/ -name *.c
find /home/dct/ -name tech*

9. How to search text within files?

To search for text within files, use the grep command. The command expects a keyword and a filename as arguments, and prints lines that contain the keyword. For example, in this case, to search all lines in the file /home/dct/practice/test/db/test.c that contain the keyword ptr, you would use the grep command like so:

grep ptr /home/dct/practice/test/db/test.c

The -n command-line option is used to display line numbers in the output of a given command.

grep -n ptr /home/dct/practice/test/db/test.c

Tip: To search a keyword in all the files present in the current directory, use the wildcard character as part of 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?

When working on the Linux command line, typing long paths, file names, and more can feel like a burden. But worry not! Auto-completing these long names and paths with tab is easy to do. For example, if you wanted to write /home you could simply type in “/ho” without the trailing slash, then press the tab.

It was easy to guess the name home in the example because there was no other similar candidate in the / directory. If a specific name, such as Alana, autocompletes on their computer running the shell application, the interface will now display those common names to show you it doesn’t know which one is correct. You’ll also have to write a few more letters in order for this new name to appear as an option again.

/home/dct/tc/te

techspot/ test/ test1/ test2/

/home/dct/tc/tec

The shell displayed all the names it can use for auto-completion. If you were typing in TechSpot, then you would have to type c to resolve the ambiguity. Once done, press tab again to autocomplete the name

Leave a Comment