System Access and File System

System Access and File System
File and Directory Properties
Everything in Linux is a file, so each file could represent a device, a directory or even just a text file. The type of the file can be known from the first character of the file properties first column an example is given as follows.
drwxr-xr-x 5 theinhumaneme theinhumaneme 4096 Dec 19 12:27 filename
Different types of files
| property | description |
|---|---|
| d | regular file |
| - | directory |
| l | link |
| c | special file or device file |
| p | named pipe |
| b | block device |
IInd column is the total count of the files, this includes the parent, the children and the file itself.
Finding Files and Directories
there are two primary ways to find files on Linux
find
find command can be used to find files on the file system, every time the command is run, the command traverses through the filesystem to find the
command - find . -name "hello.txt"
usage - find <path-to-search> -name <filename>
locate
locate is also used to find files and directories, instead of traversing the file system it indexes the file system in a db, this indexing process occurs when the device is idle, so this poses an issue for the most recent files not being shown in the search results. This can be mitigated by updating the db just before the search using the command updatedb.
command locate png
usage locate <filename / string>
Wildcards
| Character | operation |
|---|---|
| * | Match 0 or more characters |
| ? | Match a single character |
| [a-z] | Match any char in the range provided |
| \ | Escape char |
| ^ | beginning of the line |
| $ | ending of the line |
Soft and Hard links
In the Linux filesystem all the files are associated with a unique number called the inode, this number is used when there are any operations related to files such as search, rename and linking.
We cannot create soft/hard links in the same directory with the same name.
to view the inodes using the ls command pass the flag -i to the ls command eg ls -i
Softlinks
Softlinks are the links between files that are removed when the file is removed or renamed, softlinks point towards a file and not the inode.
A softlink can be created using the link command ln with the flag -s.
When the source file of a softlink is removed the destination is also removed but the link remains which needs to be manually removed.
We can only create hardlinks on the same partition of the file system else we will get a Invalid cross-device link error.
Hardlinks
Hardlinks are the links between files that point towards the inode directly, a hardlink can be created using the link command ln without any add.
In hardlinks when the source file is removed the destination files or the linked files are not removed.