Lesson 5: Files

Homepage Content Slides Video

Warning

This lesson is under construction. Learn from it at your own risk. If you have any feedback, please fill out our General Feedback Survey.

Overview

About Files

Everything in Linux is a file... except the things that aren't.

Files have:

$ ls -il
total 8
2884381 drwxrwxr-x 5 test test 4096 Nov  6 11:46 Documents
2629156 -rw-rw-r-- 1 test test    0 Nov 13 14:09 file.txt
2884382 drwxrwxr-x 2 test test 4096 Nov  6 13:22 Pictures

Everything is a file!?

Yes. Except the things that aren't..

int read_medical_device_data(int device_file_pointer) {
    // Open a connection to the device
    int * stream = open(device_file_pointer);
    // Write the stream of data to the screen
    write(STDOUT, stream);
    // Do some other stuff with that data
    // Close the data stream
    close(stream);

    return EXIT_SUCCESS;
}

File Extensions

.jpg, .txt, .py

Not necessary, more of a recommendation.

$ ls
some_text_file  squirrel

$ file some_text_file
some_text_file: ASCII text

$ file squirrel
squirrel: JPEG image data, JFIF standard 1.01

Hidden Files

Any file starting with . is called a hidden file and is not listed by default.

Adding the -a flag to ls command includes hidden files in your output.

$ ls
Documents  file.txt  Pictures

$ ls -a
.  ..  Documents  file.txt  .hidden_file  Pictures  .vimrc

Finding Metadata with 'ls -l'

$ ls -l
drwxrwxr-x   5   test     test     4096   Nov  6 11:46 Documents
-rw-rw-r--   1   test     test        0   Nov 13 14:09 file.txt
drwxrwxr-x   2   test     test     4096   Nov  6 13:22 Pictures
----------     -------  -------  -------- ------------ -------------
    |             |        |         |         |             |
    |             |        |         |         |        File Name
    |             |        |         |         +--- Modification Time
    |             |        |         +-------------  Size (in bytes)
    |             |        +-----------------------       Group
    |             +--------------------------------       Owner
    +----------------------------------------------  File Permissions

Editing Metadata

$ chown root myfile
  # Change the owner of myfile to "root".

$ chown root:staff myfile
  # Change the owner of myfile to "root" and group to "staff".

$ chown -hR root /mydir
  # Change the owner of /mydir and subfiles to "root".

$ chgrp -R devops /home/$yourusername/bootcamp
  # Make the group devops own the bootcamp dir

chmod and Octal Permissions

+-----+--------+-------+
| rwx | Binary | Octal |
+-----+--------+-------+
| --- | 000    | 0     |
| --x | 001    | 1     |
| -w- | 010    | 2     |
| -wx | 011    | 3     |
| r-- | 100    | 4     |
| r-x | 101    | 5     |
| rw- | 110    | 6     |
| rwx | 111    | 7     |
+-----+--------+-------+

chmod and Octal Permissions

$ chmod ug+x my_script.sh

$ chmod o-w

Executing a File?

When a file has the +x permission set, it can be run as a program.

For instance:

$ ls -alh my-script
-r-xr-xr-x 1 username username 1.9K Sep 27 09:44 my-script

$ cat my-script
#!/bin/bash
# The above line tells Linux how to invoke the script on my behalf.
echo 'This is a script being run without using bash!'

$ ./my-script  # my-script is invoked just like a compiled binary!
This is a script being run without using bash!

Types of Files

Directories

Directories are also files!

$ ls -alh | grep foobarbaz
drw-rw-rw-  2 voigte   voigte   4.0K Sep 29 10:47 foobarbaz

$ ls -alh foobarbaz   # Below is the literal output, not psuedo-output
ls: cannot access foobarbaz/.: Permission denied
ls: cannot access foobarbaz/..: Permission denied
total 0
d????????? ? ? ? ?            ? .
d????????? ? ? ? ?            ? ..

TODO: Messing with Files

$ touch foo # create empty file called foo

Further Reading