Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
165 views

Shell Navigation

The document introduces basic Linux commands for navigating directories and viewing files. It discusses the pwd command for printing the working directory, the cd command for changing directories, and the ls command for listing files. It explains Linux's hierarchical directory structure and provides examples of using absolute and relative paths with cd. It also summarizes the ls command's options for listing files in different formats.

Uploaded by

Isaiah Ochieng
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views

Shell Navigation

The document introduces basic Linux commands for navigating directories and viewing files. It discusses the pwd command for printing the working directory, the cd command for changing directories, and the ls command for listing files. It explains Linux's hierarchical directory structure and provides examples of using absolute and relative paths with cd. It also summarizes the ls command's options for listing files in different formats.

Uploaded by

Isaiah Ochieng
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Navigation

In this lesson, we will introduce our first three


commands: pwd (print working directory), cd (change directory),
and ls (list files and directories).

Those new to the command line will need to pay close attention
to this lesson since the concepts will take some getting used to.

File System Organization


Like Windows, the files on a Linux system are arranged in what is
called a hierarchical directory structure. This means that they are
organized in a tree-like pattern of directories (called folders in
other systems), which may contain files and subdirectories. The
first directory in the file system is called the root directory. The
root directory contains files and subdirectories, which contain
more files and subdirectories and so on and so on.

Most graphical environments include a file manager program used


to view and manipulate the contents of the file system. Often we
will see the file system represented like

this: 
One important difference between Windows and Unix-like
operating systems such as Linux is that Linux does not employ
the concept of drive letters. While Windows drive letters split the
file system into a series of different trees (one for each device),
Linux always has a single tree. Different storage devices may be
different branches of the tree, but there is always just a single
tree.

pwd
Since the command line interface cannot provide graphic pictures
of the file system structure, we must have a different way of
representing it. To do this, think of the file system tree as a
maze, and that we are standing in it. At any given moment, we
are located in a single directory. Inside that directory, we can see
its files and the pathway to its parent directory and the pathways
to the subdirectories of the directory in which we are standing.

The directory we are standing in is called the working directory.


To see the name of the working directory, we use
the pwd command.

[me@linuxbox me]$ pwd


/home/me

When we first log on to our Linux system, the working directory is


set to our home directory. This is where we put our files. On most
systems, the home directory will be called /home/user_name, but
it can be anything according to the whims of the system
administrator.

To list the files in the working directory, we use the ls command.

[me@linuxbox me]$ ls
Desktop Downloads foo.txt Pictures
Templates
Documents examples.desktop Music Public Videos
We will come back to ls in the next lesson. There are a lot of fun
things you can do with it, but we have to talk about pathnames
and directories a bit first.

cd
To change the working directory (where we are standing in the
maze) we use the cd command. To do this, we type cd followed
by the pathname of the desired working directory. A pathname is
the route we take along the branches of the tree to get to the
directory we want. Pathnames can be specified two different
ways; absolute pathnames or relative pathnames. Let's look with
absolute pathnames first.

An absolute pathname begins with the root directory and follows


the tree branch by branch until the path to the desired directory
or file is completed. For example, there is a directory on your
system in which most programs are installed. The pathname of
the directory is /usr/bin. This means from the root directory
(represented by the leading slash in the pathname) there is a
directory called "usr" which contains a directory called "bin".

Let's try this out:

me@linuxbox me]$ cd /usr/bin


me@linuxbox bin]$ pwd
/usr/bin
me@linuxbox bin]$ ls
'[' mshortname
2to3-2.7 mshowfat
411toppm mtools
a2ps mtoolstest
a2ps-lpr-wrapper mtr
aa-enabled mtrace
aa-exec mtr-packet
aclocal mtvtoppm
aclocal-1.15 mtype
aconnect mutter
acpi_listen mxtar
add-apt-repository mzip
addpart namei

and many more...

Now we can see that we have changed the current working


directory to /usr/bin and that it is full of files. Notice how the
shell prompt has changed? As a convenience, it is usually set up
to display the name of the working directory.

Where an absolute pathname starts from the root directory and


leads to its destination, a relative pathname starts from the
working directory. To do this, it uses a couple of special notations
to represent relative positions in the file system tree. These
special notations are "." (dot) and ".." (dot dot).

The "." notation refers to the working directory itself and the ".."
notation refers to the working directory's parent directory. Here is
how it works. Let's change the working directory to /usr/bin
again:

me@linuxbox me]$ cd /usr/bin


me@linuxbox bin]$ pwd
/usr/bin

O.K., now let's say that we wanted to change the working


directory to the parent of /usr/bin which is /usr. We could do
that two different ways. First, with an absolute pathname:

me@linuxbox bin]$ cd /usr


me@linuxbox usr]$ pwd
/usr

Or, with a relative pathname:

me@linuxbox bin]$ cd ..
me@linuxbox usr]$ pwd
/usr
Two different methods with identical results. Which one should
we use? The one that requires the least typing!

Likewise, we can change the working directory


from /usr to /usr/bin in two different ways. First using an
absolute pathname:

me@linuxbox usr]$ cd /usr/bin


me@linuxbox bin]$ pwd
/usr/bin

Or, with a relative pathname:

me@linuxbox usr]$ cd ./bin


me@linuxbox bin]$ pwd
/usr/bin

Now, there is something important that we must point out here.


In most cases, we can omit the "./". It is implied. Typing:

me@linuxbox usr]$ cd bin

would do the same thing. In general, if we do not specify a


pathname to something, the working directory will be assumed.
There is one important exception to this, but we won't get to that
for a while.

A Few Shortcuts
If we type cd followed by nothing, cd will change the working
directory to our home directory.

A related shortcut is to type cd ~user_name. In this case, cd will


change the working directory to the home directory of the
specified user.

Typing cd - changes the working directory to the previous one.

Important facts about file names


1. File names that begin with a period character are hidden.
This only means that ls will not list them unless we say ls
-a. When your account was created, several hidden files
were placed in your home directory to configure things for
your account. Later on we will take a closer look at some of
these files to see how you can customize our environment.
In addition, some applications will place their configuration
and settings files in your home directory as hidden files.
2. File names in Linux, like Unix, are case sensitive. The file
names "File1" and "file1" refer to different files.
3. Linux has no concept of a "file extension" like Windows
systems. You may name files any way you like. However,
while Linux itself does not care about file extensions, many
application programs do.
4. Though Linux supports long file names which may contain
embedded spaces and punctuation characters, limit the
punctuation characters to period, dash, and
underscore. Most importantly, do not embed spaces in
file names. If you want to represent spaces between words
in a file name, use underscore characters. You will thank
yourself later.

Looking Around
Now that we know how to move from working directory to
working directory, we're going to take a tour of our Linux system
and, along the way, learn some things about what makes it tick.
But before we begin, we have to learn about some tools that will
come in handy during our journey. These are:

 ls (list files and directories)


 less (view text files)
 file (classify a file's contents)

ls
The ls command is used to list the contents of a directory. It is
probably the most commonly used Linux command. It can be
used in a number of different ways. Here are some examples:

Examples of the ls command

Command Result

ls List the files in the working directory

ls /bin List the files in the /bin directory (or any other directory we care to
specify)

ls -l List the files in the working directory in long format

ls -l List the files in the /bin directory and the /etc directory in long


/etc /bin format

ls -la .. List all files (even ones with names beginning with a period character,
which are normally hidden) in the parent of the working directory in long
format

These examples also point out an important concept about


commands. Most commands operate like this:

command -options arguments

where command is the name of the command, -options is one or


more adjustments to the command's behavior, and arguments is
one or more "things" upon which the command operates.

In the case of ls, we see that ls is the name of the command,


and that it can have one or more options, such as -a and -l, and
it can operate on one or more files or directories.

A Closer Look at Long Format


If we use the -l option with ls, you will get a file listing that
contains a wealth of information about the files being listed.
Here's an example:

-rw------- 1 me me 576 Apr 17 2019


weather.txt
drwxr-xr-x 6 me me 1024 Oct 9 2019
web_page
-rw-rw-r-- 1 me me 276480 Feb 11 20:41
web_site.tar
-rw------- 1 me me 5743 Dec 16 2018
xmas_file.txt

---------- ------- ------- -------- ------------


-------------
| | | | |
|
| | | | |
File Name
| | | | |
| | | | +---
Modification Time
| | | |
| | | +-------------
Size (in bytes)
| | |
| | +-----------------------
Group
| |
| +--------------------------------
Owner
|
+----------------------------------------------
File Permissions

File Name
The name of the file or directory.
Modification Time
The last time the file was modified. If the last modification
occurred more than six months in the past, the date and
year are displayed. Otherwise, the time of day is shown.
Size
The size of the file in bytes.
Group
The name of the group that has file permissions in addition
to the file's owner.
Owner
The name of the user who owns the file.
File Permissions
A representation of the file's access permissions. The first
character is the type of file. A "-" indicates a regular
(ordinary) file. A "d" indicates a directory. The second set of
three characters represent the read, write, and execution
rights of the file's owner. The next three represent the rights
of the file's group, and the final three represent the rights
granted to everybody else. We'll discuss this in more detail
in a later lesson.

less
less is a program that lets us view text files. This is very handy
since many of the files used to control and configure Linux are
human readable.

What is "text"?
There are many ways to represent information on a computer. All
methods involve defining a relationship between the information
and some numbers that will be used to represent it. Computers,
after all, only understand numbers and all data is converted to
numeric representation.

Some of these representation systems are very complex (such as


compressed multimedia files), while others are rather simple. One
of the earliest and simplest is called ASCII
text. ASCII (pronounced "As-Key") is short for American Standard
Code for Information Interchange. This is a simple encoding
scheme that was first used on Teletype machines to map
keyboard characters to numbers.

Text is a simple one-to-one mapping of characters to numbers. It


is very compact. Fifty characters of text translates to fifty bytes
of data. Throughout a Linux system, many files are stored in text
format and there are many Linux tools that work with text files.
Even Windows systems recognize the importance of this format.
The well-known NOTEPAD.EXE program is an editor for plain
ASCII text files.

The less program is invoked by simply typing:

less text_file

This will display the file.

Controlling less
Once started, less will display the text file one page at a time.
We can use the Page Up and Page Down keys to move through
the text file. To exit less, we type "q". Here are some commands
that less will accept:

Keyboard commands for the less program

Command Action

Page Up or b Scroll back one page

Page Down or Scroll forward one page


space

G Go to the end of the text file

1G Go to the beginning of the text file


/characters Search forward in the text file for an occurrence of the
specified characters

n Repeat the previous search

h Display a complete list less commands and options

q Quit

file
As we wander around our Linux system, it is helpful to determine
what kind of data a file contains before we try to view it. This is
where the file command comes in. file will examine a file and
tell us what kind of file it is.

To use the file program, we just type:

file name_of_file

The file program can recognize most types of files, such as:

Various kinds of files

File Type Description Viewable as text?

ASCII text The name says it all yes

Bourne-Again shell A bash script yes


script text

ELF 64-bit LSB An executable binary program no


executable

ELF 64-bit LSB A shared library no


shared object

GNU tar archive A tape archive file. A common way of no, use tar tvf to
storing groups of files. view listing.
gzip compressed data An archive compressed with gzip no

HTML document text A web page yes

JPEG image data A compressed JPEG image no

PostScript document A PostScript file yes


text

Zip archive data An archive compressed with zip no

While it may seem that most files cannot be viewed as text, a


surprising number can be. This is especially true of the important
configuration files. During our adventure we will see that many
features of the operating system are controlled by text
configuration files and shell scripts. In Linux, there are no
secrets!

Manipulating Files
This lesson will introduce the following commands:

 cp - copy files and directories


 mv - move or rename files and directories
 rm - remove files and directories
 mkdir - create directories

These four commands are among the most frequently used Linux
commands. They are the basic commands for manipulating both
files and directories.

Now, to be frank, some of the tasks performed by these


commands are more easily done with a graphical file manager.
With a file manager, you can drag and drop a file from one
directory to another, cut and paste files, delete files, etc. So why
use these old command line programs?
The answer is power and flexibility. While it is easy to perform
simple file manipulations with a graphical file manager,
complicated tasks can be easier with the command line programs.
For example, how would you copy all the HTML files from one
directory to another, but only copy files that did not exist in the
destination directory or were newer than the versions in the
destination directory? Pretty hard with with a file manager. Pretty
easy with the command line:

[me@linuxbox me]$ cp -u *.html destination

Wildcards
Before we begin with our commands, we'll first look at a shell
feature that makes these commands so powerful. Since the shell
uses filenames so much, it provides special characters to help you
rapidly specify groups of filenames. These special characters are
called wildcards. Wildcards allow you to select filenames based on
patterns of characters. The table below lists the wildcards and
what they select:

Summary of wildcards and their meanings

Wildcard Meaning

* Matches any characters

? Matches any single character

[characters] Matches any character that is a member of the set characters. The set of
characters may also be expressed as a POSIX character class such as one of
the following:
POSIX Character Classes

[:alnum:] Alphanumeric characters

[:alpha:] Alphabetic characters

[:digit:] Numerals
[:upper:] Uppercase alphabetic characters

[:lower:] Lowercase alphabetic characters

[! Matches any character that is not a member of the set characters


characters]

Using wildcards, it is possible to construct very sophisticated


selection criteria for filenames. Here are some examples of
patterns and what they match:

Examples of wildcard matching

Pattern Matches

* All filenames

g* All filenames that begin with the


character "g"

b*.txt All filenames that begin with the


character "b" and end with the
characters ".txt"

Data??? Any filename that begins with the


characters "Data" followed by exactly 3
more characters

[abc]* Any filename that begins with "a" or


"b" or "c" followed by any other
characters

[[:upper:]]* Any filename that begins with an


uppercase letter. This is an example of a
character class.

BACKUP.[[:digit:]][[:digit:]] Another example of character classes.


This pattern matches any filename that
begins with the characters "BACKUP."
followed by exactly two numerals.
*[![:lower:]] Any filename that does not end with a
lowercase letter.

We can use wildcards with any command that accepts filename


arguments.

cp
The cp program copies files and directories. In its simplest form,
it copies a single file:

[me@linuxbox me]$ cp file1 file2

It can also be used to copy multiple files (and/or directories) to a


different directory:

[me@linuxbox me]$ cp file... directory

A note on notation: ... signifies that an item can be repeated


one or more times.

Other useful examples of cp and its options include:

Examples of the cp command

Command Results

cp file1 Copies the contents of file1 into file2. If file2 does not exist, it is


file2 created; otherwise, file2 is silently overwritten with the contents
of file1.

cp -i file1 Like above however, since the "-i" (interactive) option is specified,
file2 if file2 exists, the user is prompted before it is overwritten with the
contents of file1.

cp file1 Copy the contents of file1 (into a file named file1) inside of


dir1 directory dir1.

cp -R dir1 Copy the contents of the directory dir1. If directory dir2 does not exist,


it is created. Otherwise, it creates a directory named dir1 within
dir2 directory dir2.

mv
The mv command moves or renames files and directories
depending on how it is used. It will either move one or more files
to a different directory, or it will rename a file or directory. To
rename a file, it is used like this:

[me@linuxbox me]$ mv filename1 filename2

To move files (and/or directories) to a different directory:

[me@linuxbox me]$ mv file... directory

Examples of mv and its options include:

Examples of the mv command

Command Results

mv file1 If file2 does not exist, then file1 is renamed file2. If file2 exists, its


file2 contents are silently replaced with the contents of file1.

mv -i file1 Like above however, since the "-i" (interactive) option is specified,
file2 if file2 exists, the user is prompted before it is overwritten with the
contents of file1.

mv file1 The files file1 and file2 are moved to directory dir1. If dir1 does not


file2 dir1 exist, mv will exit with an error.

mv dir1 If dir2 does not exist, then dir1 is renamed dir2. If dir2 exists, the


dir2 directory dir1 is moved within directory dir2.

rm
The rm command removes (deletes) files and directories.
[me@linuxbox me]$ rm file...

Using the recursive option (-r), rm can also be used to delete


directories:

[me@linuxbox me]$ rm -r directory...

Examples of rm and its options include:

Examples of the rm command

Command Results

rm file1 Delete file1 and file2.
file2

rm -i file1 Like above however, since the "-i" (interactive) option is specified,
file2 the user is prompted before each file is deleted.

rm -r dir1 Directories dir1 and dir2 are deleted along with all of their contents.


dir2

Be careful with rm!


Linux does not have an undelete command. Once you delete
something with rm, it's gone. You can inflict terrific damage on
your system with rm if you are not careful, particularly with
wildcards.

Before you use  rm  with wildcards, try this helpful


trick: construct your command using ls instead. By doing this,
you can see the effect of your wildcards before you delete files.
After you have tested your command with ls, recall the
command with the up-arrow key and then substitute rm for ls in
the command.

mkdir
The mkdir command is used to create directories. To use it, you
simply type:

[me@linuxbox me]$ mkdir directory...

Using Commands with Wildcards


Since the commands we have covered here accept multiple file
and directories names as arguments, you can use wildcards to
specify them. Here are a few examples:

Command examples using wildcards

Command Results

cp *.txt Copy all files in the current working directory with names ending
text_files with the characters ".txt" to an existing directory named text_files.

mv dir1 Move the subdirectory dir1 and all the files ending in ".bak" in the
../*.bak current working directory's parent directory to an existing directory
dir2 named dir2.

rm *~ Delete all files in the current working directory that end with the
character "~". Some applications create backup files using this
naming scheme. Using this command will clean them out of a
directory.

Further Reading
 Chapter 4 of The Linux Command Line covers this topic in
more detail

You might also like