Basic Linux Command Line #Part2
In the first part I've discussed about file and directory management, also about how to control a process in Linux. On the part of this time we will return to discuss the Command Line Interface in Linux. This time, we will learn some commands in Linux terminal that deals with searching and changing files, merging some commands and how to find further help in Linux terminal.
https://steemit.com/utopian-io/@ghaziya/basic-linux-command-line-part-1-201816t05348108z
Find and modify file contents
Grep - Search for text in file
this command can be used to search certain text in a file. Sometimes we have one file with a lot of text stuff, this grep command will be very helpful.
Suppose I want to display the file /etc/passwd, we can do it with the $ cat /etc/passwd/ command then the result looks like this:
root: x: 0: 0: root: /root: /bin/bash
daemon: x: 1: 1: daemon: /usr/ sbin: /usr/sbin/nologin
bin: x: 2: 2: bin: / bin: / usr/sbin/nologin
sys: x: 3: 3: sys: / dev: / usr/sbin/nologin
sync: x: 4: 65534: sync: / bin: /bin/sync
----- text is partially cut --------
avahi: x: 114: 119: Avahi mDNS daemon , , , : /var/run/ avahi-daemon: /bin/false
colord: x: 115: 120: colord color management daemon , , , : /var/lib/ colord: /bin/false
saned: x: 116: 121 : : /var/lib/ saned: /bin/false
Debian-gdm: x: 117: 122: Gnome Display Manager: / var / lib / gdm3: / bin / false
user: x: 1000: 1000: Debian Live user ,,,: /home/ user: / bin/bash
This is certainly very annoying and ineffective, because all we want from this file is the information that contains the root word only.
user@debian: ~$ grep 'root' /etc /passwd
root: x: 0: 0: root: / root: /bin/bash
Grep command is very flexible and very useful when combined with the name of regular expression, commonly abbreviated regex. Regex itself is very much useful, but difficult to learn, but when it's used, it's not nice if you do not use it.
Suppose we want to find the line in /etc/passwd that begins with the word "user", then it can be done by :
user@debian:~$ grep '^user' /etc/passwd
Or which begins with the word 'root',
user@debian:~$ grep '^root' /etc/passwd
another example, suppose we want to find the line in / etc / passwd whose second letter is the letter o, it can be done by :
user@debian:~$ grep '^[a-z]o' /etc/passwd
the commands above look like unnecessary and rarely used commands, it is for ordinary users, the above examples look useless, but for a system administrator, the above commands will greatly help their work.
Echo - Printing Text to Screen
This echo command is used to display a string or sentence to the monitor screen.
user@debian:~$ echo 'Saya adalah pengguna Utopian.io'
The above example, I asked echo" to display 'I am a Utopian.io user.' This command is very useful if we write bash scripting that requires the appearance of text on the screen, or can be used by combining it with other commands.
This echo command is not only capable of displaying the words we want to display to the monitor screen, but can also display output from other linux commands, for example :
user@debian:~$ echodate
> - Enter Text (Text Will Be Overridden)
The bigger mark > is used to insert one or more text into another file, it will overwrite the previous file. Suppose I will create a file named utopian.txt
user@debian:~$ touch utopian.txt
then i use nano text editor to write down three lines of sentence :
it appears that the utopian.txt file has three lines of information in it. Now, if I use the larger command >" by combining the echo" command, the @utopian.io file will be overwritten, the three lines of information will be lost, and only the last remaining information we provide via the echo command, for example :
user@debian:~$ echo 'Saya sudah sebulan aktif di Utopian.io' > utopian.txt
>> - Insert Text (Text Will Inserted)
In contrast to the above command, these two bigger marks >> serve to add a new line at the end of the file. If the command > will delete the contents of an existing file, then this >> command will not remove the contents of the existing file, for example :
user@debian:~$ echo 'Saya adalah pengguna Utopian.io' > utopian.txt
user@debian:~$ cat utopian.txt'
user@debian:~$ echo 'Saya juga pengguna Steemit.com' >> utopian.txt
user@debian:~$ cat utopian.txt'
Merging Some Commands.
Commands and programs under Linux have very specific tasks and functions. These commands will be very useful when combined with other commands, yes, commands in Linux terminals can be combined between one command with another command, can even be combined with more than three perinah, usually for scripting purposes or to administer logs automatic.
; (semicolon) - Inter-Command Separator
This semicolon character allows us to run multiple linux commands in a single line. We only need to separate one command with another command with a semicolon (;).
Suppose I will run 4 commands in a row, ie whoami command, hostname, pwd, and ls -l.
user@debian:~$ whoami; hostname; pwd; ls -l
the three commands above will be run sequentially starting from whoami, hostname, pwd, then ls -l.
The merge of this command is not limited as in the above example only, it can be tailored to the needs and creativity of the users themselves.
| (read pipe) - Inter-Process Communication
Character | (read pipe) is like a pipe, where the output of a program or linux command becomes an input for the program or other linux commands, for example :
user@debian : ~ $ cat /etc/passwd | grep root
root : x : 0 : 0 : root : / root : /bin/bash
in the example above I use two command that is command cat and grep. The cat /etc/passwd command will show all the lines of the /etc/passwd file to the monitor screen, as I'm using | then the output from the cat command will be the input for the grep command.
The grep command that receives the input from the painted ink then looks for a sentence containing only the root word and displays it to the screen, since the row you want to display is just a line containing the root word only.
Sample Case Merging Some Commands
Now let's look at an example of merging commands in Linux with characters | and ; to form a report if you suppose a system administrator ;
user@debian:~$ echo Laporan Tanggal date untuk hostname ; echo -n Total user yang login : ; w | wc - l ; echo - n Total process berjalan : ; ps - e | wc - l
So the results obtained are ;
Laporan Tanggal Sun Dec 24 13:13:14 WIB 2017 untuk debian
Total user yang login : 3
Total process berjalan : 181
There are at least five merge commands involving six commands in Linux occurring in the above command. The w command is used to display the list of currently login accounts, while the wc is used to calculate the number of lines present in its input.
Please note in the echo command above, I combine it with other linux commands like date and hostname, this can be done by giving a `(back-quote), on the keyboard located under the ESC key, there is on the same key as ~(tilde ). The back-quote mark is defined as a replacement command, meaning that the words inside will be executed as commands in Linux.
Complicated and difficult ??? Looks useless ??? Initially yes, and indeed sometimes for ordinary users, the commands like the example above are useless, but if you want a career to be a Linux system administrator, then that's all a necessity.
Commands Seeking More Help
The use of this text mode is not an easy thing, this is because the number of commands in Linux are also parameters that accompany the command. However, we can seek help by finding more detailed documentation information about the command.
man - The Manual of a Linux Command
Linux provides a complete documentation of commands in text mudos, we can see it using this man command to get help when we forget how to use a command in Linux.
Suppose I want to know how to use echo command, then I can type man echo in Linux terminal, press [ENTER], it will show documentation and how to use echo command in Linux, when finished, press q key on keyboard to be able to return to shell terminal.
Conclusion
Now we've been talking about searching and changing files, merging some commands and how to find more help on Linux terminals. Commands in the Linux terminal are not only limited to this, there are so many other commands that have different functions. Studying these commands on Linux is more value if we want a career in Linux, let alone for a System Administrator or SisAdmin Internet Network.
Hopefully this second article can be useful for linux users who are learning how to use commands on Linux terminals.
Best regards, @ghaziya.
(Posted on Utopian.io - Rewarding Open Source Contributors)