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

4 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.

Post a Comment