<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.srevilak.net/wiki/index.php?action=history&amp;feed=atom&amp;title=Intro_to_the_Command_Line%3A_Part_II</id>
	<title>Intro to the Command Line: Part II - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.srevilak.net/wiki/index.php?action=history&amp;feed=atom&amp;title=Intro_to_the_Command_Line%3A_Part_II"/>
	<link rel="alternate" type="text/html" href="https://www.srevilak.net/wiki/index.php?title=Intro_to_the_Command_Line:_Part_II&amp;action=history"/>
	<updated>2026-04-03T19:05:46Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://www.srevilak.net/wiki/index.php?title=Intro_to_the_Command_Line:_Part_II&amp;diff=91&amp;oldid=prev</id>
		<title>SteveR: initial revision</title>
		<link rel="alternate" type="text/html" href="https://www.srevilak.net/wiki/index.php?title=Intro_to_the_Command_Line:_Part_II&amp;diff=91&amp;oldid=prev"/>
		<updated>2014-04-13T15:51:51Z</updated>

		<summary type="html">&lt;p&gt;initial revision&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Intro to The Command Line (Part II) =&lt;br /&gt;
&lt;br /&gt;
From April 1, 2014&lt;br /&gt;
&lt;br /&gt;
== Keyboard Shortcuts ==&lt;br /&gt;
&lt;br /&gt;
Linux has a set of &amp;quot;virtual consoles&amp;quot;.  Try pressing &amp;quot;Ctrl-Alt-1&amp;quot;; you&lt;br /&gt;
should see a text login screen (aka virtual console).  Now press&lt;br /&gt;
&amp;quot;Ctrl-Alt-2&amp;quot;; you&amp;#039;ll see another virtual console.  You can press&lt;br /&gt;
&amp;quot;Ctrl-Alt-7&amp;quot; to get back to the desktop GUI (the graphics environment&lt;br /&gt;
runs on its own virtual console).&lt;br /&gt;
&lt;br /&gt;
What other keyboard shortcuts are available?  Keyboard shortcuts are&lt;br /&gt;
present at (at least) three levels.   First, we have shortcuts that&lt;br /&gt;
are present regardless of whether or not you&amp;#039;re running a GUI.&lt;br /&gt;
Virtual console switching is one example.  Ctrl-Alt-Delete is&lt;br /&gt;
another.  (Yes, the three-fingered salute works in Linux too).&lt;br /&gt;
&lt;br /&gt;
Second, there are shortcuts from the graphics environment.  For&lt;br /&gt;
example, Alt-TAB to switch programs.  Your desktop should have a way&lt;br /&gt;
to list (and change) the set of keyboard shortcuts it provides.&lt;br /&gt;
&lt;br /&gt;
Finally, there are shortcuts particular to individual programs.  Since&lt;br /&gt;
we&amp;#039;re talking about the command line, we&amp;#039;ll focus on keyboard&lt;br /&gt;
shortcuts available at the terminal.&lt;br /&gt;
&lt;br /&gt;
Try this:&lt;br /&gt;
&lt;br /&gt;
  $ stty -a&lt;br /&gt;
  speed 38400 baud; rows 40; columns 85; line = 0;&lt;br /&gt;
  intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = &amp;lt;undef&amp;gt;; eol2 = &amp;lt;undef&amp;gt;;&lt;br /&gt;
  swtch = &amp;lt;undef&amp;gt;; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;&lt;br /&gt;
  lnext = ^V; flush = ^O; min = 1; time = 0;&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
The stty command prints terminal settings, and I&amp;#039;ve shown some of its&lt;br /&gt;
output above.  &amp;quot;kill = ^U&amp;quot; shows one of the keyboard bindings.  &amp;quot;^U&amp;quot;&lt;br /&gt;
is shorthand for Control-U, and &amp;quot;kill&amp;quot; means &amp;quot;remove all characters to&lt;br /&gt;
the left of the cursor.&amp;quot;  Try typing some stuff at the command line,&lt;br /&gt;
then press Ctrl-U; the entire line should be deleted.&lt;br /&gt;
&lt;br /&gt;
The shell also provides its own set of keyboard shortcuts.  Here are&lt;br /&gt;
some useful keyboard shortcuts for bash:&lt;br /&gt;
&lt;br /&gt;
* up and down arrows cycle through command history&lt;br /&gt;
* Ctrl-F and Ctrl-B move forward and backward by one word&lt;br /&gt;
* Ctrl-A moves the cursor to the beginning of the line&lt;br /&gt;
* Ctrl-E moves the cursor to the end of the line.&lt;br /&gt;
* Ctrl-L clears the screen.&lt;br /&gt;
&lt;br /&gt;
To find others, have a look at the &amp;quot;READLINE&amp;quot; section in &amp;lt;tt&amp;gt;man&lt;br /&gt;
bash&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Finding files ==&lt;br /&gt;
&lt;br /&gt;
Linux has two programs to help you find files on your hard drive:&lt;br /&gt;
&amp;lt;tt&amp;gt;find&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;locate&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Find works by examining files as the command is run; locate relies on&lt;br /&gt;
a per-built database of file names (most linux distributions include a&lt;br /&gt;
cron job to update locate&amp;#039;s database).&lt;br /&gt;
&lt;br /&gt;
Here a few commands to try&lt;br /&gt;
&lt;br /&gt;
 find /etc/&lt;br /&gt;
 find /etc/ -type f&lt;br /&gt;
 find /etc/ -type d&lt;br /&gt;
&lt;br /&gt;
The first command shows all files and directories in /etc.  The second&lt;br /&gt;
shows all regular files under /etc.  The third shows all&lt;br /&gt;
subdirectories under etc.&lt;br /&gt;
&lt;br /&gt;
You&amp;#039;ll likely see a few lines that look like this&lt;br /&gt;
&lt;br /&gt;
  find: `/etc/ssl/private&amp;#039;: Permission denied&lt;br /&gt;
&lt;br /&gt;
Here, find is saying &amp;quot;you don&amp;#039;t have permission to look in&lt;br /&gt;
/etc/ssl/private&amp;quot;, and we can use ls to verify that find is correct&lt;br /&gt;
&lt;br /&gt;
  $ ls -ld /etc/ssl/private&lt;br /&gt;
  4 drwx--x--- 2 root ssl-cert 4096 Apr 26  2013 /etc/ssl/private/&lt;br /&gt;
&lt;br /&gt;
Notice that this directory is owned by root, and only root has the &amp;quot;r&amp;quot;&lt;br /&gt;
permission bit.&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s another command to try&lt;br /&gt;
&lt;br /&gt;
  find /etc/ | head -10&lt;br /&gt;
&lt;br /&gt;
This is an example of a &amp;#039;&amp;#039;pipeline&amp;#039;&amp;#039;.  When you see a pipe character&lt;br /&gt;
(&amp;quot;|&amp;quot;) this means that the output of the command on the left is fed&lt;br /&gt;
(&amp;quot;piped&amp;quot;) to the command on the right.  We&amp;#039;ve already seen what&lt;br /&gt;
&amp;lt;tt&amp;gt;find /etc&amp;lt;/tt&amp;gt; does; &amp;lt;tt&amp;gt;head -10&amp;lt;/tt&amp;gt; is a command that prints&lt;br /&gt;
the first 10 lines of input, and discards the rest.  By combining the&lt;br /&gt;
two, we get a listing that contains ten files.&lt;br /&gt;
&lt;br /&gt;
Here&amp;#039;s another example of a pipeline&lt;br /&gt;
&lt;br /&gt;
  find /etc/ | grep mlocate&lt;br /&gt;
&lt;br /&gt;
Now, we&amp;#039;re taking find&amp;#039;s output and feeding it to &amp;lt;tt&amp;gt;grep mlocate&amp;lt;/tt&amp;gt;.  &lt;br /&gt;
Grep is a pattern matching command.  In this case, grep reads the&lt;br /&gt;
output of find, and prints lines that contain &amp;quot;mlocate&amp;quot;.  You should&lt;br /&gt;
see one file that meets this condition&lt;br /&gt;
&lt;br /&gt;
  /etc/cron.daily/mlocate&lt;br /&gt;
&lt;br /&gt;
We&amp;#039;ll return to this file in a moment.&lt;br /&gt;
&lt;br /&gt;
grep can also find patterns inside of files.  For example&lt;br /&gt;
&lt;br /&gt;
  $ grep www /etc/group&lt;br /&gt;
&lt;br /&gt;
examines the file &amp;quot;/etc/group&amp;quot;, and prints lines containing &amp;quot;www&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
== A look at /etc/cron.daily/mlocate ==&lt;br /&gt;
&lt;br /&gt;
/etc/cron.daily/mlocate is a cron job: a script that&amp;#039;s run on a&lt;br /&gt;
schedule.  (&amp;quot;cron&amp;quot; is the name of the linux service that runs&lt;br /&gt;
scheduled jobs).&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s look at the contents of this file&lt;br /&gt;
&lt;br /&gt;
  less /etc/cron.daily/mlocate&lt;br /&gt;
&lt;br /&gt;
mlocate is a &amp;quot;shell script&amp;quot;; a set of commands that are placed in a&lt;br /&gt;
file, and executed by a shell.  Let&amp;#039;s spend a few moments dissecting&lt;br /&gt;
this script:&lt;br /&gt;
&lt;br /&gt;
  #! /bin/bash&lt;br /&gt;
&lt;br /&gt;
The first line of every script is special: it specifies which&lt;br /&gt;
interpreter runs the file.  Here, we see that the interpreter is&lt;br /&gt;
/bin/bash.  The operating system actually run this script as&lt;br /&gt;
&amp;quot;/bin/bash /etc/cron.daily/mlocate&amp;quot;.&lt;br /&gt;
  &lt;br /&gt;
  set -e&lt;br /&gt;
  &lt;br /&gt;
&amp;lt;tt&amp;gt;set -e&amp;lt;/tt&amp;gt; tells the shell that it should exit immediately if any&lt;br /&gt;
scripted command fails.&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;nowiki&amp;gt;[ -x /usr/bin/updatedb.mlocate ] || exit 0&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
This line deserves a little bit of discussion.  The &amp;quot;||&amp;quot; is&lt;br /&gt;
pronounced &amp;quot;or&amp;quot;; it means &amp;quot;run the commands separated by || until one&lt;br /&gt;
of them succeeds&amp;quot;.  The shell runs &amp;quot;&amp;lt;nowiki&amp;gt;[&amp;lt;/nowiki&amp;gt; -x /usr/bin/updatedb.mlocate ]&amp;quot;;&lt;br /&gt;
if that succeeds, then the shell will skip the rest of the command&lt;br /&gt;
line.  On the other hand, if that command doesn&amp;#039;t succeed (i.e.,&lt;br /&gt;
there&amp;#039;s no updatedb.mlocate executable), then the shell runs &amp;quot;exit 0&amp;quot;&lt;br /&gt;
(which ends the script).&lt;br /&gt;
&lt;br /&gt;
&amp;quot;[&amp;quot; is actually a program, and you can see it with &amp;quot;ls /usr/bin/[&amp;quot;.&lt;br /&gt;
Do &amp;quot;man [&amp;quot; to see what it does.  The &amp;quot;[&amp;quot; command is called &amp;quot;test&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The next block contains a set of &amp;quot;if statements&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
  if which on_ac_power &amp;gt;/dev/null 2&amp;gt;&amp;amp;1; then&lt;br /&gt;
      ON_BATTERY=0&lt;br /&gt;
      on_ac_power &amp;gt;/dev/null 2&amp;gt;&amp;amp;1 || ON_BATTERY=$?&lt;br /&gt;
      if [ &amp;quot;$ON_BATTERY&amp;quot; -eq 1 ]; then&lt;br /&gt;
  	exit 0&lt;br /&gt;
      fi&lt;br /&gt;
  fi&lt;br /&gt;
  &lt;br /&gt;
The general format is &amp;quot;if CONDITION; then SOME-COMMANDS; fi&amp;quot;.  (&amp;quot;fi&amp;quot;&lt;br /&gt;
ends the &amp;quot;if&amp;quot; statement&amp;quot;).  Our condition is &amp;quot;which on_ac_power&lt;br /&gt;
&amp;gt;/dev/null 2&amp;gt;&amp;amp;1&amp;quot;.  There&amp;#039;s a lot happening in that statement, so let&amp;#039;s&lt;br /&gt;
take it piece by piece.&lt;br /&gt;
&lt;br /&gt;
* on_ac_power is a program.  It exits with a success code when your computer is running on ac power, and exits with an error code when your computer is running on batteries.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;which on_ac_power&amp;quot; finds the full path to the on_ac_power command. (Try typing &amp;quot;which on_ac_power&amp;quot; into the terminal, and see what happens).  Essentially, this checks to see if the on_ac_power command is installed on your system.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;&amp;gt;/dev/null&amp;quot;.  Sends the output of &amp;quot;which on_ac_power&amp;quot; to /dev/null (which throws the output away).  That makes sense in this case, because we don&amp;#039;t care about the output of the command, we&amp;#039;re only interested in whether it succeeds or fails.&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;2&amp;amp;&amp;gt;1&amp;quot;.  This means &amp;quot;send the output of file descriptor 2 to whereever the output of file descriptor 1 is going&amp;quot;.  File descriptor 2 is called &amp;quot;stderr&amp;quot;; this is a program&amp;#039;s error stream. File descriptor 1 is called &amp;quot;stdout&amp;quot;; this is a programs normal output stream.  &amp;quot;&amp;gt;/dev/null&amp;quot; tells the shell to discard the standard output stream and &amp;quot;&amp;gt;/dev/null 2&amp;amp;&amp;gt;1&amp;quot; tells the shell to discard standard error too.&lt;br /&gt;
&lt;br /&gt;
This general name for &amp;quot;&amp;gt;&amp;quot; is &amp;quot;redirection&amp;quot;.  The bash manual page has&lt;br /&gt;
a section (&amp;quot;REDIRECTION&amp;quot;) devoted to redirection operators.&lt;br /&gt;
&lt;br /&gt;
As a whole, this set of if statements is saying &amp;quot;see if we&amp;#039;re on&lt;br /&gt;
battery power, and if so, exit&amp;quot;.  In other words, don&amp;#039;t run this job&lt;br /&gt;
if we&amp;#039;re running on battery power.&lt;br /&gt;
&lt;br /&gt;
  ##&lt;br /&gt;
  &lt;br /&gt;
Lines starting with &amp;quot;#&amp;quot; are comments.&lt;br /&gt;
&lt;br /&gt;
  LOCKFILE=&amp;quot;/var/lib/mlocate/daily.lock&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This line is a variable assignment which gives the variable LOCKFILE&lt;br /&gt;
the value &amp;quot;/var/lib/mlocate/daily.lock&amp;quot;.  With that assignment, we can&lt;br /&gt;
use &amp;quot;$LOCKFILE&amp;quot; (the variable) in place of the actual value.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  if [ -e &amp;quot;$LOCKFILE&amp;quot; ]; then&lt;br /&gt;
      echo &amp;gt;&amp;amp;2 &amp;quot;Warning: $LOCKFILE present, not running updatedb.&amp;quot;&lt;br /&gt;
      exit 1&lt;br /&gt;
  else&lt;br /&gt;
      touch &amp;quot;$LOCKFILE&amp;quot;&lt;br /&gt;
  fi&lt;br /&gt;
&lt;br /&gt;
The block of code above ensures that only one copy of mlocate is&lt;br /&gt;
running at any given time.  If the &amp;quot;$LOCKFILE&amp;quot; exists, then print a&lt;br /&gt;
warning and stop.  Otherwise, create the lockfile and continue.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  # See ionice(1)&lt;br /&gt;
  if [ -x /usr/bin/ionice ] &amp;amp;&amp;amp;&lt;br /&gt;
      /usr/bin/ionice -c3 true 2&amp;gt;/dev/null; then&lt;br /&gt;
      IONICE=&amp;quot;/usr/bin/ionice -c3&amp;quot;&lt;br /&gt;
  fi&lt;br /&gt;
&lt;br /&gt;
This sections checks to see if the ionice program is installed.  If&lt;br /&gt;
so, we&amp;#039;ll use it to reduce the priority of mlocate&amp;#039;s I/O operations&lt;br /&gt;
(so we don&amp;#039;t completely hog the disk).&lt;br /&gt;
  &lt;br /&gt;
  $IONICE /usr/bin/updatedb.mlocate&lt;br /&gt;
&lt;br /&gt;
The last line is where all the work is done.  updatedb.mlocate builds&lt;br /&gt;
the file database for &amp;lt;tt&amp;gt;locate&amp;lt;/tt&amp;gt;.  Essentially, this command runs&lt;br /&gt;
&amp;lt;tt&amp;gt;find&amp;lt;/tt&amp;gt; on the entire filesystem, and generates an index of the&lt;br /&gt;
files that find finds.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Linux Class]]&lt;/div&gt;</summary>
		<author><name>SteveR</name></author>
	</entry>
</feed>