• English
  • Japanese

Linux Introductory Guide

1. Directory structure

This page explains the Linux file system before explaining the Linux commands. Linux has a unique hierarchical structure.It is called a tree structure, as it resembles an upside-down tree. The top level is called the root. Subdirectories and files are placed under this directory. You can also create files or subdirectories under subdirectories.

When you log in, you are automatically taken to your directory. Your directory, also called the home directory, is expressed by a "‾" (tilde). If you type the pwd command (described in 5.2) to check the current directory, you see a character string similar to /home/student.... A full directory path starting with a forward slash (/) is called an absolute path name.The working directory is called the current directory.

".." represents the current directory's parent directory. Also, if the current directory contains a directory named "hogehoge", it is described as "./hogehoge/". A directory path that is relative to a current directory like this is called a relative path name.

2. Basic commands

The basic command structure is shown as below.

command option arguments

command represents a command to be executed.option represents the function to be executed. option represents the function to be executed. This is used when one command has more than one function. This is followed by the target file name or number (arguments). On this page, variables such as file and directory names are expressed by file. Characters enclosed by [ ] can be omitted.Generally, single-character options can be placed in succession.

-a -b -c -> -abc

Note that commands are case-sensitive.

3. Correct file names

Use alphanumeric characters to name files. Do not use Kanji characters or special symbols. File names are also case-sensitive.

4. Wildcard characters in file names

It is inconvenient to specify each file name to be copied when you wish to copy multiple files. The characters "*" (asterisk) and "?" represent an arbitrary character string and a single character, respectively. For example, when you type

cp * /tmp

all of the files in the current directory are copied to /tmp. And when you type

rm foo.?

files such as foo.1 or foo.c are deleted. Files such as foo.txt are not deleted. Thus "?" matches a single character, while "*" matches any number of characters. Be very careful when you type "rm *". Doing so can delete all of the system's necessary files.

Square brackets ("[" and "]") can be used to list characters to match. For example, when you type

rm foo.[tcld]*

files of which the first letter following a period matches any characters in the brackets, such as foo.tex, foo.c and foo.log, are deleted.

5. Basic commands

Disks are limited resources.Storing unneeded emails and files on the disk wastes resources and inconveniences other workstation users.This is why each user must organize his or her own directory.Use the following commands to manipulate files.

5.1 List files (ls)

ls [-alF] [dir]
Description of options
-a  Lists the files, including those starting with "."
-l  Returns detailed file information
-F  Appends "/" to directories and "*" to executable files

When you specify a directory name, the files in that directory are listed.
If you type "-a", "." and ".." are returned. The current directory is represented by ".", while the current directory's parent directory is represented by "..".

5.2 Display the working directory (pwd)

pwd

Returns the working directory (current directory).

5.3 Move to another directory (cd)

cd [dir]

When you specify a directory, you are taken to that directory. Otherwise, you are taken to the home directory.
If the current directory is /tmp/hoge and you wish to go to the parent directory, /tmp, type

cd ..

You are then taken to /tmp.To go to the parent's parent directory from /tmp/hoge, type

cd ../..

You are then taken to the parent's parent directory.

5.4 Create a directory (mkdir)

mkdir dir

Creates a directory with the specified name.

5.5 Delete a directory (rmdir)

rmdir dir

Deletes the directory with the specified name.Note that the specified directory is not deleted if it contains files.Delete all of the files, and execute the command again.

5.6 Display files (cat, more)

cat [-n] file

Displays files.If there are too many files to display on the screen at one time, the list scrolls down.

Description on options
-n  Adds line numbers
more dir

Displays files. If there are too many files to display on the screen at one time, ---more--- is displayed. Press any of the following keys to navigate.

<space>         Scrolls down to the next page
<return>        Scrolls down to the next line
<ctrl>          Scrolls up to the previous page
q               Stops displaying files

5.7 Copy and rename a file (cp)

cp file dir

Copies a file to the specified directory.The original file remains unchanged. When you type cp file1 file2 the contents of file1 is copied to file2.

5.8 Move and rename a file (mv)

mv file dir

Moves a file to the specified directory. The original file disappears.

mv oldname newname

Renames a file and moves its contents to the renamed file. The original file disappears.

5.9 Delete a file (rm)

rm files

Deletes the specified file.

5.10 Search file contents (grep)

grep pattern file

Extracts the lines containing the specified character string.For example, if you want to see the lines containing the character string "ABC" in the org.data file, type "grep ABC org.data".

6. Standard input and output

6.1 Redirection

The standard output is to the screen. Optionally, you can save the generated output to a file. By opening the file in an editor, you can check directory files displayed using the "ls" command, or you can read error messages to identify causes of problems.

Use ">" to save the output to a file.

command > file

Redirects the output from the screen to the specified file.To add more output to the file, use ">>".

command >> file

Appends the output to the specified file.

Use a different command to output error messages. ">&" saves error messages in a file, while ">>&" appends error messages to the specified file.

The standard input is from the keyboard. Optionally, you can load data from a specified file.To do this, use "<".

command < file

You can use the sort command to sort the contents of a file in alphabetical order.

sort < file1 > file2

Sorts file1 and saves the result in file2.

6.2 Combining commands

In Linux, you can execute commands in series.This technique is called piping. With piping, the output of one command can be used as the input for a subsequent command. You can use "|" when you type "ls" to list the files in a directory, but they cannot be displayed on the screen at once.

ls | more

This allows you to view the files page by page.

7. Other commands

7.1 Other user information (finger, who)

finger [-il] username

The finger command displays the active user information. The following options are available.

-i  Displays the idle time
-l  Displays the contents of the .plan file if it is in the home directory

By specifying a username without any options, you can see his or her information. Type a first or last name or a login name.

who

Also displays active user information, but with less detail than finger.

7.2 Disk usage (du)

du [-sa] filename dir

Displays the current directory's disk usage.

-s  Displays the total only
-a  Lists the sizes of all files

You can specify a file to check its size.

8. Online manuals

Rich online manuals are one of the important features of Linux. If you have any questions about commands, try asking the computer first.

8.1 Online manuals (man)

man commandname

Returns the help text related to the specified command.If the text cannot fit on one screen, the options more and the more useful less are automatically applied, and the output pauses when a full screen of text is displayed. For details about more and less,refer to the online manuals by typing man more and man less, respectively.

Last-Modified: June 15, 2011

The content ends at this position.