File System
Objective
- Perform more operations on the file system.
- Get familiar with file permission assignment.
- Get familiar with text editors.
Summary
alias
chgrp
chmod
chown
emacs
find
gnome-text-editor
grep
id
nano
tree
vim
wc
Aliases
An alias lets you create a shortcut name for a command, file name, or any shell text. By using aliases, you save a lot of time when performing frequent tasks. An alias
is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command, for example:
> alias ll="ls -l"
> ll
total 56
drwxr-xr-x. 1 tux tux 0 Jan 25 00:16 Desktop
drwxr-xr-x. 1 tux tux 30 Jan 29 13:59 Documents
drwxr-xr-x. 1 tux tux 62 Jan 29 14:03 Downloads
...
An alias will last for the life of the shell session. Regularly used aliases can be set from the shell’s rc
file, such as ~/.bashrc
, so that they will be available upon the start of the corresponding shell session:
echo 'alias l="ls"' >> ~/.bashrc
echo 'alias ll="ls -la"' >> ~/.bashrc
source ~/.bashrc
l
ll
Note: alias
, without arguments, prints the list of defined aliases.
Searching Files
grep
Looks for strings in a file. Its name comes from the ed, a text editor, command g/re/p
(global / regular expression search / and print), which performs the same function.
Syntax
grep [-options] pattern file [file ...]
Option | Description |
---|---|
-c | Count |
-i | Case insensitive search |
-l | List filename only |
-v | Display lines with no pattern match |
Example
> grep root /etc/passwd
root:x:0:0:Super User:/root:/bin/bash
operator:x:11:0:operator:/root:/usr/sbin/nologin
> grep "FTP User" /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/usr/sbin/nologin
Exercise 1
- Display the contents of
/etc/hosts
to show all known systems on your network. - Locate all occurrences of the word
nologin
in the/etc/passwd
file. - Make a directory called
cse
. - Create another directory under
cse
calledcs
. - Create a file called
cmps405
. Movecmps405
intocs
. Verify the contents ofcs
.
Locating Files
find
Used to locate files within the directory structure that meet specified search criteria.
Syntax
find path predicate-list
path
is the pathname of the directory to start the search from. All subdirectories are searched recursively by default.
Predicate | Description | Example |
---|---|---|
-atime n | Accessed n days ago | -atime 10 |
-atime +n | Accessed more than n days ago | -atime +90 |
-atime -n | Accessed less than n days ago | -atime -30 |
-ctime n | Created n days ago | -ctime 1 |
-ctime +n | Created more than n days ago | -ctime +15 |
… | See man find for more predicates under tests |
Example
> find / -ctime +10 -name include
Exercise 2
- Create the following file structure under the current running user:
file-a
file-b
file-c
exercise-1.c
hw-1.md
prog-1.c
project-1.md
hello.png
hw-1.png
hw-1.svg
- Add sample C source code to the
*.c
files. - List all C file names whose content has
main
using one command. - Copy all C files only from the
docs
directory to thesource
directory using one command. - Move the
backup
directory and put it inside theimages
directory using one command. - Starting from your home directory, find all files whose name start with the letter
D
using one command.
Sorting Lines
sort
Sorts lines, lexicographically or numerically, in a file.
Syntax
sort [-nr] filename
Example
> sort /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/usr/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/usr/sbin/nologin
chrony:x:990:989:chrony system user:/var/lib/chrony:/sbin/nologin
...
Counting Words
wc
Counts lines, words, and characters in a file.
Syntax
wc [-lwc] filename
Option | Description |
---|---|
-l | Lines |
-w | Words |
-c | Characters |
None | Show all counts |
Example
> wc -l /etc/passwd
46 /etc/passwd
> wc -w /etc/passwd
106 /etc/passwd
> wc /etc/passwd
46 106 2646 /etc/passwd
Text Editing
nano
To start GNU nano, open a terminal and type nano
followed by the name of an existing file or a new file that you want to create. The available shortcuts are displayed at the bottom of the terminal window.
vim
- To start the vim text editor, open a terminal and simply type
vim
followed by the name of an existing file or a new file that you want to create. vim
works in two main modes, one for editing text and the other for giving commands. To switch between the two modes you use thei
and<escape>
keys for editing and commands respectively.- Use the command
:wq
to save (write) changes and quit. - Use the command
:w
to save (write) changes without quitting. - Use the command
:q!
to ignore changes and quit.
emacs
-
To start the Emacs text editor, open a terminal and simply type
emacs
followed by the name of an existing file or a new file that you want to create. -
To save a file, press
C-x C-s
(<ctrl> + x
followed by<ctrl> + s
). -
If you don’t want to save but quit, use the command
C-x C-c
(<ctrl> + x
followed by<ctrl> + c
). If there is unsaved data, pressy
for yes. -
emacs
has the following editing keys:C
stands for<ctrl>
andA
stands for<alt>
:Command Key Combination Moving to the next line C-n
(n
for Next)Moving to the previous line C-p
(p
for Previous)Moving one character forward C-f
(f
for Forward)Moving one character backward C-b
(b
for Backward)Moving one word forward A-f
(f
for Forward)Moving one word backward A-b
(b
for Backward)Moving to the start of a line C-a
Moving to the end of a line C-e
(e
for End)Moving one page down C-v
(or<page-down>
)Moving one page up A-v
(or<page-up>
)
gnome-text-editor
To start the GNOME text editor, open a terminal and type gnome-text-editor
followed by the name of an existing file or a new file that you want to create. Notice that the terminal is occupied until you exit from the editor.
Linking Files
Symbolic links are files that point to other files or directories. They are similar to shortcuts and hyperlinks.
ln
Creates a symbolic or a hard link.
Syntax
ln [-s] original-file symbolic-link
Example
> touch file
> ln -s file link
> ls -l
total 4
-rw-r--r--. 1 tux tux 0 Feb 4 20:49 file
lrwxrwxrwx. 1 tux tux 4 Feb 4 20:49 link -> file
> echo "Hello, world!" >> file
> cat link
Hello, world!
> echo "Hello, multiverse!" >> link
> cat file
Hello, world!
Hello, multiverse!
> ls -l
total 4
-rw-r--r--. 1 tux tux 0 Feb 4 20:50 file
lrwxrwxrwx. 1 tux tux 4 Feb 4 20:49 link -> file
> rm file
> ls -l
total 4
lrwxrwxrwx. 1 tux tux 4 Feb 4 20:49 link -> file
File Security
- File security limits access to the files. The system provides you with the commands to specify who can access a file and, once accessed, what type of operations can be done on the file.
- The three types of permissions are:
r
(read),w
(write) andx
(execute). - To deny a certain permission
-
is used in place ofrwx
, e.g.r--
r-x
- Access may be granted to or revoked from the following types of users: the owner or user (
u
), members of the group (g
), other users (o
). Thea
option may be used instead ofugo
. - Operators used are
+
(grants a permission),-
(denies or revokes a permission), and=
(sets all permissions for a specified user). - Permissions are listed and applied in the following order:
u
[rwx
],g
[rwx
],o
[rwx
]. - The
ls -l
command is used to view or list permissions. - The
chmod
command is used to set the file permission (access mode).
Listing Permissions
File information may be displayed using ls -l
and has the following format for each entry:
- Type, permissions, and attributes
- Entry type
- User permissions
- Group permissions
- Others permissions
- Extended attributes
- Hard-link count
- Size (bytes)
- User (owner)
- Group (owner)
- Last modification date
- Name
Entry Type
Symbol | Type |
---|---|
- | Ordinary file |
d | Directory |
l | Symbolic link |
b | Block special file |
c | Character special file |
s | Local socket |
p , | | First-in/first-out pipe special file |
Users
Symbol | Type | Remarks |
---|---|---|
u | User | Owner of the file which is usually the file creator |
g | Group | Members of the group with which the file is associated; usually the primary group of the file creator |
o | Others | Everyone else, that is, anyone who is not the file owner or a member of the group. |
To display your user id and group information, use the id
command:
> id -a
uid=1000(tux) gid=1000(tux) groups=1000(tux),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
> id tux
uid=1000(tux) gid=1000(tux) groups=1000(tux),10(wheel)
> id root
uid=0(root) gid=0(root) groups=0(root)
Permissions
Symbol | Permission | Effect on Files | Effect on Directories |
---|---|---|---|
r | Read | Examine or copy file contents | List filenames (ls ) |
w | Write | Modify file contents | Add and remove (cp , rm , touch , mv ) |
x | Execute | Run or execute a script or a program | Obtain files’ details and search the directory (cd , ls -l , find ) |
- | Revoke | Used in place of a permission to revoke it (deny access) | Similar to the effect on files |
Changing Permissions
chmod
-
Permissions may be changed for several reasons:
- to hide private files from other users
- to share files with other users
- to make files modifiable
- to make files executable
- to prevent or allow modification of directory contents
-
File and directory permissions are changed using the
chmod
command. -
Only the owner of the file or the superuser (
root
) can change the permissions. -
Permissions can be specified using either the symbolic method or the octal method.
-
You can change the owner and group using the
chown
andchgrp
commands, respectively, for example:sudo chown root file sudo chgrp wheel file ls -l file
Symbolic Method
chmod [who][action][access] file [file ...]
who
:u
,g
,o
,a
.action
:+
,-
,=
.access
:r
,w
,x
.
Example
> mkdir directory
> cd directory
> touch tigers bears elephants
> ls -l
total 0
-rw-r--r--. 1 tux tux 0 Feb 4 20:39 bears
-rw-r--r--. 1 tux tux 0 Feb 4 20:39 tigers
-rw-r--r--. 1 tux tux 0 Feb 4 20:39 elephants
> chmod u-w tigers
> ls -l tigers
-r--r--r--. 1 tux tux 0 Feb 4 20:39 tigers
> chmod go-r bears
> ls -l bears
-rw-------. 1 tux tux 0 Feb 4 20:39 bears
> chmod u=rx,g=r elephants
> ls -l elephants
-r-xr--r--. 1 tux tux 0 Feb 4 20:39 elephants
> chmod o= elephants
> ls -l elephants
-r-xr-----. 1 tux tux 0 Feb 4 20:39 elephants
Exercise 3
- Use
touch
to create a file calledfile_modes
:
touch file_modes
- List the file information by typing:
ls -l file_modes
- Remove all permissions from this file using:
chmod ugo= file_modes
- List the file information and confirm the change.
- Verify that you can neither read from nor write to this file.
cat file_modes
echo "date" >> file_modes
- Add write permission for yourself:
chmod u+w file_modes
- Verify that you can write now to the file
file_modes
:
echo "date" >> file_modes
- Now add write permission for everyone else. Also add read permission for yourself:
chmod go+w,u+r file_modes
- Verify that you can now read from the file
file_modes
:
cat file_modes
- Assign everyone read and write permission to your file:
chmod ugo=rw file_modes
- Now deny everyone write permission, but grant everyone execute permission:
chmod a-w,a+x file_modes
- Finally, verify that your file can now be executed:
./file_modes
As file_modes
contains the command date
, the output should be the current system date.
Octal Method
The permission categories rwx
can be represented numerically: A granted permission is represented by 1
and a revoked permission by 0
. The rwx
combination for any user category may now be represented as a 3-digit binary number which is equivalent to one octal digit:
rwx | Binary 4 2 1 | Octal Digit | Privilege |
---|---|---|---|
--- | 0 0 0 | 0 | No privilege |
--x | 0 0 1 | 1 | Execute only |
-w- | 0 1 0 | 2 | Write only |
-wx | 0 1 1 | 3 | Write and execute |
r-- | 1 0 0 | 4 | Read only |
r-x | 1 0 1 | 5 | Read and execute |
rw- | 1 1 0 | 6 | Read and write |
rwx | 1 1 1 | 7 | Read, write, and execute |
The effective access permission for the 3 user categories (ugo
) is represented by just one 3-digit octal number, as shown in the following table:
Access Permissions | Octal Equivalent |
---|---|
rwxrwxrwx | 777 |
r--r--r-- | 444 |
rw-rw-r-- | 664 |
rwxr-xr-x | 755 |
r-------- | 400 |
--------- | 000 |
Example
> ls -l
total 0
-r--------. 1 tux tux 0 Feb 4 20:39 bears
-r--r--r--. 1 tux tux 0 Sep 15 21:22 tigers
-r-xr-----. 1 tux tux 0 Sep 15 21:22 elephants
> chmod 664 *
> chmod 000 tigers
> ls -l
total 0
-rw-rw-r--. 1 tux tux 0 Feb 4 20:39 bears
----------. 1 tux tux 0 Feb 4 20:39 tigers
-rw-rw-r--. 1 tux tux 0 Feb 4 20:39 elephants
Exercise 4
- Create a file called
practice
usingtouch
. - List the files in your directory along with their permissions.
- Change the permissions on the file
practice
so that only you can read and write it. - Change the permissions so that nobody can access the
practice
file, not even yourself. - Verify that you have no access to the file.
- Try to create a file in another user’s directory.