Linux Files and Directories
This is a page where file and directory properties are discussed.
Introduction to Files and Directories
In Linux data is stored in files. Files are organized in directories(folders). Files and directories are contained within filesystems. Multiple filesystems may be in use on a computer.
Each file has a name, which you create. Filenames can be composed of letters (upper and lower case), numbers and simple punctuation (hyphens and underbars included). Actually any character can be used except / (slash), but you have to do extra work to specify non-alphanumeric filenames, and you'll regret using one. Many filenames end with an extension, i.e. a dot and one to three letters. It is conventional to use an extension that signifies what is in the file, e.g. “.c” for C-language source code or “.py” for a Python script. Some programs, such as web browsers, depend on the extension to tell how to process the file.
Files are are organized in directories. Every user has a home directory, and you can (and should) create sub-directories within it, one for each project or purpose. For example:
/users/jdoe/
code/
convec_sim/
diffeq/
documentation/
Sage-9.4/
tmp/
Above there are folders for coding projects, documentation, and for temporary files.
Every program(process) you run executes in a particular directory called the current directory. You'll see the current directory shown the shell prompt:
jdoe@wurc1:~/code/convev_sim$
In this case the directory is "~/code/convev_sim". More on that later.
When a directory or file name is printed, it can be done so in two ways: as a relative or absolute path.
There are two special directories: "." and "..":
- . - This is an abbreviation for the current directory
- .. - This is an abbreviation for the parent(upper) directory. If the current directory is "/users/jdoe/code/convec_sim/" then ".." is the same as "/users/jdoe/code/"
A relative path is relative to the current directory. All relative paths begin with either "./" or the name of a file or folder. All absolute paths begin with a "/". The path "/" represents the top-level of the filesystem hierarchy.
Relative paths:
- . - The current directory
- ./diffeq/ - A path to a folder name diffeq in the current directory.
- tmp/test.c - A path to a file named test.c in a folder named tmp in the current directory
- ./tmp/test.c - Same as the previous one
- results.csv - A file name results.csv in the current directory.
- ../diffeq/test.c - A file in a folder in the parent directory
- ../../documentation/sage-9.4/README - Two levels up...
Absolute paths:
- /users/jdoe/tmp/
- /users/jdoe
- /tmp
Another abbreviation: "~" represents your home directory. In the prompt above you see "~/code/convev_sim". If your home directory is "/users/jdoe" then "~" is the same as "/users/jdoe/code/convev_sim"
Use the cd command to change to another directory. Use ls to list files and folders in the current directory. Using cd without a directory name takes you to your home directory. Example:
jdoe@wurc1:~$ cd code
jdoe@wurc1:~/code$ ls
convev_sim diffeq
jdoe@wurc1:~/code$ cd diffeq/
jdoe@wurc1:~/code/diffeq$ cd ../../documentation/
jdoe@wurc1:~/documentation$ ls
Sage-9.4
jdoe@wurc1:~/documentation$ cd Sage-9.4/
jdoe@wurc1:~/documentation/Sage-9.4$ cd ~/code
jdoe@wurc1:~/code$ cd
jdoe@wurc1:~$
To create a file you need to use an editor. Learn more about Linux Editors here.
To create a folder we use the mkdir command:
jdoe@wurc1:~/code$ ls
convev_sim/ diffeq/
jdoe@wurc1:~/code$ mkdir hi
jdoe@wurc1:~/code$ ls
convev_sim diffeq hi
Lets create a simple python script:
jdoe@wurc1:~/code$ cd hi
jdoe@wurc1:~/code/hi$ nano hi.py
Enter:
#!/usr/bin/python3
print("Hello!")
Note: if the first line of a file starts with '#!' it's called a shebang and tells the shell what program to use to run the file. More on this later.
Then to save: CTRL-X, presss 'y', press enter.
To run it we can do this:
jdoe@wurc1:~/code/hi$ python3 hi.py
Hello!
jdoe@wurc1:~/code/hi$
A simpler way to run it would be to this:
jdoe@wurc1:~/code/hi$ ./hi.py
-bash: ./hi.py: Permission denied
Note: to run a program that is in our current directory we must explicitly give a relative or absolute path to it.
But we appear to not have permission to do this. Lets look closer:
jdoe@wurc1:~/code/hi$ ls -l
total 4
-rw-rw-r-- 1 jdoe jdoe 35 Apr 17 11:59 hi.py
The "-l" option to ls tells it to display the long format. There are seven fields shown:
- Permissions
- Number of hardlinks to this file(not important for this lesson)
- The owning user
- The owning group
- The file size in bytes
- The last modification date and time
- The file name
Permission are broken down this way:
- rw- rw- r--
Permissions for "other" users
Permissions for owning group
Permissions for owning user
Extra permissions beyond the scope of this lesson
Each permission triplet represents three bits: Read, Write, eXecute. "rw-" means read, write, but no execute permission. The example above - "-rw-rw-r--" - means the owning user has RW, the owing group has RW, and all other users have only R permission.
The chmod command changes file permissions. Examples:
- chmod u=rwx,go= - set user permission to RWX and remove all permissions from group and other
- chmod ug=rw,o=r - set user and group to RW and set other to R.
- chmod ug+x,o+r - ADD X permission to existing permissions for user and group, add R to other
- chmod g-x,o= - REMOVE X permission from existing permissions for group and remove all permissions from other
Lets give ourselves execute permission for our script:
jdoe@wurc1:~/code/hi$ chmod u+x hi.py
jdoe@wurc1:~/code/hi$ ls -l
total 4
-rwxrw-r-- 1 erikm erikm 35 Apr 17 11:59 hi.py
Now we can run our script like this because it is marked as executable and it has a shebang line with the path to python3:
jdoe@wurc1:~/code/hi$ ./hi.py
Hello!
jdoe@wurc1:~/code/hi$
Lets give execute permission to our group as well:
jdoe@wurc1:~/code/hi$ chmod g+x hi.py
jdoe@wurc1:~/code/hi$ ls -l
total 4
-rwxrwxr-- 1 erikm erikm 35 Apr 17 11:59 hi.py
Lets remove all permissions from all other users:
jdoe@wurc1:~/code/hi$ chmod o= hi.py
jdoe@wurc1:~/code/hi$ ls -l
total 4
-rwxrwx--- 1 erikm erikm 35 Apr 17 11:59 hi.py
For directories the permissions have a slightly different meaning:
- o=rwx - Other users can create files(W), list contents(R), and access content(X)
- o=r - Others users can get ONLY names of files in the folder. Not useful.
- o=w - Not useful.
- o=x - Others access contents(X) if they know the file name and they have access to the specific file.
- o=rw - Not a useful set of permissions.
- o=rx - Other users can list directory contents and access files they have permissions to.
Other file manipulation commands
First, all commands are thoroughly(but tersely) documented in the system manual pages. You can view any command's manual page using the man command. Example:
man rmdir
See the less command below for help as the man command uses less to display the text in a paginated way. Or man less!
You'll use these commands frequently:
- rm file - Removes a file. Be careful! You can't un-remove it later. Please remove files you aren't going to use anymore. Also, rm -r directory will remove every file in directory. If you need to remove multiple files/directories and you do not want to be asked about each one use the '-f' switch: rm -rf directory1 file1 file2 . Be careful!
- rmdir directory - Removes directory only if it is empty.
- cp file1 file2 - Makes a copy of file1, calling it file2. If file2 does not exist it is created. If it does exist, it is overwritten.
- du subdir - Shows how much disk space the files in subdir occupy. Also, du -s subdir shows the disk space occupied by subdir itself.
- ls - List of files. You may specify specific files or directories to be listed.
- ls -l gives a "long" list showing the file's date, size and permission.
- ls -s gives a "simple" list with sizes in kilobytes
- Files whose names begin with "." (dot) normally are not shown.
- ls -a shows them.
- mkdir directory - Makes a directory called directory.
- less file - Displays file one page at a time, starting at the top of the file. Type "h" for help. "q" to quit.
- mv old new - Moves or renames file old to new. It could be moved to a different directory and the simple name could be changed. If new is a directory that already exists, file old will be placed in it. This form still works if several old files are specified.
- mv file dir/ - move file to a directory
- mv file1/ dir/file2 - rename and move
- quota - Checks your quota of file space. If it displays nothing, you're OK. Also, quota -v displays a report even if you're not over quota. Please remove files that you're not going to use any more, particularly large ones.