Lab Tutorial - Navigating the Linux File System
Navigating the Linux File System
The main concept that unites all versions of UNIX is the following four basics:
● Shell: The shell is the utility that processes your requests. When you type in a command
at your terminal, the shell interprets the command and calls the program that you want.
The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are
most famous shells which are available with most of the Unix variants.
● Commands and Utilities: There are various commands and utilities which you would use
in your day to day activities. cp, mv, cat and grep etc. are few examples of commands
and utilities. There are over 250 standard commands plus numerous others provided
through 3rd party software. All the commands come along with various optional
options.
● Files and Directories: All data in UNIX is organized into files. All files are organized into
directories. These directories are organized into a tree-like structure called the
filesystem.
Unix - File Management
All data in UNIX is organized into files. All files are organized into directories. These directories
are organized into a tree-like structure called the filesystem. When you work with UNIX, one
way or another you spend most of your time working with files. This tutorial would teach you
how to create and remove files, copy and rename them, create links to them etc.
In UNIX there are three basic types of files:
1. Ordinary Files: An ordinary file is a file on the system that contains data, text, or
program instructions. In this tutorial, you look at working with ordinary files.
2. Directories: Directories store both special and ordinary files. For users familiar with
Windows or Mac OS, UNIX directories are equivalent to folders.
3. Special Files: Some special files provide access to hardware such as hard drives, CD-ROM
drives, modems, and Ethernet adapters. Other special files are similar to aliases or
shortcuts and enable you to access a single file using different names.
Listing Files:
To list the files and directories stored in the current directory. Use the following command:
$ ls
$ ls -l
Meta Characters:
Meta characters have special meaning in Unix. For example * and ? are metacharacters. We use
* to match 0 or more characters, a question mark ? matches with single character.
For Example:
$ ls ch*.doc
$ls *.doc
Hidden Files:
An invisible file is one whose first character is the dot or period character (.). UNIX programs
(including the shell) use most of these files to store configuration information
To list invisible files, specify the -a option to ls:
$ ls -a
Display Content of a File:
You can use cat commands to see the content of a file. Following is the simple example to see
the content of above created file:
$ cat filename
You can display line numbers by using -b option along with cat command as follows:
$ cat filename -b
The easiest way to create a file with a short amount of text is by using the echo command.
Specify the required text, then use the > symbol to redirect the output to the new file. The
format of the command is: echo "line of text" > newfile_name.
$ echo "Placeholder for final text" > tmp_file.txt
Less command?
More command?
The touch Command
An easy way to create a file is with the touch command. This method creates a new empty file
in the current directory.
To create a new file with no contents, use the touch command followed by the name of the file.
$ touch [Link]
Alternatively, the redirect, or > symbol can accomplish the same task.
$ [Link]
The touch command can create multiple files at the same time.
$ touch [Link] [Link]
Counting Words in a File:
You can use the wc command to get a count of the total number of lines, words, and characters
contained in a file. Following is the simple example to see the information about above created
file:
$ wc filename
You can give multiple files at a time to get the information about those files. Here is simple
Syntax:
$ wc filename1 filename2 filename3
Copying Files:
To make a copy of a file use the cp command. The basic syntax of the command is:
$ cp source_file destination_file
Following is the example to create a copy of an existing file filename.
$ cp filename copyfile
Now you would find one more file copyfile in your current directory. This file would be exactly
same as original file filename.
Renaming Files:
To change the name of a file use the mv command. Its basic syntax is:
$ mv old_file new_file
Following is the example which would rename existing file filename to newfile:
$ mv filename newfile
The mv command would move an existing file completely into a new file. So in this case you
would fine only newfile in your current directory.
Deleting Files:
To delete an existing file use the rm command. Its basic syntax is:
$ rm filename
Caution: It may be dangerous to delete a file because it may contain useful information. So be
careful while using this command. It is recommended to use the -i option along with the rm
command. Following is the example which would completely remove existing file filename:
$ rm -i filename
You can remove multiple files at a tile as follows:
$ rm filename1 filename2 filename3
Search Files:
Find is able to recursively search any given path for various files. Using the file command we
can search for files by name, owner, group, permissions, type, size, time modified, date and
various other criteria. The find command is highly efficient but this efficiency comes at a cost of
time.
. represents current directory
/ represents root directory
Searching a file by its name
$ find -name findme
Searching a text file only
$ find / -name "*.txt"
Searching for a file ignoring the case.
$ find -iname fIndMe
Files modified more than 10 days.
$find -mtime 10
Files modified less than 10 days.
$ find -mtime -10
Files with size more than 10MB.
$ find / -size 10M
Search for empty files and directories
$ find / -empty
Unix - Directory Management
A directory is a file whose sole job is to store file names and related information. All files,
whether ordinary, special, or directory, are contained in directories.
UNIX uses a hierarchical structure for organizing files and directories. This structure is often
referred to as a directory tree . The tree has a single root node, the slash character ( /), and all
other directories are contained below it.
Home Directory:
The directory in which you find yourself when you first login is called your home directory.
You will be doing much of your work in your home directory and subdirectories that you'll be
creating to organize your files.
You can go in your home directory anytime using the following command:
$ cd ~
Here ~ indicates home directory. If you want to go in any other user's home directory then use
the following command:
$ cd ~username
To go in your last directory you can use following command:
$ cd -
Absolute/Relative Pathnames:
Directories are arranged in a hierarchy with root (/) at the top. The position of any file within
the hierarchy is described by its pathname. Elements of a pathname are separated by a /. A
pathname is absolute if it is described in relation to root, so absolute pathnames always begin
with a /. These are some examples of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
A pathname can also be relative to your current working directory. Relative pathnames never
begin with /. Relative to user amrood' home directory, some pathnames might look like this:
chem/notes
personal/res
To determine where you are within the filesystem hierarchy at any time, enter the command
pwd to print the current working directory:
$pwd
Listing Directories:
To list the files in a directory you can use the following syntax:
$ ls dirname
Following is the example to list all the files contained in /usr/local directory:
$ ls /usr/local
Creating Directories:
Directories are created by the following command:
$ mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to create. For
example, the command:
$ mkdir mydir
Creates the directory mydir in the current directory. Here is another example:
$ mkdir /tmp/test-dir
This command creates the directory test-dir in the /tmp directory. The mkdir command
produces no output if it successfully creates the requested directory.
If you give more than one directory on the command line, mkdir creates each of the directories.
For example:
$mkdir docs pub
Creates the directories docs and pub under the current directory.
Creating Parent Directories:
Sometimes when you want to create a directory, its parent directory or directories might not
exist. In this case, mkdir issues an error message as follows:
$ mkdir /tmp/amrood/test
In such cases, you can specify the -p option to the mkdir command. It creates all the necessary
directories for you. For example:
$ mkdir -p /tmp/amrood/test
Above command creates all the required parent directories.
Removing Directories:
Directories can be deleted using the rmdir command as follows:
$ rmdir dirname
Note: To remove a directory make sure it is empty which means there should not be any file or
sub-directory inside this directory.
You can create multiple directories at a time as follows:
$ rmdir dirname1 dirname2 dirname3
Above command removes the directories dirname1, dirname2, and dirname2 if they are empty.
The rmdir command produces no output if it is successful.
Changing Directories:
You can use the cd command to do more than change to a home directory: You can use it to
change to any directory by specifying a valid absolute or relative path. The syntax is as follows:
$ cd dirname
Here, dirname is the name of the directory that you want to change to. For example, the
command:
$cd /usr/local/bin
Renaming Directories:
The mv (move) command can also be used to rename a directory. The syntax is as follows:
$mv olddir newdir
You can rename a directory mydir to yourdir as follows:
$mv mydir yourdir
Check Calendar
For example to check calendar you need to type cal command as follows:
$ cal
Change Password
All Unix systems require passwords to help ensure that your files and data remain your own and
that the system itself is secure from hackers and crackers. Here are the steps to change your
password:
$ passwd
Listing Directories and Files
All data in UNIX is organized into files. All files are organized into directories. These directories
are organized into a tree-like structure called the filesystem. You can use ls command to list out
all the files or directories available in a directory.
Following is the example of using ls command with -l option.
$ ls -l
Who Are You?
While you're logged in to the system, you might be willing to know : Who am I? The easiest way
to find out "who you are" is to enter the whoami command:
$ whoami
Who is Logged In?
Sometimes you might be interested to know who is logged in to the computer at the same
time.
There are three commands available to get you this information, based on how much you'd like
to learn about the other users: users, who, and w.
$ users
$ who
$w
Try the w command on your system to check the output. This would list down a few more
information associated with the users logged in the system.
Logging Out:
When you finish your session, you need to log out of the system to ensure that nobody else
accesses your files while masquerading as you.
To log out:
Just type logout command at command prompt, and the system will clean up everything and
break the connection
System Shutdown:
The most consistent way to shutdown a Unix system properly via the command line is to use
one of the following commands:
Command Description
halt Brings the system down immediately
init 0 Powers off the system using predefined scripts to synchronize and
clean up the system prior to shutdown
init 6 Reboots the system by shutting it down completely and then
bringing it completely back up
poweroff Shuts down the system by powering off
reboot Reboots the system
shutdown Shuts down the system
You typically need to be the superuser or root (the most privileged account on a Unix system)
to shut down the system, but on some standalone or personally owned Unix boxes, an
administrative user and sometimes regular users can do so.