Linux Learning 2 - File System

Made by Mike_Zhang


Unfold Linux Topics | 展开Linux主题 >


1 Introduction

File system:

  • The collection of storage devices which, combined, make up the system’s file space.

Partition:

  • A disk partition is a logical division of the collection of storage devices into independent units.
  • In Linux, common partitions are for the user directory portion (/home), the operating system kernel and other system files (/, /bin, /sbin, /etc) and the swap space (virtual memory).

Directory:

  • A user-specified location that allows a user to organize files.

File:

  • The basic unit of storage in the logical file system, the file will either store data or an executable program.

Path:

  • A description of how to reach a particular file or directory.

inode:

  • A data structure stored in the file system used to represent a file.

Link:

  • A combination of a file’s name and the inode number that represents the file.

[Example of Linux hierarchy file system]

  • The top-level Linux directories, many of which contain subdirectories and subsubdirectories.
  • One user, foxr, is shown, and only two subdirectories of /usr are shown.

1.1 Linux File System Structure

Linux Top-Level Directory Structure


1.2 Path

Paths can be expressed in one of two ways: absolute and relative.

Absolute path:

  • Starts at the root level of the file system and works its way down the file system’s hierarchy, directory by directory.
  • The length of an absolute path is determined by the number of directories/subdirectories that exist between the root level and the target file or directory.
  • Each new directory listed in a path is denoted by a slash (/) followed by the directory name.
  • [Example]
    • /home/foxr/temp/temp2/foo.txt

Relative path

  • Starts at the current working directory.
  • The subdirectory is specified without a leading slash (/).
  • [Example]
    • If foxr’s current directory is temp,
    • then the file foo.txt is found through the relative path:
    • temp2/foo.txt.

To indicate a directory or file beneath the current directory:

  • Specify a downward motion.
  • This is done by listing subdirectories with slashes between the directories.

To indicate a directory above the current directory:

  • use .. as in ../foo.txt which means the file foo.txt is found one directory up.

2 Filenames with Wildcards

A wildcard is a way to express a number of items (files, directories, links) without having to enumerate all of them.

Wildcards in Linux

Wildcard Explanation
* Match anything (0 characters or more)
? Match any one character
[chars] Match any one of the characters in the list
[char1-char2] Match any one of the characters in the range from char1 to char 2 (e.g., [0–9], [a-e], [A-Z])
{word1,word2,word3} Match any one of the words
[!chars] Match any one character not in the list

[Examples Using ls with Wildcards]

ls Command Result
ls *.txt file1.txt file21.txt file2a.txt file2.txt file_a.txt file.txt
ls *.* file1.txt file21.txt file2a.txt file2.txt file_a.txt file.txt file.dat file1.dat file2.dat
ls * All files
ls file?.* file1.txt file2.txt file1.dat file2.dat
ls file??.txt file21.txt file2a.txt file_a.txt
ls file?.{dat,txt} file1.dat file1.txt file2.dat file2.txt
ls [abc][abc][abc] aba abc
ls [!a]* All files except for aba and abc

3 File System Commands

Common Linux File Commands

Command Description Common Options
pwd Display current directory
cd Change directory
ls List contents of directory view
mv Move or rename file(s)/directory(ies) -f (force), -i (interactive), -n (do not overwrite existing file)
cp Copy file(s) same as mv, also -r (recursive)
rm Delete file(s) -f (force), -i (interactive), -r (recursive)
mkdir Create a directory
rmdir Delete a directory
cat Concatenate files (display to window) -n (add line numbers), -T (show tabs)
less Display file screen-by-screen -c (clear screen first), -f (open nonregular files)
more Display file screen-by-screen -num # (specify screen size in # rows), +# (start at row number #)
file Describe the type of entity passed to the command

3.1 Move Command

Move and rename files/directories.

1
mv [options] source destination

Rename a file:

  • Source and destination are the old name and the new name, respectively.

Move a file:

  • Source and destination must be in different directories. If destination is a directory, then the file’s name is not changed.

[Example]

1
mv ~foxr/temp/foo2.txt .

Move foo2.txt to the user’s subdirectory temp without changing the name


1
mv foo1.txt ~/temp/foo2.txt

Move the file and rename it from foo1.txt to foo2.txt


1
mv ~foxr/temp/foo2.txt .

Move foxr’s file foo2.txt, which is in his subdirectory temp, to the current working directory.


3.2 Copy Command

1
cp [options] source destination

Option 1:

  • Destination specifier can be another directory
  • the file is copied into the new directory and the new file is given the same name as the original file.

Option 2:

  • Destination is both a directory and file name
  • the file is copied into the new directory and given a different name.

Option 3:

  • Destination is a filename
  • the file is copied into the current directory under the new filename.

Common Options for the cp Command

Option Description
-b Create a backup of every destination file
-f Force copy, as with mv’s -f
-i Prompt the user, as with mv’s -i
-I, -s Create hard/symbolic link rather than physical copy
-L Follow symbolic links
-p Preserve ownership, permissions, time stamp, etc.
-r Recursive copy (recursion is discussed below)
-u Copy only if source newer than destination or destination missing
-v Verbose (output each step as it happens)

[Example]

1
cp foo.txt ~zappaf/foo1.txt

Copy the file foo.txt from the current working directory to zappaf’s home directory, new file is foo1.txt.


1
cp *.txt ~

All files in the current directory that end with a .txt extension are copied to the user’s home directory.


1
cp foo1.txt .

Will not work because you cannot copy foo1.txt onto itself.


1
cp -r ~foxr/temp .

Copy directory temp2 with the files f1, f2, and f3, but also subdirectories a and b, and the files b1 and b2 from directory b


3.3 Deletion Command

Remove or delete

1
rm [options] file(s)

option -i:

  • This option causes rm to pause before each file is deleted and ask the user if he or she is sure about deleting that file.

option -f:

  • Override the prompting messages.
  • This can be dangerous though, so only use -f when you are sure that you want to delete the file(s) specified.

option -r:

  • the same as with cp, the files and subdirectories are deleted.

rm -fr *:

  • This will delete everything in the current directory, any subdirectories and their contents as well.

3.4 Directory Commands

  1. Create a directory
1
mkdir TEMP

The directory is created in the current working directory, unless a path is specified. The directory is initially empty.

  1. Delete a directory
1
rmdir TEMP

To remove a directory, it must be empty. Therefore, you would have to delete its contents first.

rm -r directory:

  • To delete the directory and its contents using the recursive deletion.
  • Delete the contents stored in directory first and then delete the directory itself.

3.5 Text File Viewing Commands

1
cat [options] file(s)

The cat command (short for concatenate) displays the contents of all of the files specified, without pausing. more commonly used with redirection.


You can also view the first part or last part of a file easily using the commands head and tail, respectively.

  • These two commands display the first and last 10 lines of a file, respectively.
  1. head: precede the integer with a minus sign to indicate that the program should skip some number of bytes or lines.
  2. tail: precede the integer with a plus sign to indicate the starting point within the file.

[Examples Using head and tail]

Command Result
head file.txt Output first 10 lines of file.txt
tail file.txt Output last 10 lines of file.txt
head –c 100 file.txt Output first 100 bytes of file.txt
head –n –3 file.txt Output first 11 lines of file.txt (all but the last three lines)
tail –n 5 file.txt Output last 5 lines of file.txt
tail –c +100 file.txt Output all characters of file.txt starting at the 100th byte

3.6 Sort Commands

1
sort [options] file(s)

Sort a given file line-by-line in increasing order

option -r:

  • causes sort to work in decreasing order.

option -f:

  • Causes sort to ignore case so that ‘a’ and ‘A’ are treated equally (otherwise ‘A’ comes before ‘a’).

4 Permissions

Permissions:

  • a mechanism to support operating system protection.
  • Protection ensures that users do not misuse system resources.

In Linux:

  • Permissions are established for all files and directories. Permissions specify who can access a file or directory, and the types of access.

Permissions are controlled at three levels:

  • Owner (called user, or ‘u’ for short)
  • Group (‘g’ for short)
  • The rest of the world (called other, or ‘o’ for short)

Permissions have eight different types of access:

  • Consist of all of the combinations of read, write, execute, and no access
  • read access as ‘r,’
  • write access as ‘w,’
  • execute access as ‘x.’

Each level of access provides:

  • Read—for a file, it can be viewed or copied
  • Write—for a file, it can be overwritten (e.g., using save as)
  • Execute—for a file, it can be executed (this is necessary for executable programs and shell scripts)
  • Read—for a directory, the directory’s contents can be viewed by ls
  • Write—for a directory, files can be written there
  • Execute—for a directory, a user can cd into it

To delete a file, you must have write access to the directory that contains it.


To change a file’s permission

1
chmod permissions file(s)

Type 1:

  • Specifying permissions is to describe the changes to be applied as a combination of u, g, o along with r, w, x.
    • To add a permission, use +
    • To remove a permission, use –.
    • To change a permission on all three of u, g, and o, you can use ‘a’ instead for all.
1
chmod g–w,o–r file1.txt

Remove writable by group and remove readable by other.


Type 2:

  • To altering permissions uses an =.
  • Assign new permissions rather than a change to the permissions.
1
chmod u=rwx,g=r,o= file1.txt

To make file1.txt readable, writable, and executable to the user, readable to the group, and nothing to the world,

  • To let other to have no access, place no letters after the o=

Type 3:

  • Changing all of the permissions with a 3-digit number.
  • Each digit is the summation of the access rights granted to that party (user, group, other).
  • readability is a 4 (100).
  • writability is a 2 (010).
  • executability is a 1 (001).
1
chmod 750 file1.txt

To have readable, writable, and executable access for the owner, readable, and executable access for the group and no access for the world.


Reading permissions


[Example 3-Digit Permissions]


References

R. Fox, Linux with operating system concepts. Boca Raton, Fl: Crc Press, 2015.


Outro

尾巴
有关Linux的学习,马上继续更新。
最后,希望大家一起交流,分享,指出问题,谢谢!


原创文章,转载请标明出处
Made by Mike_Zhang




感谢你的支持 | Thank you for supporting

Linux Learning 2 - File System
https://ultrafish.io/post/linux-learning-2/
Author
Mike_Zhang
Posted on
December 28, 2022
Licensed under