In 1991, Linus Torvalds wrote the first version of the Linux kernel. Some years earlier, Richard Stallman began the GNU Project to create a free Unix-like operating system.
Linux, the open-source operating system, has gained immense popularity over the years due to its flexibility, security, and the wide range of options it offers to users. One of the key strengths of Linux is its ability to be customized and tailored to specific needs, resulting in the creation of various "flavours" or distributions. These flavours, often referred to as Linux distros, provide different combinations of software packages, desktop environments, and system configurations.
Fig 1. a
Why use the Command Line?
It’s been said that “graphical user interfaces make easy tasks easy, while command line interfaces make difficult tasks possible,” and this is still very true today.
What is Shell?
When we speak of the command line, we are referring to the shell. The shell is a program that takes keyboard commands and passes them to the operating system to carry out. Almost all Linux distributions supply a shell program from the GNU Project called bash. The name is an acronym for bourne-again shell, a reference to the fact that bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
Terminal Emulators
When using a graphical user interface (GUI), we need another program called a terminal emulator to interact with the shell. Several other terminal emulators are available for Linux as shown in Fig 1. a, but they all do the same thing: give us access to the shell.
Understanding the File System Tree
Like Windows, a Unix-like operating system such as Linux organizes its files in what is called a hierarchical directory structure. This means they are organized in a tree-like pattern of directories (sometimes called folders in other systems), which may contain files and other directories.
Important notes on the File system
Filenames and commands in Linux, like Unix, are case-sensitive. The filenames File1 and file1 refer to different files.
Though Linux supports long filenames that may contain embedded spaces and punctuation characters. Most important, do not embed spaces in filenames. If you want to represent spaces between words in a filename, use underscore characters.
Linux has no concept of a “file extension” like some other operating systems. You may name files any way you like.
Working with Commands
In this section, I tried to cover everything related to Linux and its commands. It focuses on the small core commands that you will use 80% or 90% of the time, trying to simplify the usage of the more complex ones. All those commands work on Linux, macOS, WSL, and anywhere you have a UNIX environment.
- man: command that will help you understand all the other commands. type man to get the manual:
-
ls: Inside a folder you can list all the files that the folder contains using the ls command. ls accepts a lot of options. One of my favourite options combinations is -larth.
You have, from left to right:
the file permissions (and if your system supports ACLs, you get an ACL flag as well)
the number of links to that file
the owner of the file
the group of the file
the file size in bytes
the file modified datetime
the file name
- cd: Once you have a folder, you can move into it using the cd command. cd means change directory.
You can use the .. special path to indicate the parent folder:
You can also use absolute paths, which start from the root folder /
- pwd: Whenever you feel lost in the filesystem, call the pwd command. It will print the current folder path.
- mkdir: You create folders using the mkdir command
You can create multiple folders with one command:
You can also create multiple nested folders by adding the -p option
- rmdir or rm: To delete folders with files in them, we'll use the more generic rm command which deletes files and folders, using the -rf options.
- mv: you can move a file around using the mv command. You specify the file's current path, and its new path:
The pear file is now moved to file_new. If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder
- cp: You can copy a file using the cp command:
To copy folders you need to add the -r option to recursively copy the whole folder contents:
- touch: You can create an empty file using the touch command
find: The find command can be used to find files or folders matching a particular search pattern. It searches recursively.
In: The ln command is part of the Linux file system commands. It's used to create links. What is a link? It's like a pointer to another file. A file that points to another file. You might be familiar with Windows shortcuts. They're similar. We have 2 types of links: hard links and soft links.
a. Hard links: Hard links are rarely used. They have a few limitations: you can't link to directories, and you can't link to external filesystems (disks). For example, say you have a file called file.txt. You can create a hard link to it using:
Now any time you edit any of those files, the content will be updated for both.
b. Soft links: Soft links are different. They are more powerful as you can link to other filesystems and directories, but when the original is removed, the link will be broken.
ln -s <original> <link>
Now if you delete the original file, the links will be broken, and the shell will tell you "No such file or directory" if you try to access it
- gzip: You can compress a file using the gzip compression protocol named LZ77 using the gzip command.
Command: gzip filename
This will compress the file, and append a .gz extension to it. The original file is deleted. To prevent this, you can use the -c option and use output redirection to write the output to the filename.gz file
There are various levels of compression. The more the compression, the longer it will take to compress (and decompress). Levels range from 1 (fastest, worst compression) to 9 (slowest, better compression), and the default is 6. The -v option prints the compression percentage information.
- gunzip: The gunzip command is equivalent to the gzip command, except the -d option is always enabled by default.
This will gunzip and remove the .gz extension, putting the result in the filename file. If that file exists, it will overwrite that. You can extract to a different filename using output redirection using the -c option:
- tar: The tar command is used to create an archive, grouping multiple files in a single file. Its name comes from the past and means tape archive. Back when archives were stored on tapes.
This command creates an archive named archive.tar with the content of newlink.txt and softlink.txt:
tar -cf archive.tar newlink.txt softlink.txt #c stand for create
To extract files from an archive in the current folder, use:
tar is often used to create a compressed archive, gzipping the archive. This is done using the z option: tar -czf archive.tar.gz file1 file2
alias:
cat: Similar to a tail in some way, we have cat. In its simplest usage, cat prints a file's content to the standard output:
using the output redirection operator > you can concatenate the content of multiple files into a new file:
Using >> you can append the content of multiple files into a new file, creating it if it does not exist.
When watching source code files it's great to see the line numbers, and you can have cat print them using the -n option:
- less: The less command is one I use a lot. It shows you the content stored inside a file, in a nice and interactive UI.
Usage: less <file name>
You can navigate the file contents using the up and down keys, or using the space bar and b to navigate page by page. You can also jump to the end of the file pressing G and jump back to the start by pressing g. You can search contents inside the file by pressing / and typing a word to search. This searches forward. You can search backwards using the '?' symbol and typing a word.
- tail: It opens the file at the end, and watches for file changes. Any time there is new content in the file, it is printed in the window. To exit, press ctrl-C.
You can print the last 10 lines in a file: tail -n 10 <filen name>
You can print the whole file content starting from a specific line using + before the line number: tail -n +10 <file name>
- wc: The wc command gives us useful information about a file or input it receives via pipes.
The first column returned is the number of lines. The second is the number of words. The third is the number of bytes.
grep: You can use grep to search in files, or combine it with pipes to filter the output of another command. we can find the occurrences of the 'session' line in the auth.log file
Using the -n option it will show the line numbers:
One very useful thing is to tell grep to print 5 lines before, and 5 lines after the matched line, to give us more context. That's done using the -C option, which accepts a number of lines:
sort: Suppose you have a text file which contains the names. The sort command helps us sort them by name:
Use the r option to reverse the order
you can use on the output of another command, for example you can order the files returned by ls with:
To be continue...