Lesson 6: 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.
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.
Everything in Linux is a file... except the things that aren't.
Files have:
Owner | atime, ctime, mtime |
Group | POSIX ACLs |
Permissions | Spinlock |
Inode | i_ino |
Size | read, write and link count |
Filename |
$ 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
Yes. Except the things that aren't...
This functionality isn't just limited to the shell! Let's say you're programming an interface for a device that streams data from a sensor. Using the "Everything is a file" philosophy, we could read data from the device like so:
int read_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;
}
$ ls -l
crw-rw-rw- 1 root tty 5, 0 Jan 6 13:45 /dev/tty
brw-rw---- 1 root disk 8, 0 Dec 21 14:12 /dev/sda
srw-rw-rw- 1 root root 0 Dec 21 14:13 /var/run/acpid.socket
prw------- 1 lance lance 0 Jan 5 17:44 /var/run/screen/S-lance/12138.ramereth
lrwxrwxrwx 1 root root 4 Nov 25 09:26 /var/run -> /run
$ stat /etc/services
File: `/etc/services'
Size: 19303 Blocks: 40 IO Block: 4096 regular file
Device: fc00h/64512d Inode: 525111 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-01-07 08:22:43.768316048 -0800
Modify: 2012-05-03 09:01:30.934310452 -0700
Change: 2012-05-03 09:01:30.982310456 -0700
Birth: -
.jpg, .txt, .py
Not necessary, more of a recommendation.
File contains information about its encoding
$ ls
some_text_file squirrel
$ file some_text_file
some_text_file: ASCII text
$ file squirrel
squirrel: JPEG image data, JFIF standard 1.01
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
$ 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
| +------------------------------------- References Count
+---------------------------------------------- File Permissions
& Type
You can edit the metadata of a file with various commands, but some of the most useful commands are chown, chmod, and chgrp commands. These commands allow you to edit the owner, the read/write/execute, and the group permissions of a file respectively.
# Change the owner of myfile to "root".
$ chown root myfile
# Change the owner of myfile to "root" and group to "staff".
$ chown root:staff myfile
# Change the owner of /mydir and subfiles to "root".
$ chown -hR root /mydir
# Make the group devops own the bootcamp dir
$ chgrp -R devops /home/$yourusername/bootcamp
+-----+--------+-------+
| 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 |
+-----+--------+-------+
Example:
$ chmod ug+x my_script.sh
# Adds the permission to execute the file to its
# owner user and owner group.
$ chmod o-w myfile.txt
# Removes the permission to write to the file
# from users other than its owners.
When a file has the +x permission set, it can be run as a program.
For instance:
$ ls -lh 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!
Directories are also files!
$ ls -alh | grep foobarbaz
drw-rw-rw- 2 voigte voigte 4.0K Sep 29 10:47 foobarbaz
# Below is the literal output, not pseudo-output
$ ls -alh foobarbaz
ls: cannot access foobarbaz/.: Permission denied
ls: cannot access foobarbaz/..: Permission denied
total 0
d????????? ? ? ? ? ? .
d????????? ? ? ? ? ? ..
# create empty file called foo
$ touch foo
$ touch bootcamp/emptyfile
$ ls -alh bootcamp/emptyfile
-rw-rw-r-- 1 dobc dobc 0 Nov 3 22:38 bootcamp/emptyfile
# You may need to create the devops group.
$ sudo chown dobc:devops bootcamp/emptyfile
# Alternatively, you can also do the following
$ sudo chgrp devops bootcamp/emptyfile
$ touch allperms
$ chmod ugo+rwx allperms
$ ls -l allperms
-rwxrwxrwx 1 dobc dobc 0 Nov 3 22:39 allperms
Bonus: What's another way of giving a file all permissions?