File and directory commands in linux

Using Linux from the command line

The file systems in Linux machines are based on a hierarchical directory tree. There is one root directory which you can refer to with the forward slash symbol (/). All the files and directories are located in the subdirectories of this directory, so that each file has a unique combination of a name and a directory path. Also, the commands that the user gives are executed in the directory that the user currently is. It is called the current working directory.

Normally, you do not need to know the explicit directory paths when you work in the CSC environment. It is enough to know the locations of the files in the user’s own disk areas. The user- and the project-specific disk areas are presented in chapter 3. However, you should remember that many disk areas in the CSC environment can be accessed from several different servers (e.g., from the user’s home directory) while some areas are server-specific. In the case of shared disk areas, the path to a certain file may be different on different servers.

Structure of Linux commands

Once the terminal connection to CSC, e.g., to [Puhti-shell], has been opened, the remote server is operated by using Linux commands. The standard structure of a command is:

command -options argument1 argument2 .

The command is executed by pressing the return key (Enter). The names and functions of the options and the arguments depend on the Linux command. In many cases, you can run the command without any options and arguments. Options are used to modify the actions that the command performs. Arguments are used to define the files, directories and values that are used as input parameters and to define where the output is written.

For example, the command ls can be used as such or with several options and arguments. Running a plain command ls lists the content of a directory in alphabetical order. You can modify the output of the command, for example, by using the option -t . With this option, the directory content list is ordered according to the age of the file (timestamp). If no argument is given, ls prints the content of the current working directory. By giving an argument to the ls command, the user can define a directory whose content should be listed. For example, the command

ls -t /scratch/project_2979797

will list the content of directory /scratch/project_2979797. In case there aer errors in the command, the arguments or the options, the command will not be executed when the return key is pressed. Instead, an error message is printed to the screen. Thus, having errors in the command often does not cause any major problems. Issues may arise when using undefined shell variables. The output of commands vary from one command to the other but in many cases, no output means that command was successfully executed.

Читайте также:  Linux pipe example c

Most of the Linux commands have their own manual page that can be studied using the man command. For example the manual page of ls command is shown using the command

Manual pages can be very detailed and technical. However, often you do not need to read and understand all the details given there, but instead, you can just see what command-line options are available for the particular command, which are of interest to you and then start testing/using them in practice.

There are thousands of Linux commands, though, you do not need to know the majority of them in order to get started. Below, we introduce the most frequently used ones. You can also use the command apropos to find a suitable command. Apropos lists those Linux commands whose short description lines match the text that is given as a command argument. For example, if you want to look for commands that are processing pdf files, you could type

Note that the listing that apropos prints includes only Linux commands but not program names. Thus, the sample command above would produce a list that contains many pdf conversion commands but no pdf viewers such as acroread or evince .

Basic commands for using directories

The table below lists the commands that are most frequently used for moving in the directory hierarchy and managing it. Below are some examples of directory related commands.

Name Argument Description
cd directory Change current working directory
ls directory List the content of a directory
pwd Print the directory path of current working directory
mkdir directory Create a new directory
rmdir directory Remove a directory

When you log in to a server at CSC, you will be taken to your home directory. You can check your location, i.e., the path of the current working directory with the command pwd (abbreviation from Print current Working Directory). However, you do not have to remember the location of your home directory (see the cd command).

The content of the directory can be listed with the ls command. The plain ls command just lists the names of the files and directories in your current directory. You can get more information about the files and directories with command ls -la . The -l option produces a long directory listing that, in addition to the name, contains also information about the access permissions, the size and the modification time of files and directories. The option -a defines that all files, including also hidden files that start with dot (.) character, are listed. Below is a sample output for ls -la command:

kkayttaj@c305:~>ls -la total 26914 drwx------+ 3 kkayttaj csc 10 Dec 22 09:12 . drwxr-xr-x 20 root root 0 Dec 22 09:12 .. drwx------+ 42 kkayttaj csc 472 Dec 22 09:07 .. -rwxr-x---+ 1 kkayttaj csc 1648 Dec 22 09:01 .cshrc -rw-------+ 1 kkayttaj csc 93 Dec 22 09:01 .my.cnf -rw-------+ 1 kkayttaj csc 48 Dec 22 09:05 Test.txt -rw-------+ 1 kkayttaj csc 878849 Jan 19 2009 input.table drwxr-xr-x+ 2 kkayttaj csc 2 Dec 22 09:11 project1 -rw-------+ 1 kkayttaj csc 26432051 Dec 22 09:08 results.out -rw-------+ 1 kkayttaj csc 25 Mar 27 2009 sample.data -rw-------+ 1 kkayttaj csc 49 Mar 27 2009 test.txt 

The first output row: total 26914 tells that the total size of the files in the directory is 26914 KB. In the list, the first character tells if the item is a file (-) or a directory(d). The next nine characters display the access permissions of the files (see the chmod command for more details). The next columns show the number of links pointing to the item, owner, user group, size in bytes, modification time and the name of the file or directory.

Читайте также:  Пароль от биоса линукс

By default, files are presented in an alphabetical order. You can order the results by modification time with the option -t or by size with the option -S (note: uppercase S, not lowercase s). Two other frequently used options are -h (Human readable) which prints out the sizes of large files in megabytes or gigabytes, and -r which means reverse sorting order. For example, the command:

is very handy when you want to check what files have recently been modified or created. The ls and pwd commands do not modify files in any way so you can use them always when you want to know where you are and what files your current directory contains.

The command cd directory_name moves you from the current directory to a directory you specified. For example, the user kkayttaj could go to his local temporary directory using the command

In the latter command, the automatically defined environment variable $TMPDIR that contains the explicit directory path is used to define the target directory.

New directories can be created with the command mkdir directory_name. For example, the command:

Creates a new directory called project1. You can use the ls command to check that the directory was created. Now you can go to this directory with the command

You can come back from the project1 directory using the command

Note the space between cd and the dots in the command. One dot ( . ) and two dots ( .. ) have a special meaning in Linux. One dot ( . ) means the current directory, and two dots ( .. ) mean the directory that is one level up in the directory tree, i.e., the directory where the current directory resides. Executing the cd command without any arguments will always move you back to your home directory, regardless of where you are in the directory tree. An empty directory can be removed with the command rmdir directory_name. For example:

Читайте также:  Linux set user rights

Basic commands for files

On a fundamental level, a file in a Linux system is just a string of bytes, were a byte consists of eight bits. So-called text files contain only bytes that can be interpreted as text characters using the ASCII encoding rules. Thus, these files can be considered as consisting of lines of text. In the so-called binary files, also non-ASCII bytes are used, and they cannot and are not intended to be convertible to readable text. Typical examples of binary files are compiled programs, images or compressed files. Normally, users work mostly with text files, and also in the examples of this guide, we normally assume that the files contain some kind of text data: letters or numbers.

Each file has a name. The name can, in principle, be any combination of characters. However, several characters have a special meaning, e.g., ? , * and # , see below and thus, using these characters in filenames may cause problems. We recommend that you only use ordinary letters (lower- or uppercase), numbers, dots ( . ), dashes ( — ) or underscores ( _ ) in file- and directory names. Also, using space characters in filenames may cause problems. We recommend that space characters are replaced by underscores, for example, new_file.txt instead of new file.txt . Note that Linux is case-sensitive: lower- and uppercase characters are not considered equal unlike in Windows computers, and, e.g., the names New_File.txt and new_file.txt refer to different files.

In Linux, the usage of filename extensions such as .doc or .txt is not obligatory. Most Linux tools do not require an extension to be specified. However, in the long run, using systematic naming conventions, including illustrative name extensions, makes file management easier.

Print the content of the specified file of files to the standard output (your screen)

Источник

Оцените статью
Adblock
detector