The file command is used to determine the data type of a particular file - in particular, helps us determine what type of data it contains.
Note
The particular use of file is because in Linux file extensions are not necessary. We often use them by convention, but we could just as easily not have them
stat
Provides detailed information about a particular file or directory. It includes information such as:
Used to find files in a directory tree. For example, if we want to find all regular files in a specific directory with permission attributes of 644:
Example
To find all regular files in a directory with permission attributes of 644:
find /var/log -type f -name "*.log" -perm 644
locate
Can be used to find files in the system.
Example
To find a file called “config” (case-insensitive) we might do:
locate -i config
Locate is a lot like fzf, except it is not interactive - it will simply print to the screen data from an updated, indexed database about files in the system.
diff
diff is used to see the difference between files line by line.
Comes with multiple options:
Flag
Use
-b
Ignore changes in amount of whitespace
-w
Ignore all whitespace differences
-i
Compare case-insensitive
-t
Preserve tab chars
-c and -u
Show diffs in readable format
When reading the output of diff, some characters are important:
--- identifies the first argument (the first file passed in), while +++ indicates the file that is compared to determine differences. @@ marks where a change occurred in a file.
- indicates what was removed, while + indicates what was added.
sdiff
sdiff is a fancier version of diff that displays the differences side by side (much like the working tree in VSCodium).
For sdiff, the pipe symbol | indicates that there is a difference between files; < means a specific line is only in the first file, while > indicates the line is only in the second file.
lsof
lsof stands for “List Open Files”, and shows which files are current open in the system and which processes are using them.
This is useful to see why a file can’t be modified, unmounted, etc.
File Links
We can use links to make multiple paths lead to the same file. In general, ln is the command used to create both hard and symbolic links between files.
Because ln can overwrite the file being referenced, a common option is to use --backup to maintain a backup of the original file before overwriting.
We can also use -s to create symbolic links; -i prompts for confirmation, and -f forces link creating by removing existing destination file.
hard links
A hard link creates another name for a file that shares the same data. This is difference from symbolic links in that a hard link is simply a second name for an existing file, and in both the original and the new link the same inode will be referenced.
Deleting one of the created files will not remove the underlying data if there is another file pointing to the same inode.
symbolic links
A symlink creates a pointer that references the original file’s path. Unlike hard links, files created with symbolic (or soft) links will have their own inode. Because of this, the symlink simply stores the location of the target, not the actual data - as a result, deleting or moving the target file means that the symbolic link will break.
Example
By running the following command we will be able to access the /usr/local/bin/backup file by navigating to ~/backup. In other words, we will be redirected to the source dir.