Starting, Exiting, Reading and Writing Files in Emacs
Starting, Exiting, Reading and Writing Files in Emacs
Ctl-x Ctl-w Write out the file giving a new name when prompted
Ctl-x Ctl-s Write out all files currently being worked on and exit
Ctl-x Ctl-c Exit after being prompted if there any unwritten modified files
arrow keys Use the arrow keys for up, down, left and right; or:
Ctl-s Search forward for prompted for pattern, or for next pattern
Ctl-r Search backwards for prompted for pattern, or for next pattern
Ctl-space Mark the beginning of the selected region; the end will be at the cursor position
Ctl-w Yank (cut) the current marked region and put it in buffer
Ctl-y Paste at the current position the yanked line or lines from the buffer
Command Description
A shell is a command line interpreter which can constitute the user interface for terminal windows. It can also be used
as a mechanism to run scripts, even in non-interactive sessions without a terminal window, as if the commands were
being typed in.
at the command line accomplishes the same thing as running the following script:
#!/bin/bash
The #!/bin/bash at the beginning of the script should be familiar to anyone who has developed any kind of script in
UNIX environments. Following the magic #! characters goes the name of whatever scripting language interpreter is
tasked with executing the following lines. Choices
include /usr/bin/perl, /bin/bash, /bin/csh, /usr/bin/python and /bin/sh.
Linux provides a wide choice of shells; exactly what is available to use is listed in /etc/shells; e.g. on one system we
get:
1
$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
/bin/zsh
Most Linux users use the default bash shell, but those with long UNIX backgrounds with other shells may want to
override the default. It is worth reviewing the main choices in the historical order of introduction.
Any command shell can be invoked merely by typing its name at the command line. A user’s default shell can be
changed with the chsh utility.
We will concentrate on bash, which is generally the default shell under Linux.
Kinds of shells
A login shell is one requiring a password (logging in)
An interactive shell is one in which the standard input/output streams are connected to terminals
A non-interactive shell is one in which the standard input/output streams may be connected to a process,
etc.
Initialization
Interactive shells
Login shells:
if /etc/profile exists, source it
if ~/.bash_profile exists, source it
else if ~/.bash_login exists, source it
else if ~/.profile exists, source it
on exit, if ~/.bash_logout exists, source it
Non-login shells:
if ~/.bashrc exists, source it
Non-interactive shells
Despite what the man page says, it seems to be the same as interactive shells.
Note that by default, most distributions include a system-wide file (usually /etc/bashrc) from the user’s ~/.bashrc.
Aliases permits custom definitions. Typing alias with no arguments gives the list of defined aliases. unalias gets rid
of an alias.
8
alias l=’ls -laF’
Environment variables are not limited in length or number. Lots of applications use them, for instance, in order to set
default values for configuration options.
Note: Putting ./ in your path is a security risk; an unfriendly user might substitute an executable which could be quite
harmful. However, if you are on a single user system, you may want to violate this recommendation.
Type env (or export) to get a list of presently exported environment variables, set to get the complete set of
variables.
Some variables to set (use whatever values make sense for you!):
EDITOR=/usr/bin/emacs
CD_PATH=$HOME:/tmp
LS_COLORS="......"
PAGER=/usr/bin/less
HISTSIZE=1000
An environment variable must be exported to propagate its value to a child process. You can do either of the
following:
$ export VAR=value
You can also make one or more environment variables take effect for just one command:
The default command line prompt is $ for normal users and # for the root or superuser.
Customizing the command line prompt is as simple as modifying the value of the environment variable PS1. For
example, to set it to display the hostname, user and current directory:
2
$ PS1="\h:\u:\w>"
c7:coop:/tmp>
Besides the aesthetic value of having a prettier prompt than the default value, embedding more information in the
prompt can be quite useful. In the example given we have shown:
The machine name - this is useful if you run command line windows on remote machines from your desktop;
you can always tell where you are, and this can avoid many errors.
The user name - this is particularly important if you are running as a superuser (root) and can help you avoid
errors where you take what you think is a benign action and wind up crippling your system.
The current directory - it is always important to know where you are. You certainly do not want to do
something like rm * in the wrong directory.
Here is a table with some of the possible special characters that can be embedded in the PS1 string:
\n Newline
\u User coop
\h Hostname c7
Note you can embed any other string you like in the prompt.
A number of characters have a special meaning and cause certain actions to take place. If you want to print them
directly, you usually have to prefix them with a backslash (\) or enclose them in single quotes.
| Piping
|| OR list
; Separate commands
{} Lists
~ Usually means $HOME
$ Parameter substitution
’’ Take exactly as is
Try:
1
$ echo $HOME
$ echo \$HOME
$ echo ’$HOME’
$ echo "$HOME"
File descriptors:
0 = stdin
1 = stdout
2 = stderr
sends stdout and stderr to a file, but foo >>& file does not work ; you have to do foo >> file 2>&1
Note that foo > file 2>&1 is not the same as foo 2>&1 > file; the order of arguments is important.
A nice non-portable trick you can use in Linux is to take advantage of the device nodes:
1
/dev/stdin
/dev/stdout
/dev/stderr
Each step in a pipeline is a separate shell; i.e. there is a true pipeline. Be careful with redirection. Also |& does not
work.
The tee utility can be very useful for saving output while still looking at the screen:
1
Each step in a pipeline is a separate shell; i.e. there is a true pipeline. Be careful with redirection. Also |& does not
work.
The tee utility can be very useful for saving output while still looking at the screen:
The second form permits nesting, while the first form does not. Note that the first form has “backticks” (‘) not
apostrophes.
Arithmetic expressions may be evaluated in two different ways, using the expr utility, or the $((..)) syntax:
For x=3:
Expression Gives
echo $x + 1 3+1
Expression Gives
echo $(expr $x + 1) 4
echo $((x+1)) 4
echo $(($x + 1 )) 4
The $((..)) syntax is more modern and preferred; expr is less efficient, as it invokes an external program and is
trickier to use.
1) current directory>
(i.e. /home/coop> )
PS1='\w''>' (OR)
PS1='`pwd`>' (BETTER)
PS1='\h:\u:\w>' (OR)
PS1='\h:\u:'`pwd`'>' (BETTER)
#!/bin/bash
#/* **************** LFD201:2018-05-21 s_11/lab_pipe.sh **************** */
#/*
# * The code herein is: Copyright the Linux Foundation, 2018
# *
# * This Copyright is retained for the purpose of protecting free
# * redistribution of source.
# *
# * URL: http://training.linuxfoundation.org
# * email: [email protected]
# *
# * This code is distributed under Version 2 of the GNU General Public
# * License, which you should have received with the source.
# *
# */
#!/bin/bash
# Lab: Redirection
# $ ls /etc/passwd /etc/passwd_not
# where one file exists and the other does not.
# 1) Get the 'stdout' output of the command in one file and the
# 'stderr' output in another.
# 4) Now pipe the result of the 'ls' command into 'sort' and get the
# 'stderr' output into a separate file.
Lab 2: Redirection
$ ls /etc/passwd /etc/passwd_not
1) Get the 'stdout' output of the command in one file and the 'stderr'
output in another.
Week 2
Prev
Next
o
o
o
QUIZ • 10 MIN
Try again
Receive grade
TO PASS80% or higher
Grade
40%
View Feedback
TO PASS 80% or higher
Try again
GRADE
40%
40%
1.Question 1
Which of the following commands would add newbin, a directory in your home directory, to the PATH(Select all
answers that apply)?
PATH=$HOME/newbin:$PATH
Correct
$PATH=$PATH:$HOME/newbin
PATH=$HOME/$newbin:$PATH
PATH=$PATH:$HOME/newbin
Correct
1 / 1 point
2.Question 2
To make an environment variable (VAR) effective for only one command (foobar), you should do:
VAR=value ; ./foobar
export VAR=value ./foobar
VAR=value ./foobar
Incorrect
0 / 1 point
3.Question 3
Which of the following expressions will give the correct mathematical result (7) for x = 10 (Select all answers that
apply)?
$ echo $x - 3
$ echo $(($x - 3 ))
Correct
$ echo $(expr $x - 3)
Correct
4.Question 4
Which commands will get both the normal and error outputs of prog into afile?
Correct
0 / 1 point
5.Question 5
Which of the following commands has the correct syntax for specifying an alias?
Correct
You can have blank spaces in an alias if you use single or double quotes.
1 / 1 point
Week 2
Prev
Next
o
o
o
QUIZ • 10 MIN
Try again
Receive grade
TO PASS80% or higher
Grade
60%
View Feedback
TO PASS 80% or higher
Try again
GRADE
60%
60%
1.Question 1
Which of the following commands would add newbin, a directory in your home directory, to the PATH(Select all
answers that apply)?
PATH=$HOME/$newbin:$PATH
PATH=$HOME/newbin:$PATH
Correct
PATH=$PATH:$HOME/newbin
Correct
$PATH=$PATH:$HOME/newbin
1 / 1 point
2.Question 2
To make an environment variable (VAR) effective for only one command (foobar), you should do:
VAR=value ; ./foobar
export VAR=value ./foobar
VAR=value ./foobar
Incorrect
0 / 1 point
3.Question 3
Which of the following expressions will give the correct mathematical result (7) for x = 10 (Select all answers that
apply)?
$ echo $x - 3
$ echo $(expr $x - 3)
Correct
$ echo $(($x - 3 ))
Correct
1 / 1 point
4.Question 4
Which commands will get both the normal and error outputs of prog into afile?
foo >> file
Correct
0 / 1 point
5.Question 5
Which of the following commands has the correct syntax for specifying an alias?
Correct
You can have blank spaces in an alias if you use single or double quotes.
1 / 1 point
Here is a list of the main directories which should be present under /:
Main Directories
Director In
Purpose
y FHS?
/bin Yes Essential executable programs that must be available in single user mode
Files needed to boot the system, such as the kernel, initrd or initramfs images, and boot configuration files and
/boot Yes
bootloader programs
/home Yes User home directories, including personal settings, files, etc.
64-bit libraries required by executable binaries in /bin and /sbin, for systems which can run both 32-bit and
/lib64 No
64-bit programs
/media Yes Mount points for removable media such as CD’s, DVD’s, USB sticks etc.
Virtual pseudo-filesystem giving information about the system and processes running on it; can be used to
/proc Yes
alter system parameters
Virtual pseudo-filesystem giving information about the system and processes running on it; can be used to
/sys No
alter system parameters, is similar to a device tree and is part of the Unified Device Model
/tmp Yes Temporary files; on many distributions lost across a reboot and may be a ramdisk in memory
Director In
Purpose
y FHS?
A system should be able to boot and go into single user, or recovery mode, with only
the /bin, /sbin, /etc, /lib and /root directories mounted, while the contents of the /boot directory are needed for the
system to boot in the first place.
Many of these directories (such as /etc and /lib) will generally have subdirectories associated either with specific
applications or sub-systems, with the exact layout differing somewhat by Linux distribution. Two of
them, /usr and /var, are relatively standardized and worth looking at.
Non-essential binaries and scripts, not needed for single user mode; generally this means user applications not needed to
/usr/bin
start system
/
Header files used to compile applications
usr/include
/usr/local Local data and programs specific to the host; subdirectories include bin, sbin, lib, share, include, etc.
/var/run Information about the running system since the last boot
/var/tmp Temporary files to be preserved across system reboot; sometimes linked to /tmp
The path is a critical aspect of your environment, and is encapsulated in the PATH environment variable. On an
RHEL 7 system for a user named student, we get:
$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:\
/usr/local/sbin:/usr/sbin:/home/student/.local/bin:/home/student/bin
(Note we have had to split the path across across two lines in the output.)
When a user tries to run a program, the path is searched (from left to right) until an executable program or script is
found with that name. You can see what would be found with the which command, as in:
/usr/bin/emacs
Note that if there was a /usr/local/bin/emacs, it would be executed instead, since it is earlier in the path.
$ MY_BIN_DIR=$HOME/my_bin_dir
$ export PATH=$MY_BIN_DIR:$PATH
$ export PATH=$PATH:$MY_BIN_DIR
with the first form prepending your new directory and the second appending it to the path.
Note that the current directory is noted by ./ and the directory up one level by ../.
The current directory is never placed in the path by default. Thus, if you want to run foobar in the current directory,
you must say:
1
$ ./foobar
for it to work.
You can save changes to your path by putting them in your shell initialization file, .bashrc in your home directory.
Another useful path variable is CDPATH which is searched when you change directories. For example:
5
$ cd bin
$ export CDPATH=/usr:$CDPATH
$ cd bin
/usr/bin
Any path which begins with / is considered absolute because it specifies the exact filesystem location. Otherwise, it is
considered relative and it is implicitly assumed your current directory is prepended.
The ln program can be used to create hard links and (with the -s option) soft links, also known as symbolic links or
symlinks.
$ ln file1 file2
Note that two files now appear to exist. However, a closer inspection of the file listing shows that this is not quite true.
The -i option to ls prints out in the first column the inode number, which in UNIX filesystems is unique for each file
object. This field is the same for both of these files; what is really going on here is that it is only one file, but it has two
names. The 2 that appears later in the ls listing indicates that this inode has two links to it.
$ ls -li /bin/g*zip
which shows that gzip and gunzip are both only one program and the executable is only one file; whether it
compresses or decompresses files depends on which name it is invoked with, which is always available
as argv[0] when the program executes.
Hard links are very useful and they save space, but you have to be careful with their use, sometimes in subtle ways.
For one thing, if you remove either file1 or file2 in the above example, the inode object (and the remaining file name)
will remain, which is generally desirable.
However, if you edit one of the files, exactly what happens depends on your editor; most editors, including vi and
emacs, will retain the link by default, but it is possible that modifying one of the names may break the link and result
in the creation of two objects.
$ ln -s file1 file2
$ ls -li file1 file2
Notice file2 no longer appears to be a regular file, and it clearly points to file1 and has a different inode number.
Symbolic links take no extra space on the filesystem (unless their names are very long), as they are stored directly in
the directory inode. They are extremely convenient, as they can easily be modified to point to different places.
Unlike hard links, soft links can point to objects even on different filesystems (or partitions), which may or not be
currently mounted or even exist. In the case where the link does not point to a currently mounted or existing object,
one obtains a dangling link.
c7:/usr/share/gdb/auto-load>symlinks -rv .
Create a simple executable file with the name ls in your current directory, which we will assume to be /tmp:
1
$ cd /tmp
$ chmod +x ls
$ ./ls
but just typing ls will bring up the normal /bin/ls, which can be verified by typing which ls.
If you do:
$ export PATH=/tmp:$PATH
then typing ls will bring up your program no matter where you are sitting on the filesystem.
$ export PATH=./:$PATH
which puts the current directory first in the path, no matter where you are, or
$ export PATH=$CWD:$PATH
which will put the current working directory at this time in your future path.
Prepending your current directory to the path is generally a bad idea, as it makes trojan horses easy to implement.
Vedantham Ramachandran
Week 2
Next
o
o
o
QUIZ • 10 MIN
Try again
Receive grade
TO PASS80% or higher
Grade
20%
View Feedback
TO PASS 80% or higher
Try again
GRADE
20%
20%
1.Question 1
Which of the following pseudo-directories are empty when the system is not running (Select all answers that apply)?
/etc
/dev
Correct
/boot
/proc
Correct
/sys
0 / 1 point
2.Question 2
Which of the following statements describes the best practice?
The current directory should be appended to the path
Incorrect
0 / 1 point
3.Question 3
If file does not exist, which command will produce an error?
ln file file2
ln -s file file2
Incorrect
0 / 1 point
4.Question 4
Which command will list the partition information on the first hard disk and then exit?
fdisk -l hd1
fdisk -l /dev/sda
fdisk /dev/sda
Correct
1 / 1 point
5.Question 5
Which directory trees usually have frequently changing data, and would not be desirable on a partition that is more
static? Select all answers that apply.
/usr
/home
/var
Correct
/bin
/tmp
Correct
0 / 1 point