5 Linux command line tips & tricks

In this post we will show you some useful commands which you can use within your BASH shell.

Bash Shell Linux

1) repeat the last command argument: lets say that you need to check the content of a file without editing it and you use the command:
      
then you find out that you need to edit the same file. Using the following bash shortcut you can repeat the argument of the last command and use it as argument for the new command:
      

2) you need to check the content of a specific line or range of lines of a script or configuration file. There are several ways to achieve this:
- use less and jump to the line you need to review ( if you want to go to line 27, just open the file with less and type 27g ). The same option is valid using vi
- print only the content of the specified lines using sed (my favorite way)
      
the above command will print the line between 25 and 30

3) you need to determine which were the latest 10 users logged to your server:
      

4) You want to disable your printer service (as this is a server and does not have any printers connected ...):
a) find if "cups" service is running:
      
   alternatives:
      
      
b) if you find the service started and enabled you need to stop it first:
      
c) now you need to disable so it will not start at next system reboot:
      
That's it. The same procedure can be applied to any other services. I have been using commands tested on CentOS7. These commands will run on Red Hat 7 and on most of the other up to date Linux distributions.

5) You need to convert a unix text file into a windows (replace end of the line character). While using unix2dos is the fastest way, the utility is not available as default in all the distributions (perhaps it is not included in the minimal CentOS 7 installation). More than this, why you have to install a package to do a specific task when you can adopt the creative "unix-way"... there are several unix-way, I am going to provide you a few, but with your fantasy and experience you may create new one (and choose which one you prefer)
      
awk is really powerful ... in this example it is gonna "sub"-stitute the character at the end of each line ($) with the windows end of line (\r) from the file unix.txt. The output is forwarded line per line to the file win.txt
      
sed is possibly more difficult compare to awk but the above command shows a really similar structure. Perhaps both the commands (awk and sed) are manipulating regular expressions and that's waht we need. Again, s stand for substitute ... what ? the end of the line character ($) with \r ...

Notes:
Other useful commands are published in the following posts of Cyber Sec Linux:
How to hack your Linux Wireless interface in 5 steps
8 Daily Tasks every System Administrator need to know

Please, feel free to share and comments !

No comments:

Post a Comment