Topics Map > Computing > linux

Linux Files and Directories

This is a page where file and directory properties are discussed.

Files and Directories

In UNIX data is stored in a file. Each file has a name, which you create. Filenames should 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 “.gif” for an image in the GIF format. Some programs, such as web browsers, depend on the extension to tell how to process the file.

Files are indexed in directories. Every user has a home directory, and you can (and should) create sub-directories within it, one for each project or purpose. Every process executes in a particular directory called the current directory. Use the cd command to change to another directory. Files in the current directory are accessed just by their names. To specify a file in another directory give the name of that directory, slash, the filename. The directory that contains a directory can be referred to as “..” (dot-dot), and the home directory is abbreviated “˜” (tilde) or $HOME. 

Here's a complicated filename: ˜/tasks/lapack/Notes2.txt. This means: In my home directory, subdirectory “tasks”, its subdirectory “lapack”, the file “Notes2.txt” (which judging from the extension probably contains text).

To create a file you need to use an editor or use output redirection.  You can type "touch file1" and an empty file will be created.   Learn more about Linux Editors here.

cd directory
Changes to the directory directory, which becomes the current directory.
Also, cd .. changes to the containing directory.
Also, cd (without arguments) changes to your home directory.

chmod perm file
Sets the permission of one or more files or directories. Useful permissions are:
Only you can use the file.
Same, for directories and executable files.
Anyone can look at the file; only you may write. This permission is required if a file is to be visible on the World Wide Web.
Anyone may look at files in the directory (if the file permission allows); only you may put files in it.

For directories, anyone may use a file if the name is known (or guessed) and the file permission allows, but others may not do ls in the directory to snoop for other filenames. Often used on home directories.

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.

List of files. You may specify specific files or directories to be listed.
e.g. ls -l gives a "long" list showing the file's date, size and permission.
e.g. ls -s gives a "simple" list with sizes in kilobytes Files whose names begin with "." (dot) normally are not shown.
e.g. ls -a shows them.

mkdir directory
Makes a directory called directory.

more file
Displays file one page at a time, starting at the top of the file.

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.

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.

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.

rmdir directory
Removes directory

Simple Editor Commands

emacs Starts the Emacs text editor.

ispell Starts an interactive spell-checker, that can be used from unix or from within an editor. This program is better than spell.
e.g. ispell -h summarizes options.
e.g. ispell -t file.tex checks the spelling of TeX or LaTeX files.

pico Starts the Pico text editor.

spell Starts the standard unix spell-checker.

vi or vedit or view Starts the vi text editor.

Learn more about Linux Editors here.

Keywords:linux, command line, directories, files, permissions   Doc ID:114558
Owner:Sara N.Group:UW Math Department
Created:2021-10-28 14:00 CDTUpdated:2021-10-29 13:22 CDT
Sites:UW Math Department
Feedback:  0   0