Many of us who love to work on Linux enjoy the privilege of using a plethora of commands and tools. Here is our effort to introduce you to a few very simple- to-use, yet enormously effective commands. The intended audience may belong to all classes of Linux users and the only requirement is to have a basic acquaintance with Linux. Our article deals with bash shell and Linux version Fedora 9, kernel 2.6.25.
- Often, commands on the console may span many lines, and encountering a type mistake at the beginning of the command would require you to use the slow way of punching the right/left arrow keys to traverse in the command string.
Remedy: Try Ctrl+e to move to the end of the command string and Ctrl+a to reach start. It’s the fastest way to edit a Linux command line. To delete a word in the command string, use Ctrl+w. - Another wonder of a simple shell variable is
!$
. Let’s say you have to create a directory, go into it and then rename it. So the flow of commands would be:$ mkdir your_dir $ mv your_dir my_dir $ cd my_dir
Remedy: Well, Linux has a shorter and quicker way:
$ mkdir your_dir $ mv !$ my_dir $ cd !$
!$
points to the last string in the command string. This is useful in various scenarios where the last word of command string is to be used in subsequent commands (almost with all Linux commands likevi
,tar
,gzip
, etc). - Do you want to know what an
ls
or adate
command does internally? Just run the following code to get to know the basic block of any Linux command:$ strace -c /usr/bin/ls
strace
is a system call monitor command and provides information about system calls made by an application, including the call arguments and return value. - What if you want to create a chain of directories and sub-directories, something like
/tmp/our/your/mine
?
Remedy: Try this:$ mkdir -p /tmp/our/your/mine
- One very interesting way to combine some related commands is with
&&
.$ cd dir_name && ls -alr && cd ..
- Now for some fun! Have you ever tried checking the vulnerability of your Linux system? Try a fork-bomb to evaluate this:
$ :(){ :|: & };:
It’s actually a shell function; look closely and it’s an unnamed function
:()
with the body enclosed in{}
. The statement ‘:|:
’ makes a call to the function itself and pipes the output to another function call—thus we are calling the function twice.&
puts all processes in the background and hence you can’t kill any process. Finally ‘;
’ completes the function definition and the last ‘:
’ initiates a call to this unnamed function. So it recursively creates processes and eventually your system will hang. This is one of the most dangerous Linux commands and may cause your computer to crash!
Remedy: How to avoid a fork bomb? Of course, by limiting the process limit; you need to edit/etc/security/limits.conf
. Edit the variablenproc
touser_name hard nproc 100
. You require root privileges to modify this file. - One more dirty way to hack into the system is through continuous reboots, resulting in the total breakdown of a Linux machine. Here’s an option that you need root access for. Edit the file
/etc/inittab
and modify the lineid:5:initdefault:
toid:6:initdefault:
. That’s all! Linux specifies various user modes and6
is intended for reboot. Hence, your machine keeps on rebooting every time it checks for the default user mode.
Remedy: Modify your Grub configuration (the Linux bootloader) and boot in single user mode. Edit the file/etc/inittab
and change the default user level to 5.
I hope you’ll have some fun trying out these commands, and that they bring you closer to Linux. Please do share your feedback and comments.
dint know that “!$” pointed to the last string of the previous command… awesome…!
sounds good …
suppose u tried ” /etc/init.d/networking restart” then will it work via your command ? “!$” .. Ans :- No
root@eOdissa-desktop:/home/amaresh/Documents/Testing# /etc/init.d/networking restart
* Reconfiguring network interfaces… Ignoring unknown interface eth1=eth1.
Ignoring unknown interface eth2=eth2.
Ignoring unknown interface ath0=ath0.
Ignoring unknown interface wlan0=wlan0.
[ OK ]
root@eOdissa-desktop:/home/amaresh/Documents/Testing# !$
restart
bash: restart: command not found
Here do “!/etc/”
root@eOdissa-desktop:/home/amaresh/Documents/Testing# !/etc
/etc/init.d/networking restart
* Reconfiguring network interfaces… Ignoring unknown interface eth1=eth1.
Ignoring unknown interface eth2=eth2.
Ignoring unknown interface ath0=ath0.
Ignoring unknown interface wlan0=wlan0.
[ OK ]
sounds good …
suppose u tried ” /etc/init.d/networking restart” then will it work via your command ? “!$” .. Ans :- No
root@eOdissa-desktop:/home/amaresh/Documents/Testing# /etc/init.d/networking restart
* Reconfiguring network interfaces… Ignoring unknown interface eth1=eth1.
Ignoring unknown interface eth2=eth2.
Ignoring unknown interface ath0=ath0.
Ignoring unknown interface wlan0=wlan0.
[ OK ]
root@eOdissa-desktop:/home/amaresh/Documents/Testing# !$
restart
bash: restart: command not found
Here do “!/etc/”
root@eOdissa-desktop:/home/amaresh/Documents/Testing# !/etc
/etc/init.d/networking restart
* Reconfiguring network interfaces… Ignoring unknown interface eth1=eth1.
Ignoring unknown interface eth2=eth2.
Ignoring unknown interface ath0=ath0.
Ignoring unknown interface wlan0=wlan0.
[ OK ]
didnt knew about !$….very good…thanks
:(){ :|: & };: was awesum …:):)kepp going
:(){ :|: & };: was awesum :) :) keep going
Hi Ganesh,
Thanks. I’ll share more of such stuff in future.
Vishal
Hi amaresh_83,
“!$” points to last stirng in the command. So running “/etc/init.d/networking restart”, will make !$=restart.
If you run > !$
You will not get desired result.
Vishal
some good info well presented thanks!
Hi Vishal,
I have tried >!$ but found not working.
I have tried with ssh santosh@192.168.1.4
after this I have exited form that machine.
and now tried the >!$ command, instead of logging in to the other box, it gave output as > santosh@192.168.1.4
I guess something is missing with that command ?
Thanks,
Santosh.
Very nice, thank you very much. Keep posting in the future.
>>I have tried >!$ but found not working.
>>I have tried with ssh santosh@192.168.1.4
I$ would have value = “santosh@192.168.1.4”
Bash demark command string with “space/blank” as delimiter.
>>I guess something is missing with that command ?
It’s perfectly fine.
Very nice ………….. I loved the article
Great Article Vishal. Looking fwd for more such articles from you.
!$ just holds the last args. in previous command on shell, If you want make reuse of used command than try :
$ ctrl+r than type few later of already used command..
HTH
Enjoy maadi
The is very nice. Its basic , interesting and fun to apply … Thanks for this article.
Thanks for the tip. I was unaware that !$ would yield last argument of the previous command. There was this way !-1:$ seems !$ is a shortcut. In that case !^ should yield the first argument. Nice!
2. Actually i like doing rather than using !$
Hi Srinivas,
Your comment is ambiguous. On a general note, “i$” is there to minimize your typing-efforts :-)
yes. this is good. thanks dear
Try ctrl+r and start typing the previous / recently used commands
ex: /etc/init.d/httpd restart
try ctrl+r rest you will get the command and give enter
this works fine :) keep sharing
We can also use ; in place of && for
cd dir_name && ls -alr && cd ..
That means
cd dir_name; ls -alr; cd ..
gives the same result as above command
Nice
https://www.opensourceforu.com/
Hi G K Reddy,
Following format will work irrespective of success/failure of previous commands.
cd dir_name; ls -alr; cd ..
But in case we want subsequent commands to work only if previous commands succeeded, then “&&” format(as suggested by Vishal) is well suited. This is sometimes required.. like make and install etc..
Really all command are crazy
Very good artical………..
execute a command date
and then try !d after at any time.
it gives date output .it remembers the prevoius command using its initial letter.
command $ :(){ :|: & };: is very very dangerous…
load average went up-to 350.35, 4271.60, 3103.02 in sun machine… :'(
Awesome.. How I will try the No.6
:-)
awesome article, in number 6, you need to re-login after editing “/etc/security/limits.conf” to take effect.
It cost me a cold reboot ;)
Hi All,
even i found the !$ command not working..since Vishal ran this on fedora its quite possible that other linux versions mint,ubuntu might not be having this command.
Please let us know if anyone not getting the results has tried on OS other than fedora.
try following:
1. find all gcc commands: history
$history | grep gcc
2. repeat previous command: !!
$apt
$sudo !!
3. reverse search pattern: !pattern
$gcc abc.c
$gdb a.out
$!g –> will repeat “gdb a.out”
$!gc –> will repeat “gcc abc.c”