Files

Updated: 2020-06-29

inode

inode = index node. A data structure describes a file or a directory. For a user, a file is a path like /home/yourname/whatever.txt; for the operating system, an inode is used to store the extra info about the file, like file type, owner, group, who can access the file, size of the file, last modified time, etc.

Manage Files

Create Files

$ touch a.txt             # Create an empty file
$ echo "hello" > b.txt    # Create a file with "hello"
$ echo "world" >> b.txt   # append content to the existing file

Remove Files

$ rm a.txt

Manage Directories

$ mkdir dir1
$ rmdir dir1
$ rm -r dir2

ln -s will create symbolic links, or soft links, or symlinks(calling symlink system call), while without -s will create hard links(calling link system call).

$ sudo ln -s <source_path> <target_path>

For example:

$ ln -s 0.1.0-SNAPSHOT/ snapshot
$ ls -l
... snapshot -> 0.1.0-SNAPSHOT/

File Info

Use ls -l to check file info:

$ ls -l
-rw-rw-rw- 1 ubuntu ubuntu    13 Mar 12 16:56 a.txt

Type

The first char is the type of the file:

  • -: regular file
  • d: directory
  • l: symbolic link

Permissions

Next are 3 groups of rwx describing the permissions:

  • 1st rwx: permissions for owner of the file
  • 2nd rwx: permissions for group owners of the file
  • 3rd rwx: permissions for all other users

where

  • r: permission to read
  • w: permission to write
  • x: permission to execute
  • -: no permission

Instead of rwx, we can also use a number between 0 and 7 to describe the permissions: map rwx to the binary form of the number, for example,

  • 7 => 111 => rwx: you have all the permissions
  • 6 => 110 => rw-: you can read or write but not execute
  • 5 => 101 => r-x: you can read or execute but not edit
  • 4 => 100 => r--: read only
  • 0 => 0 => ---: no permission at all

Then a file's permission can be described by 3 numbers, e.g. 755 is equivalent to rwxr-xr-x.

To change a file's permission, use chmod

$ chmod 755 a.txt

or use something like this if you do not like the numbers:

$ chmod a+rw a.txt

where a is for all users, + is to add permissions, rw is for read and write. Check $ man chmod for all available options.

Owners

-rw-rw-rw- 1 ubuntu ubuntu    13 Mar 12 16:56 a.txt
             ------ ------
                |      |
                |      |---- group owner of the file
                |----------- owner of the file

To change owners, use chown:

$ sudo chown root:root a.txt

The first root is the OWNER while the second root is the GROUP.

Search Files

Find files with .txt suffix in home directory

$ find ~ -name "*.txt"

Or use locate

$ locate passwd

locate uses a database (using updatedb) rather than hunting individual directory paths.

which is used for locating binaries; whereis lists locations for binaries, sources, and man pages.

grep is used to search inside the content.