Schermafbeelding 2023-10-08 Om 16.21.07
Schermafbeelding 2023-10-08 Om 16.21.07
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:
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
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$
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
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
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.)
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
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:
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
28/09/2023 34
Reading files - CAT
The cat command
Examples:
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
Example:
“head -n 20 data.txt” or “head -20 data.txt”
→ returns the first 20 lines of the 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
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
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
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
28/09/2023 48
Character classes []
prog[123].c
→ the square brackets match those characters that are enumerated within
them (no others)
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
28/09/2023 52
File search patterns – Character classes []
You can specify negated character classes
→ interpreted as “all characters except these”
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:
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:
28/09/2023 59
Handling files – Copying, moving and
deleting
mv command:
Options of mv:
28/09/2023 60
Handling files – Copying, moving and
deleting
mv command:
Example:
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
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:
28/09/2023 65
Handling files – Copying, moving and
deleting
rm command:
28/09/2023 66