Actions

Linux CLI commands and tips

From zen2

Navigate

  • Ctrl-a Move to the start of the line.
  • Ctrl-e Move to the end of the line.
  • Alt-] x Moves the cursor forward to the next occurrence of x.
  • Alt-Ctrl-] x Moves the cursor backwards to the previous occurrence of x.
  • Ctrl-u Delete from the cursor to the beginning of the line.
  • Ctrl-k Delete from the cursor to the end of the line.
  • Ctrl-w Delete from the cursor to the start of the word.
  • Ctrl-y Pastes text from the clipboard.
  • Ctrl-l Clear the screen leaving the current line at the top of the screen.
  • Ctrl-x Ctrl-u Undo the last changes. Ctrl-_
  • Alt-r Undo all changes to the line.
  • Alt-Ctrl-e Expand command line.
  • Ctrl-r Incremental reverse search of history.
  • Alt-p Non-incremental reverse search of history.
  • !! Execute last command in history
  • !abc Execute last command in history beginning with abc
  • !n Execute nth command in history
  • ^abc^xyz Replace first occurrence of abc with xyz in last command and execute it

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:

!s

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

Ampersand

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.

Find and Replace in previous command

^find[^replace[^append]]

It looks for the first occurrence of [find] in the last command and substitutes it with [replace].

stupid example:

lulz@localhost:~$ cp /mnt/networkshare/pub/tehcodes.tar.gz .
cp: cannot stat '/mnt/networkshare/pub/tehcodes.tar.gz': No such file or directory
lulz@localhost:~$ ^teh^the
cp /mnt/networkshare/pub/thecodes.tar.gz .
lulz@localhost:~$

Another 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