0% found this document useful (0 votes)
14 views66 pages

Schermafbeelding 2023-10-08 Om 16.21.07

Uploaded by

maan ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views66 pages

Schermafbeelding 2023-10-08 Om 16.21.07

Uploaded by

maan ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

LINUX - CHAPTER 2:

HANDLING FILES AND DIRECTORIES

28/09/2023 1
Files and Path Names

28/09/2023 2
Files
Linux file names:

In Linux file names, you are essentially allowed to use almost any
character that your computer can display.

Some exceptions:

- File names may not contain a forward slash ( / )


- Spaces are permitted, although they are best avoided
- Files names may never start with a hyphen ( - )

28/09/2023 3
Files
!! Linux distinguishes uppercase and lowercase letters in file names.
Unlike Windows, where uppercase and lowercase letters in file
names are displayed but treated the same.

Example:
Linux considers x-files and X-Files as
two different file names.

28/09/2023 4
Directories
Linux supports the idea of hierarchical “directories” which are used to group
files

File names do not need to be unique within the whole system, but only within
the same directory

The slash (“/”) serves to separate file names from directory names and directory
names from one another

Example:
joe/letter.txt would be the file letter.txt in the directory joe

28/09/2023 5
Directories
The Linux directory structure has a top level, called the root directory and it is
symbolized by the / character.
A visual representation of a typical Linux directory structure:

28/09/2023 6
Directories
To view the root filesystem, type ls /

Or ls –l /

28/09/2023 7
Home Directory
On most Linux distributions there is a directory called home under the root directory: /home
Under this /home directory there will be a directory for each user /home/yourname
Your home directory even has a special symbol that you can use to represent it: ~
Change to the user's home directory:

Your current directory is the directory where you are currently working in -> pwd command

28/09/2023 8
Absolute and Relative Path names
Absolute path:
→ When you give a pathname that starts from the root directory, it is called
an absolute path.

For example, if you are in your home directory and you want to go to
the /etc/perl/Net directory:

=> A name that starts with the root directory / is called an “absolute path
name”

28/09/2023 9
Absolute and Relative Path names
Relative path:
Names starting from the current directory.
A relative path provides directions using your current location as a point of
reference

Example: if you are in the /etc/perl/Net directory and then you wanted to go to
the /usr/share/doc directory.
Using an absolute pathname, use cd /usr/share/doc
Using relative pathnames, use cd ../../../usr/share/doc command:

28/09/2023 10
Absolute and Relative Path names
Summary:
A path name starting with a “/” is absolute; all others are relative

28/09/2023 11
Absolute and Relative Path names
Two shortcuts in relative path names (and even absolute ones):
- “..” refers to the directory above the directory in question in the directory
tree
- “.” within a directory always stands for the directory itself.

28/09/2023 12
Absolute and Relative Path names
Example:
• /home/joe
“..” refers to /home

• Executing in the current dir (letters):
Current directory
cat ../novels/ivanhoe.txt
→ will show the content of the
textfile ivanhoe.txt

28/09/2023 13
Absolute and Relative Path names
Example:
• “.” within a directory stands for the directory itself

Why would anyone need a method to refer to a directory when this directory has already have been reached?

The shell searches program files for external commands in the directories listed in the environment variable PATH.
If you, as a software developer, want to invoke a program, for example “prog”, which
(a) resides in a file within the current directory
and
(b) this directory is not listed in PATH (for security reasons)
you can still get the shell to start your file as a program by saying

without having to enter an absolute path name

28/09/2023 14
DIRECTORY COMMANDS

28/09/2023 15
The current directory: cd & co
cd command
→ for changing the current directory
→ “Change directory”
→ simply give the desired directory as a parameter:

28/09/2023 16
The current directory: cd
cd command
→ if you don’t give a parameter, you will end up in your homedirectory:

28/09/2023 17
The current directory: cd & co
cd command
→ “cd -”: changes to the directory that used to be current before the most
recent cd command
=> to alternate between two directories

28/09/2023 18
The current directory: cd & co
pwd command
→ gives the absolute path of the current directory
→ “print working directory”

Remark:
The current directory is (possible) also visible in your prompt (depending on your system settings)

Example:~/test$

Where ~/test stands for /home/dverbeek/test


→ the tilde (“~”) stands for the current user’s home directory

28/09/2023 19
Listing files and directories – ls
ls command
→ to find out which files and directories are located within a directory
→ “list” command

→ Without options: output = a multi-column table sorted by file name


Names of files of different types => in different colours.
Most distributions have agreed the colours to use:

28/09/2023 20
Listing files and directories – ls
ls command – options:
→ -a (“all”): to display hidden files (whose names begin with a dot)
→ -l (“long”): displays not only the file names, but also some additional
information about each file

28/09/2023 21
Listing files and directories – ls
ls –l
→ Output:
The first character gives the file type:
- plain files “-”
- executable files “-”
- directories “d”
- links “l”
Next nine characters = access permissions
Number of hard links (see later)
Owner of the file (sysadmin here)
Groupsowner (sysadmin)
Size of the file in bytes
Date and time of the last modification of the file’s content
At the end, the name of the file

28/09/2023 22
Listing files and directories – ls
Other options of ls:

28/09/2023 23
Listing files and directories – ls
If you want to see the extra information of a directory (such as /tmp):

→ NOT “ls –l /tmp”: because this will list the data for all the files within /tmp

→ use the -d option to suppress this and obtain the information about/tmp itse lf

28/09/2023 24
Listing files and directories – ls
Display all of the files in a directory as well as all of the files in all subdirectories under a directory.
This is called a recursive listing.

tmp
errorreport
test
test1
test2

28/09/2023 25
creating and deleting directories –
mkdir and rmdir
mkdir command: to create new directories
→ “make directory”
You can give one or more directory names as argument

→To create nested directories in a single step


=> option –p
otherwise, the command assumes that all directories in the path name except the
last one already exist

Example:

28/09/2023 26
creating and deleting directories –
mkdir and rmdir
rmdir command: to remove a directory
→ “remove directory”
→ At least one path name of a directory to be deleted must be given
→ The directories that will be deleted MUST be empty (may not contain entries
for files, subdirectories, etc.)

→ Only the last directory in every name will be removed:

28/09/2023 27
creating and deleting directories –
mkdir and rmdir
rmdir command:
→ option –p
→ all empty subdirectories mentioned in a name can be removed in one step,
beginning with the one on the very right

28/09/2023 28
FILE COMMANDS

28/09/2023 29
CREATING files – NANO or TOUCH
The touch command

Used to create a file without any content


Can be used when the user doesn’t have data to store at the time of file creation

28/09/2023 30
Reading files - CAT
The cat command
to join several files that are given in the command line
Just a single file name
→ the content of that file will be written to standard output

Options:

28/09/2023 31
Reading files - CAT
The cat command

Examples:

Files vb and file1 are


joined:

28/09/2023 32
Reading files - CAT
The cat command

Examples:
option –b:
numbers non-empty lines

option –E:
shows end of line

28/09/2023 33
Reading files - CAT
The cat command

Examples:
option –b: numbers non
empty lines

option –n: numbers all


lines

28/09/2023 34
Reading files - CAT
The cat command

Examples:

option –s: suppresses


repeated empty lines

28/09/2023 35
Reading files - CAT
The cat command

Examples:
option –t: shows tabs

28/09/2023 36
Reading files – HEAD & TAIL
The head (& tail) command

Gives by default the first ten (last ten) lines of every file passed as an argument

The -n option lets you specify a different number of lines:

Example:
“head -n 20 data.txt” or “head -20 data.txt”
→ returns the first 20 lines of the file data.txt

“tail -n 5 data.txt” or “tail -20 data.txt”


→ returns the last 5 lines of file data.txt

28/09/2023 37
Reading files – HEAD & TAIL
The head (& tail) command

28/09/2023 38
Reading files – HEAD & TAIL
The head (& tail) command

Example:

10 lines
default

28/09/2023 39
Reading files – HEAD & TAIL
The head (& tail) command

Example:

28/09/2023 40
FILE SEARCH PATTERNS

28/09/2023 41
The command echo
The “echo” command
→used to display line of text/string that are passed as an argument
→= a built in command that is mostly used in shell scripts and batch files to
output status text to the screen or a file
Syntax:
echo [option] [string]

28/09/2023 42
The command echo
The “echo” command

→ echo in combination with search patterns (see next slides):


• It will search the current directory for matches with the given search pattern.
These matches will be displayed on the screen.
• When no match is found, it literally prints the search pattern.

28/09/2023 43
simple search patterns - * ?
If you specify a parameter containing an asterisk, like:

prog1/p*.c

=> the shell replaces this parameter by a sorted list of all file names that “match”
the parameter

“Match” means that in the actual file name there may be an arbitrary-length
sequence of arbitrary characters in place of the asterisk

28/09/2023 44
simple search patterns - * ?
prog1/p*.c

matches with for example:

The only character the asterisk will not match is the slash.

28/09/2023 45
simple search patterns - * ?
Example:

28/09/2023 46
simple search patterns - * ?
Example:

$ echo test/d*
→ This command will output
the matching file names or directories

The search pattern “*” describes


“all files in the current directory”—excepting
hidden files whose name starts with a dot

Search patterns ignore hidden files unless you explicitly ask for them to be
included, namely “.*”
28/09/2023 47
simple search patterns - * ?
The search pattern “?” stands for exactly one arbitrary character (again
excluding the slash)

Example:

A pattern like

p?.c

matches the names


(and others)

28/09/2023 48
Character classes []
prog[123].c

→ the square brackets match those characters that are enumerated within
them (no others)

The pattern in the example matches with:

But not with:

28/09/2023 49
Character classes []
Example:

28/09/2023 50
Character classes []
You may specify ranges as in

prog[1-9].c

[A-Z]bracadabra.txt

=> The square brackets in the first line match all digits, the ones in the second all
uppercase letters

28/09/2023 51
Character classes []
REMARK:
In the character encodings the letters are not contiguous:
A pattern like
prog[A-z].c

not only matches progQ.c and progx.c,


but also prog_.c.
→Check an ASCII table, using “man ascii”
→If you want to match “uppercase and lowercase letters only”, you need to use
prog[A-Za-z].c

28/09/2023 52
File search patterns – Character classes []
You can specify negated character classes
→ interpreted as “all characters except these”

Something like prog[!A-Za-z].c


→ matches all names where the character between “prog” and “.” is not a letter

28/09/2023 53
HANDLING FILES
(copying, moving, deleting, ...)

28/09/2023 54
Handling files – Copying, moving and
deleting
cp command:
You can copy files using the cp (“copy”) command.
Two basic approaches:
• 1 ∶ 1 copy
• list of source files

• 1:1 copy:
cp <source filename> <target file name>

→ 1 ∶ 1 copy of the content of the source file will be placed in the target file
→ Normally cp doesn’t ask if it should overwrite the target file
when it already exists, it just does it. (caution!!)
The option -i ask if existing target file should be overwritten.28/09/2023 55
Handling files – Copying, moving and
deleting
cp command:
• 1:1 copy:
Possible to give a target directory (example .) name instead of a target file name
→ The source file will then be copied to that directory, keeping its old name, with
permissions of the target directory

Example:

We first created an exact copy of file list under the name list2
Then we copied the /etc/passwd file to the current directory (represented by the
dot as a target directory name)
28/09/2023 56
Handling files – Copying, moving and
deleting
cp command:
• list of source files:

Instead of a single source file


→ longer list of source files (or a wildcard pattern)
→ With this approach, it is not possible to copy a file to a different name, but
only to a different directory.

So the Linux command “cp *.txt *.bak” fails with an error message

28/09/2023 57
Handling files – Copying, moving and
deleting
cp command:

Options:
o-b (--backup): makes a backup of each existing destination file
o-i (--interactive): prompts before overwriting
o-R (--Recursive): copies directories with their content
o-u (--update): copies only if the source file is newer than the target file
o-v (--verbose): explains what is being done

activity

28/09/2023 58
Handling files – Copying, moving and
deleting
mv command:
The cp command makes an exact copy of a file.
It physically duplicates the file on the storage medium or creating a new, identical
copy on a different storage medium.
The mv (“move”) command moves a file to a different place or change its
name

Syntax of mv:

mv <one file or more to move> <where? directory name>

mv lets you rename directories as well as files

28/09/2023 59
Handling files – Copying, moving and
deleting
mv command:
Options of mv:

o-b (--backup): makes a backup of each existing destination file


o -i (--interactive): prompts before overwriting
o-v (--verbose): explains what is being done

28/09/2023 60
Handling files – Copying, moving and
deleting
mv command:
Example:

The original file passwd is replaced by the renamed file list2


Like cp, mv does not ask for confirmation if the target file name exists, but
overwrites the file

28/09/2023 61
Handling files – Copying, moving and
deleting
rm command:

To delete a file, you must have write permission in the corresponding directory

For example: You are “lord of the manor” in your own home directory
So you can remove even files that do not properly belong to you

Remark: Write permission on a file is completely irrelevant with deleting a file.

28/09/2023 62
Handling files – Copying, moving and
deleting
rm command:

Similar to cp or mv:
The files are deleted from the file system without confirmation
You should be especially careful when shell wildcard patterns are used

The “rm *” command deletes all non-hidden files from the current directory

28/09/2023 63
Handling files – Copying, moving and
deleting
Removing directories and subdirectories
with “rm -r *” subdirectories also removed even with content !

28/09/2023 64
Handling files – Copying, moving and
deleting
rm command:

“rm -i” is more carefully:


for each file, rm asks whether it should be removed (“y” for “yes”) or not (“n” for
“no”)

28/09/2023 65
Handling files – Copying, moving and
deleting
rm command:

Other options of rm:


o -v (--verbose): displays all activity on the screen

28/09/2023 66

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy