A Linux system administrator needs to know some Linux tricks. In this article, you will learn 3 useful Hacks for Linux users. Of course, you confirm that the world of Linux is filled with so much fun and interesting stuff, the more you go in, the more we find kinds of stuff. In our efforts to bring those little hacks and tips for you that make you different from others, here we have come up with three little tricks.
Table of Contents
3 useful Hacks for Linux users
Brief but important. Join us to learn these 3 useful Hacks for Linux users.
How to Schedule a Linux Job Without Cron
First, let’s know what does this title means basically? Scheduling a job/command in Linux is acronym to cron. Whenever you need to schedule a job, you call cron, but do you know you can schedule a job at a later time without corn? You can do it as suggested below.
Run a command (say date) every 5 sec and write the output to a file (say date.txt). You need to run the below one-liner script directly on the command prompt.
while true; do date >> date.txt ; sleep 5 ; done &
To explain more, have a look at the anatomy of the above one-liner script:
- while true – Ask script to run while the condition is true, it acts as a loop that makes the command to run again-and-again or say in a loop.
- do – do perform what follows, ie., execute a command or set of commands that lies ahead of doing statement.
- date >> date.txt – here the output of date command is being written to a file date.txt. Also, note that we have used >> and not >.
- >> ensures that the file (date.txt) is not overwritten every time the script executes. It just appends the changes. Whereas > overwrite the file again and again.
- sleep 5 – It asks the shell to keep a time difference of 5 seconds before it executed again. Note the time here is always measured in seconds. Say if you want to execute the command every 6 minutes, you should use (6*60) 360, in a succession of sleep.
- done – marks the end of the while loop.
- & – Put the whole process in a loop to the background.
Similarly, you can execute any script in the same manner. Here is the command to call a script after a certain interval (say 100 sec) and the name of the script is script_name.sh.
Please remember that the script above should be run in the directory where the script to be called lies, else you need to provide a full path (/home/$USER/…/script_name.sh). The syntax for calling script at above-described interval is:
while true; do /bin/sh script_name.sh ; sleep 100 ; done &
Due to your recent studies in this guide, you can guess that the above one-liner is not a replacement of Cron, because Cron utility supports a whole lot of options, as compared and is very flexible as well as customizable. However if we want to run certain test cases or I/O benchmark, then the above singe command will serve the purpose.
buy a virtual machine with bitcoin
2- How to Clear Terminal without Using ‘clear’ Command
Secondly, to answer the question of what we do to clear the screen, you may say all know it’s ‘clear‘ command. But please consider that if we make a habit of using key combination ‘ctrl+l‘ to clear terminal, we will save a lot of time of ours.
Key combination ‘Ctrl+l‘ has the same effect as ‘clear‘ command. So from next time use ctrl+l to clear your Linux Command Line Interface.
In Conclusion, Since ctrl+l is a key combination, so you can not use it inside a script. If you need to clear screen inside a shell script, call command ‘clear’, for all other cases I could think of now, ctrl+l is more than sufficient.
3- Run a command and come back to the current working directory automatically.
Finally, to learn an amazing hack not many people know, follow the last step of this tutorial.
You may run a command no matter what it returns back to the current directory. All you need to do is to run the command in parentheses i.e., in between ( and ).
Let see the example,
avi@deb:~$ (cd /home/avi/Downloads/)
(sample) Output
avi@deb:~
First, it cd to directory Downloads and then again return back to home directory in one go. In case you think the command didn’t execute and for some reason one or another it is not throwing error, since there is no change in prompt. Let’s do a little more tweak.
avi@deb:~$ (cd /home/avi/Downloads/ && ls -l)
(sample) Output
-rw-r----- 1 avi avi 54272 May 3 18:37 text1.txt -rw-r----- 1 avi avi 54272 May 3 18:37 text2.txt -rw-r----- 1 avi avi 54272 May 3 18:37 text3.txt avi@deb:~$
So in the above command, it first changed the current directory to Downloads. Then list the content of that directory before returning back to the current directory. Also, it proves that the command executed successfully. You may run any sort of command in the parentheses and return back to your current working directory without a hitch.
Good job! You succeded to learn more about Linux commands and save more Linux tricks to work better.
Dear user, we wish this tutorial 3 useful Hacks for Linux users would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.