Linux Commands I Hardly Knew

Saturday, June 28, 2008

Linux Commands I Hardly Knew

Quickly Find a PID with pgrep

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria.

pgrep ssh
- This will list all PIDs associated with the ssh process.

Execute The Last Executed Command

This will execute the last command you used on the command line.

Isn't the UP arrow for that?

The !! command is very useful when you forget to start a command with sudo :
apt-get update
sudo !!

Execute The Last Command Starting With ...

If you want to execute a command from history starting with the letter S you can use the following:

- This will execute the last command used on the command line that started with s.

Last Argument

You can use the last argument from the last command by refering to it as $_

so you only need to type a long path like this once:
cp assignment.htm /home/phill/reports/2008/
cd $_
to go straight to the 2008 folder as well.

You can use this however you like. Always the last argument of the command above.

Like using $_ for the last argument of the last command, you can also hit ALT+. to quickly paste it at the cursor.

Run a Command Repeatedly and Display the Output

watch runs a command repeatedly, displaying its output. This allows you to watch the program output change over time. By default, the program is run every 2 seconds. watch is very similar to tail.

watch -d ls -l
- This will watch the current directory for any file changes and highlight the change when it occurs.

Save Quickly in VI/VIM

Save and quit the file you’re editing in vi by exiting insert mode, holding shift, and hitting z twice (ZZ). That is quick or not, it depends.

You can use :wq OR more easily :x

Quickly Log Out of a Terminal

You can quickly log out of a terminal session by using: CTRL+D

Navigate to the Last Directory You Were In

cd –
will take you to the last directory you were in.

Make Parent Directories the Smart Way

mkdir -p /home/adam/make/all/of/these/directories/ will create all directories as needed even if they do not exist. Why waste time doing something silly like: mkdir make ; cd make ; mkdir all ; cd all ; mkdir of ; cd of … you get the point. Use mkdir -p ! This is one command I really lacked.

Delete the Entire Line

If you’ve just typed a long string of commands that you don’t need to enter anymore, delete the entire line by using: CTRL+U,K. That is not easy. CTRL+U deletes whatever is to the left of the cursor and CTRL+K deletes what is to the right. If you are at the end of command, CTRL+U will do. Especially if you start typing password and you make a mistake. Alternatively, you can use CTRL+C, which discards the current typed command, and gives you a new line.
CTRL+L will clear the screen.

Set the Time stamp of a File

touch -c -t 0801010800 filename.c will show the time stamp as 2008-01-01 8:00. The format is (YYMMDDhhmm).

Command to File

fc will open the last command from your shell history in the default editor. You can also specify a text editor. You can add a history line number or the first few letters of the most recent command.
fc -e kate wget
- will open kate with the last shell command starting with wget. You can edit the command, and when you save and close kate, the command will execute.


Ending a command with & runs the command with a new PID, releasing the command line back to you. Useful for running a background process.

Resetting your session

Instead of killing and re-starting your terminal session, you can merely type the command reset. This will reset your terminal back to its defaults, clear the screen, and everything will be as it was before.

Source :

You have unchartered commands, pls share...

The post got lots of comments across the web. I have compiled the comments and is posting it ...

» Linux Commands I Hardly Knew - Reloaded

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


Anonymous said...


Ending a command with & runs the command with a new PID, releasing the command line back to you. Useful for running a background process.

Take out "with a new PID", and it might make more sense. Everything you run gets its own PID, unless you exec it.

Oh, and "$_" is not quite as good as "!$", which you can add with one hand. Try out "!!" also.

In addition to "cd -", try pushd and popd.

Will said...

If you're working on some unixes and you need the watch command but don't have it available, it can easily be scripted with something like the following:

while true;
do $1&&sleep $2&&clear;

Unknown said...

Another way to delete a command you wrote out but don't need to enter any more is: esc then backspace. It should instantly delete the line.

ErikHK said...

"Isn't the UP arrow for that?"
I think it's more convenient to use Ctrl-P/N/F/B instead of the arrows (like in emacs), so you don't have to move your hands so much.

Maex said...

:x and :wq are NOT the same.
:w always writes the file and :q quits. So :wq ist write&quit.
:x however checks if the file has been modified, if so it does a :wq else only a :q
This is most useful to not mess up modification times on files if you haven't cheanged anything.
An alias for :wq is ZZ

Btw. some of the "unix commands" are not commands but specific to a shell (bash in your case). And thet depend on the mode you run the shell in (like emacs or vi).

atoztoa said...

Yeah, got it... :)

The Title must be "Linux Bash/Shell Commands I Hardly Knew".

You can use CRTL+R and start typing a command, if what u type matched something from history, that command will be shown. Very easy, if you had typed a very long command the previous day, like CVSROOT.

Unknown said...

Nice post. Many of the things you pointed out were specifically BASH related. I have recently come across a few other good post on BASH that you and your readers might enjoy:

Bash EMACS editing mode

Bash Vi editing mode

Bash history tips

These include handy cheat sheets as well.


Chase Driven said...

I've been sitting here on my MacBook trying a couple of these which address my own habits (!!, cd -, etc) and am pleasantly surprised to see they haven't been disabled or hobbled. Nice!

Unknown said...

Dude, thats pretty cool. thanks for the insight!


lee said...

One of my favorites is:


This puts the selected event into the command line buffer, but does
not execute the command. You can use editing the command before
executing. For instance:


moves the last 'cp' command into the command line buffer. Try
it, you'll like it.

By the way


is the meta key as in:

<bashManual>The text M-k is read as `Meta-K' and
describes the character produced when the Meta key (if you have one)
is depressed, and the <k> key is pressed. The Meta key is
labeled <ALT> on many keyboards. On keyboards with two keys
labeled <ALT> (usually to either side of the space bar), the
<ALT> on the left side is generally set to work as a Meta
key. The <ALT> key on the right may also be configured to work
as a Meta key or may be configured as some other modifier, such as a
Compose key for typing accented characters.</bashManual>

Lochinvar said...

how about !:# where # is the part of the last command
sudo apt-get install xxxx
!:0 sudo
!:1 apt-get
!:2 install
!:3 xxxx
useful for typos and repeated actions.

Anonymous said...

Actually, the command I find most useful is "sudo init 0"


klang said...

Thy this for size:

mkdir -p /home/adam/make/{all,some}/of/{these,those}/directories/<

Billco said...

I've been Linuxing for over a decade and even I learned a few bits today. Kudos!

Draj said...

Kudos! I have some tips aswell.

CTRL-r search string
Will search your history file for what you type.

![history #]
Will execute the command as numbered shown with the 'history' command.

CTRL-a and CTRL-e will move cursor to beginning and the end of the commandline.


Anonymous said...

from my experience !! is really dangerous..
You really should be able to see whatever it is you are typing into you system..

just be careful when using it

elPadawan said...

Already knew them. True, the post should be "Shell/Bash/Vi/Miscellaneous stuffed used in my command line under a *NIX I Hardly knew". But it starts to be unreadable at this point :).

One thing about ending with a "&". If you use "&&" instead of "&", it will "run the next one only if the previous command completed successfully".

oh, and not sure if this one is just bash or all shells: the use of {,}.

mv directory.{current,backup} will result in "mv directory.current directory.backup". Have fun.

Anonymous said...

Great list. I've been using Unix/Linux for a long time, and some of these are new to me.


An alternative to "pgrep" could be:

ps -ef | grep 'name-of-process'
F.ex.: ps -ef | grep apache2

...will not only list PID, but also name of user that started the process together with other details.

Daniel Wellington Coupons said...

1 800 GET LENS Coupon
The GET LENS Coupon Chronicles
You’ll locate this digital mirror on every item page. Glasses work by positioning a lens before the eye to correct the focus. The big reason for people needing glasses is a result of the eye being unable to completely focus an image on the retina.

Designer Outlet said...

ContactsAmerica Coupons
Make an account in case you do not have one, and you may start checking out. Payment is made through PayPal (which likewise accepts all significant credit cards). Then and just then should you finish your transaction at checkout. Benefit from our coupon site for anything you have to buy the largest online transaction. Enter the power for those lenses, then you can begin the checkout procedure. The ticketing system is not going to support that. Service units ought to be new units or very compact units who don’t have sufficient adult support to run a booth, but still wish to take part in Scout Fair

RRB News said...

The Railway Recruitment Board is a Government Organization. It organizes various Recruitment each year. Now all of you can check out details about
RRB Group D Admit Card.
It is necessary to Download RRB Mumbai Group D Admit Card 2018.
Railway Recruitment Board (RRB) conducts computer based examination for recruitment of ALP & Technician posts. RRB has released centralized notification 01/2018 for total 26502 vacancies.
RRB ALP & Technician Recruitment 2018 RRB Result 2018 will be held in two stages followed by Aptitude Test (if applicable) and Document Verification RRB Patna Admit Card 2018. First Stage CBT is scheduled to be held in the month of May and June 2018.

Iain said...

Great list. I think I need to save this for future use :)

Post a Comment