Linux Commands I Hardly Knew - Reloaded

Sunday, July 6, 2008

Linux Commands I Hardly Knew - Reloaded

I had posted Linux Commands I Hardly Knew a couple of days before.

I admit, it was a copy from Foogazi. I did mention the source. I just made it more readable and included the comments also into the post. I don't need any credit. The post got 25,000+ hits. It clearly means it was useful, that's enough.

The post got lots of comments across the web. I have compiled the comments and is posting it as Linux Commands I Hardly Knew - Reloaded.

Also, lots of people pointed out that most of the commands mentioned were not Linux commands, but specific to the shell. Still, I am keeping the name unchanged.

So, here you go...

Command History

CTRL+R and CTRL+S will search through the previous commands, backwards and forwards respectively.

In zsh, you can tab-complete history expansion, so you just use !s[tab] to see what you're running. bash doesn't have that, but you can do echo !s and, if you like what it says, !s (or !*, which pulls everything from the previous command line except the echo).

^find[^replace[^append]]

It looks for the first occurance of [find] in the last command and substitutes it with [replace] and appends the [append]. Anything you don't specify defaults to empty.

Example:
~% touch CapitalizedFilename
~% mv zCapitalizedFilename lowercase
mv: cannot stat `zCapitalizedFilename': No such file or directory
zsh: exit 1 mv zCapitalizedFilename lowercase
~% ^z
mv CapitalizedFilename lowercase
~% mz lowercase
zsh: command not found: mz
zsh: exit 127 mz lowercase
~% ^z^v^ lower_case
mv lowercase lower_case


Be sure to take a look at your shell's man-page for history expansion, there's a lot of useful stuff in there. You can expand particular commands, so !-3 expands to the third-most-recent command. You can often modify the expansion with ':'-modifiers. :s does a regex replacement, :2-4 selects the second through fourth words, :1* selects all but the first word, :Q de-quotes a word, etc.

pgrep Again

pgrep is same as
ps -A | grep firefox | awk '{ print $1}'
OR
ps -A | awk '/firefox/ { print $1} '

Grep to Kill?

kill `ps -A | grep firefox | awk '{ print $1}'`

OR

pkill firefox

pkill is to kill what pgrep is to grep.

OR

killall -9 firefox

killall doesn't have the same behavior across all Unix/Linux/BSD variants.

OR

pgrep firefox | xargs kill -9

Background Processes

You can put a process in the background after you've started it with CTRL-Z followed by bg. Also check out fg, bg, jobs.

A way to background a process and redirect the output after it's started...

If you've got screen running before you started the process, you can hit CTRL-A followed by D to detach. It will keep running in the background, keeping output to itself, even if you log out. To reattach, type screen -r. (Haven't checked this!!!)

Signatures

cowsay and figlet are interesting commands for generating signatures.

CTRL+L for Clearscreen

It is a feature of readline, which is used by bash and many other shells and other cli-based programs. You can customize key bindings in your ~/.inputrc file. Google it. That screen clearing readline command is clear-screen.

More Interesting Commands

http://www.shell-fu.org/lister.php?top

See also... » 10.1 Linux Commands I Can't Live Without

» Effective Use of VIM - Part 2

» Effective Use of VIM - Part 1

» Placing Adsense in Blogger Post

» Unlock a PC on Windiows XP ???

» A Hole through the Centre of the Earth


ATOzTOA : Latest Headlines


60 comments:

PaulRay said...

Thanks so much!
I've been a Linux (ubuntu) user for over a year and some of these were new to me.
I appreciate your effort to educate us.

Keith Pickett said...

I want to interject something about CTRL-S from the command line. In most, if not all terminals, CTRL-S will stop (or pause) activity in a terminal. Bring up a terminal (xterm). Type something like "ls -al" and then CTRL-S immediately. If it's a directory that doesn't contain a lot of files it will list files quickly in long format. If it has a lot of files, you will notice the scrolling stop when you enter CTRL-N. If you try to type anything else, nothing happens. The way to get around this is to enter CTRL-Q.
I guess i'm a little confused about your post above regarding the use of CTRL-S. Maybe I missed something obvious.
Anyway, if your terminal locks up for no reason, at least try CTRL-Q to undo it. This, by the way, goes back to the early days of terminals when programmers had to use this to pause directory listings and data output being echoed. We didn't have the use of scrollbars. ;-)
Hope this helps someone.

Keith Pickett said...

Sorry, I have to share some other helpful command line search examples. If your shell is bash (or ksh), you can set the command line mode to either vi or emacs. I prefer vi. To set it, just type:
# set -o vi

You can add this to your .bashrc or .profile (for ksh).

At the command line, you can search for previously used commands just like you can search for terms in vim. For example, if you have run several php scripts from the command line, then search for each of them using the following command(s):
Note:
ESC = tap the Escape key
ENTER = Enter key

#ESC /p ENTER

You will now see a command with the letter "p" in it. As in Vim, use the "j" and "k" keys to scroll up and down the list. Now, searching for only one letter (like we did with just "p") could yield a lot of files. If you instead enter:
ESC /php ENTER

(Note: Wildcards like * and ? are accepted)

That will yield results closer to what you are searching for. Again, use "j" and "k" to cycle the list. Once you found the command you want to re-run, just hit ENTER to execute it.

Or... while scrolling the list,just enter search slash ( "/" ) to search for a new term.
For example, you have entered ESC /php and are scrolling the list, but decide to search for the term "perl" instead. All you have to do is enter /perl ENTER and it will list all the previous commands with the term "perl" in it.

To just cycle through your most recent commands,
just do the following:

#ESC k
You may have to tap "k" a second time. You will see your most recent commands cycling through. To scroll back down the list, tap "j".

Not to confuse you further, you can go back and re-use the previous search term by just entering:
# ESC / ENTER
You should see your most recent search results scrolling.


Hope this helps.

Keith Pickett said...

In my previous post above, the "#" sign is the command prompt and should not be typed in. Sorry for any confusion.

Angelina William said...

very nice and Informative, preparing for linux Certification, wanted to learn some linux Commands,recently joined in http://www.kerneltraining.com/linux-admin-training/, can anyone suggest me some books and videos.

Kuldeep said...
This comment has been removed by the author.
Manisha Jain said...

It will be available here Rajasthan Board Result

If you need RBSE Board Result wait for May month


It will be available here Rajasthan Board 10th Result

It will be available here Rajasthan Board 12th Result

It will be available here RBSE 10th Result

It will be available here RBSE 12th Result




I want to know about HSSC Clerk Result Date. Will board cancel HSSC Result exam or not???

We hope you like Haryana Clerk Result Information given here. We will update it soon.

Board has released HSSC Patwari Result Information given here. Document verification date 27 April 2017

We hope you like Delhi Police Admit Card Information given here. We will update it soon.

Pradeep Saini said...
This comment has been removed by the author.
Nodia & Publication said...

Great information, preparing for a Linux education, Good to see this GATE Online Test Series (for EC, EE, ME branches only)
Thanks for sharing

GAURAV GUPTA said...

MP Vyapam Sub Engineer Result 2017 is going to declare soon at main webportal . candidate can check result online mode only . The exam was conducted in the month of July .

John Fernando said...

Easy to access the old result of Lottery for Nagaland : Nagaland Lotteries old result by just clicking on this link

The old lottery result of the Sambad are as follows : Lottery Sambad old result

We will get the old lottery result of Mizoram on our finger tip :Mizoram Lotteries old result through clicking this link

Nice to get the old lottery result of Sikkim : Sikkim Lotteries old result on this click

John Fernando said...

We will get the old lottery result of Sambad on our finger tip :Lottery Sambad old result through clicking this link

Nice to get the old lottery result of Nagaland: Nagaland Lotteries old result on this click

Easy to access the old result of Lottery for Sikkim: Sikkim Lotteries old result by just clicking on this link

The old lottery result of the Mizoram are as follows : Mizoram Lotteries old result

John Fernando said...

It is very good information that we will get details : Bigg Boss 11 on one single click

Seems to nice to watch the show :Watch Bigg Boss 11 on our finger touch

We will get details of contestant of the Big Boss show :Bigg Boss 11 Contestants on single shot

Nice to hear the details such as Voot Bigg Boss 11 of the show.

The complete details of audition of this show :Bigg Boss 11 Audition will give complete information

Post a Comment