0% found this document useful (0 votes)
29 views

Chap4 - The Shell - File Management

The document discusses the Unix/Linux file system hierarchy and organization. It describes typical directory structures and file types. It also provides an overview of common commands for navigating, viewing, and managing files and directories.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Chap4 - The Shell - File Management

The document discusses the Unix/Linux file system hierarchy and organization. It describes typical directory structures and file types. It also provides an overview of common commands for navigating, viewing, and managing files and directories.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Operating System II

Chapter 4 - The Shell - File Management

Dr. Fuad Mire Hassan


March 2024
Unix File System
➢ The Unix/Linux file system is hierarchical, similar to other operating
systems today
○ Files are organized in directories
○ Directories may contain sub-directories
➢ What is different (from Windows anyway) is that there are no drive
letters (such as C:, or D:)
○ All files and directories appear under a single root, even if multiple
storage devices are used
➢ Learning command-line navigation of the file system is essential
for efficient system usage
Hierarchical File System
➢ In the Linux (Unix) OS, the "root directory" / is the starting directory, and
other "child directories", "grandchild directories”, etc. are created
➢ The hierarchical structure resembles an "upside-down tree“. There is
actually a command called tree that can display a “tree diagram”!

home public

user1 user2 user3 ipc144 nled


Typical Unix/Linux Directories
➢ / Root directory (ancestor to all directories).
➢ /home Used to store users’ home directories.
➢ /bin Common system binaries (commands).
➢ /usr/bin Common utilities (commands) for users.
➢ /usr/sbin Common utilities for user administration.
➢ /etc General System Admin. Files (eg passwd)
➢ /var Dynamic files (log files)
➢ /tmp, /var/tmp Temporary files for programs
➢ /dev Device files (terminals, printers, etc.)
Home directory
➢ Every user when receiving an account has a “home” directory
created
➢ This is where you keep your personal files
➢ ~ represents your home
➢ A cd command without any parameter will get you directly to your
home directory
➢ Remember to keep your files private
Types of Files

➢ On a Unix/Linux file system a “file” can be anything


○ To an average computer user a file is a text document, video,
music, photo etc.
➢ A directory is really an index file, containing references to file
locations on the physical disc and other related information
➢ Any file (or directory) name starting with a period is considered to be
a hidden file
Types of Files
➢ You can use the ls –l command to determine the type of file.

For Example:
ls -l /dev/tty
crw-rw-rw- 1 root root 5, 0 2003-03-14 08:07 /dev/tty

ls -l monday.txt w1.c
-rw-r--r-- 1 someuser users 214 2006-01-23 14:20 monday.txt
-rw-r--r-- 1 someuser users 248 2005-10-12 13:36 w1.c

ls –ld uli101
drwxr-xr-x 2 someuser users 4096 2006-01-17 16:43 uli101

You can determine file type from looking at first


character in detailed listing: Note: you can use the –d option with
- indicates a regular file the detailed listing command to get information
for just the directoryfile. eg. ls –ld /home/myacct
b or C indicates a device file
d indicates a directory file
Hidden Files
➢ A file is hidden if its name starts with a .
For example: .profile
➢ ls –a will show all files including hidden
➢ . and .. directories are hidden
○ ls –A will show “Almost” all files – not including . and ..

➢ Why make files hidden?


○ To clean up directories
○ To hide backups
○ To protect important files from accidental deletion
➢ Remember: directories are really files, you can hide them as well
Working With The File System
➢ Be very careful when working with files on the command line, as
there is no undo command or a Trash/Recycling Bin
○ A single command can wipe out your entire account
○ Changes are instant and permanent
➢ Make backups of important files, preferably outside of your account –
USB storage is a good option
➢ You may need additional ways to control file access through file
permissions which will help you prevent accidental file damage or
deletion
Basic Commands
pwd
○ Used to display the user’s present working directory. A user may need to where they are
located on the computer system in order to build directories, copy files, etc…

cd directorypath
○ Used to change to a directory. Entering the cd command without the directory path will
change to the user’s home directory.
Basic Commands
ls

○ Used to display the contents of a directory (eg. regular files or sub-directories). By


default, the ls command displays non-hidden filenames only.

○ The following are common options associated with the ls command:


○ -a short display of hidden & non-hidden files
○ -l detailed display of files (excluding hidden files)
○ -F displays / after directory, * after executable file
○ Options can be combined, for example: ls -la (or ls -l -a)
Basic Commands
mkdir directorypath
○ Used to create a subdirectory with a directory. Multiple arguments can be used to create
many subdirectories. The option –p allows for parent directories to be created.

rmdir directorypath
○ Used to remove only empty directories (i.e. directories that contain no subdirectories or
regular files). A user cannot remove a directory from within the directory location itself.
Basic Commands
mv sourcepath destinationpath
○ Used to move a file from one location to another and/or rename the file. The mv
command can be used to move directories as well as files. The –i option asks for
confirmation if the destination filename already exists.

cp sourcepath destinationpath
○ Used to copy a file from one location to another. The cp command can be used to backup
important files.
○ The –i option asks for confirmation if the destination filename already exists.
○ The -r option allows copying of directories and their contents
Basic Commands
rm filepath
○ Used to remove a regular file.

rm -r filepath
○ Used to recursively remove a directory and it's contents. Recursive means to descend to
lower levels, which in this case, indicates that subdirectories and it contents are also
removed.

Note: it is a good idea to include the –i option to confirm deletion of subdirectories and its
contents!
Basic Commands
cat filepath
➢ To join files (i.e. to concatenate files). For example, cat file1 file2 file3 will display the contents of file1 and file2
and file3 on the screen at the same time.

➢ To display the contents of small files (files longer than the screen will scroll to the end).
For example, issuing the command cat .bash_profile in your home directory would display the contents of your
setup file.

more filepath
➢ Used to display the contents of large regular files one screen at a time. The user can navigate
throughout the file by pressing keys such as:
➢ spacebar Move to next screen
b Move to previous screen
enter Move to next line
/car Search for pattern "car"
q Exit to shell

less filepath
➢ Works like more command, but contains more navigation features.
Basic Commands
touch path

○ Used to update the date and time of existing files.

○ The touch command is also used to create empty files.


You will be using the touch command to create empty files when you practice the file
management on-line tutorial

file path

○ Determines a file type


○ Useful when a particular file has no file extension or the extension is unknown/incorrect
The find Command
➢ The find command allows searching for files by file name, size as well as file
attributes recursively throughout the file system
○ An optional action can be performed on matches
➢ Examples:
○ Search for a file named bob:
■ find / -name bob
○ Delete empty files belonging to user alice:
■ find / -user alice -empty -delete
○ Find all files modified less than 5 minutes ago:
■ find / -mmin -5
○ Find large files:
■ find . -size +100M
File Naming
➢ Unix/Linux is case sensitive!
➢ Adopt a consistent file naming scheme – this will help you find your files
later
➢ Make your file and directory names meaningful
➢ Avoid non alphanumeric characters, as they have a special meaning to
the system and will make your work more difficult
➢ Avoid using spaces in file names – consider periods, hyphens and
underscores instead
➢ Feel free to use file name extensions to describe the file purpose
Special characters
➢ Some characters have special meaning to shell
➢ They include:
&;|*?‘“[]()$<>{}^#/\%!~
➢ Above characters:
○ Should not be used in file names
○ Sometimes are called meta-characters
Other Unix/Linux Commands
➢ whoami

○ Used to display the username of the account’s owner.

➢ finger username

○ Used to display additional information regarding the user (regardless whether or not the user is
currently logged onto the system).

➢ who

− Used to display a list of other users currently logged on to the same system is the user. Useful
options with the who command are –i and -T
Other Unix/Linux Commands
➢ passwd
○ Used to change an user's password on a standalone system. After issuing this
command, the system will prompt the user for their existing password – if that password
verifies, then the user is asked to enter and re-enter their new passwords.

○ While entering in the new password, feedback may be provided to reject unsuitable passwords
(see previous notes on how to create a valid password)

○ Please note that most of Seneca's systems rely on a centralized user directory, so the usage of
the passwd command here is limited
Other Unix/Linux Commands
➢ date

○ Used to display the current date and time of the server. The super-user (i.e. root-user) can use this
command to set the date and time. Refer to the online manual to format date display.

➢ cal

○ Used to display the current month’s calendar (by default). The user can also display the yearly
calendar (year as the first argument), or a specific month for a specific year (month number as the
first argument, and year as the second argument).
Getting Help with Commands
A comprehensive online manual for common UNIX/Linux commands exists on your server

The online manual is a command called man

Command Structure:

man [options] command

Options:
-k provides short (one-line) explanation relating
to the commands matching the character string. This
can be used if user doesn’t know name of command.
eg. man -k calendar
THANKS

You might also like