Last updated -


Sydney Linux Tutorial



Navigation :.
:: Home :: Linux Distribution :: Linux Installation :: Linux Commands :: Linux File System :: Linux Bash Shell :: Linux Administration :: Linux KDE :: Linux VMWare Contact Us


We recommend :.




Useful resources :.

Free Linux Tutorial

Free Linux tutorial Sydney-Linux.com provides comprehensive information about Linux installation, using and administration. Here you can find answers for all your Linux questions!


Linux commands tutorial

Linux commands list

Linux command guide for working with files and directory

In the previous sections we have already mentioned some commands for working with files and directories: pwd, cd, ls, ln, chmod. In this section we consider the (very briefly) a few frequently used commands. Every true Linux user should be able to work with Linux command line and keep in mind his own Linux commands cheat sheet. Here you can find Linux command reference, prepared by Sydney-Linux experts. These Linux commands for beginners and advanced users you have to know!

Well, here we go, the most useful commands in Linux!

Teams chown and chgrp

These commands are used to change the file owner and group file. Perform the change of ownership can only root, change the group could do alone, the file owner or root. In order to have the right to change the group owner must additionally be a member of the group, which he wants to give rights to this file. The format of these two teams is similar:

[root] # chown vasja file-name

[root] # chgrp usersgrp file-name

Team mkdir

Team mkdir to create a subdirectory in the current directory. The argument This command should be given a name to the directory. In the newly created directory automatically created two entries: . (link to this is the directory) and .. (link to the parent directory). That create a subdirectory, you should have in the current directory write permissions. You can not create a subdirectory in the current, and in some other directory, but then you need to specify the path to create directories:

Related links:

[user] $ mkdir / home/kos/book/glava5/part1

Team mkdir can be used with the following options:

  • -m mode - sets the mode of access for new directory (for example, -m 755 ) ;

  • -p - create these intermediate directories (if they do not exist).

Team cat

Team cat is often used to create files (although you can use and the team touch ). On command cat to standard output (ie the screen) displays the contents specified file (or multiple files if their names consistently specify as arguments to commands). If the output Team cat redirected to a file, you can get a copy of a file:

[user] $ cat file1> file2

Actually, the original purpose of the team cat just and implied output redirection, because this team created for the concatenation, ie, combining multiple files into one:

Related links:

[user] $ cat file1 file2 ... fileN> new-file

It is possible

redirect the input and output This team and used to create new files. For this purpose input commands cat sending data from standard input (ie keyboard), and output - a new file:

[user] $ cat> newfile

After you type whatever you want, press the key combination < Ctrl > + or < Ctrl > + , and all that you have entered will be recorded in newfile . Of course, in this way are mostly short text Files.

Team cp

While copying files sometimes use team cat , but in Linux there for this special team cp . It can be applied in one of the two forms:

[user] $ cp [options] source destination

[user] $ cp [options] source_directory new_directory

Related links:

In the first case file or directory source copied, respectively, in a file or directory destination, but in the second case, the files contained in the catalog source_directory copied to the directory new_directory. To copy must have the right read files that are copied, and write permissions to the directory which is copied.

If the target indicates existing file, its contents will be erased, so if you copy must caution. However, you can use the command cp with option-i, then before overwriting an existing file will be asked for confirmation (I highly recommend you always use this option !).

The team cp There are several other useful options (Table 4.3).

Table 4.3. The main options command cd.

Option

value

-p

Stores file modification time and the maximum possible power. Without This option for the new file specified powers, consistent authority, click Users

-R or -r

If source - directory, then copied it, and all its constituent sub-directories, ie, retains its original shape directory tree

-d

If specify this option, symbolic links will remain links (otherwise, instead of referring the file is copied, which gives reference)

-f

Overwrite when copying files (if they already have) without additional Warnings

[user] $ cat file1 file2 ... fileN> new-file

It is possible

redirect the input and output This team and used to create new files. For this purpose input commands cat sending data from standard input (ie keyboard), and output - a new file:

[user] $ cat> newfile

After you type whatever you want, press the key combination < Ctrl > + or < Ctrl > + , and all that you have entered will be recorded in newfile . Of course, in this way are mostly short text Files.

Team cp

While copying files sometimes use team cat , but in Linux there for this special team cp . It can be applied in one of the two forms:

[user] $ cp [options] source destination

[user] $ cp [options] source_directory new_directory

In the first case file or directory source copied, respectively, in a file or directory destination, but in the second case, the files contained in the catalog source_directory copied to the directory new_directory. To copy must have the right read files that are copied, and write permissions to the directory which is copied.

If the target indicates existing file, its contents will be erased, so if you copy must caution. However, you can use the command cp with option-i, then before overwriting an existing file will be asked for confirmation (I highly recommend you always use this option !).

The team cp There are several other useful options (Table 4.3).

Table 4.3. The main options command cd.

Option

value

-p

Stores file modification time and the maximum possible power. Without This option for the new file specified powers, consistent authority, click Users

-R or -r

If source - directory, then copied it, and all its constituent sub-directories, ie, retains its original shape directory tree

-d

If specify this option, symbolic links will remain links (otherwise, instead of referring the file is copied, which gives reference)

-f

Overwrite when copying files (if they already have) without additional Warnings

4.6.5. Team mv

If you do not need to copy and move file from one directory to another, you can use the command mv . The syntax of this command is similar to the command syntax cp . Moreover, it first copies the file (or directory), and only then deletes the original file (directory). And the options it has are the same as in cp .

Team mv can be used not only to move, but also for renaming files and directories (ie, move them inside one directory). To do this, simply ask as arguments old and the new file name:

[user] $ mv oldname newname

But keep in mind that the team mv does not allow to rename multiple files (using the template name), so that the team mv *. Xxx *. yyy will not work.

When you use mv , well as using cp , Do not forget to use the option-i to get warning when the file will be overwritten.

4.6.6. Teams rm and rmdir

To remove unnecessary files and directories in Linux serve team rm (Deletes files) and rmdir (Deletes an empty directory) . To use these commands, you must have write permission to the directory that are removed files or directories. This power to modify the files themselves are not required. If you want to receive before deleting the file additional request to confirm the operation, use the -I.

If you try to use the command rm (Without options) to delete the directory, you will receive a message that it is a directory, and delete does not happen. To delete the directory should delete all files in it, then delete the directory using Team rmdir . However, you can delete non empty directory with all its member subdirectories and files if you use the command rm with the option-r.

If you give the command rm * , then delete all files in the current directory. Subdirectories thus not be removed. To delete both files and subdirectories current directory must also have the option -r . However, always remember that no team in the Linux File Recovery after removing them (even if you woke up immediately after incorrect removal of a file or directory)!

So think twice before deleting anything, and do not neglect the option -i .

Teams more and less

Team cat allows you to standard output (screen) the contents of any file, but it is used for these purposes is very rare, except to output for very small files. Fact is that contents of a large file quickly skips to the screen, and user sees only the last line in the file. Therefore cat mainly used for its intended purpose - to concatenate files and to view the contents of files (of course, text) used commands more and less (Or text editors).

Team-filter more displays the contents of the file on the screen for individual pages, size as once in the whole screen. To see the next page, you must press the spacebar. Pressing < Enter > leads to a shift in one line. In addition to the space key and < Enter > While paused, still some keys act as managers (Eg, key returns you Scroll backwards one page), but we shall not give their full list as the list of options the team. You need to start yet remember that exit the viewer can use the keys < Q > , because if you do not know, then you have a long and tedious press the space bar until you reach the end of a long file. About all the options the team more You can read the online manual, man or info.

Utility less , developed within the project GNU, contains all the functions and commands output control, available in the program more , and some additional, for example, allows the use of keys cursor control (, , , ) To navigate the text. Remember, we already talked about Thus, when we looked at interactive prompt man .

Teams more and less allows to search a string in file view, and Team less allows you to search both forward and in reverse direction. To organize the search character string string must type in a command line program in the bottom of the screen (where colon) / string. If the search string is found, will displays the corresponding piece of text, and found rows will at the top of the screen.

Team find and wildcard characters to filename

Another frequently used commands for files in Linux is a team searching for the desired file find . Team find can search for files by name, size, date created or modified and other criteria.

General syntax find has the following form:

find [Spisok_katalogov] search_criterion

Parameter "spisok_katalogov" defines where to find the desired file. The easiest way to set as the initial directory search / directory, but , In this case, the search could take a very long time, because it will viewed the entire directory structure, including mounted file systems (including the network, if any). Can reduce the search, if we ask instead of a single root a list of several directories (of course, those which can be corrupted file):

[user] $ find / usr / share / doc / usr / doc / usr / locale / doc-name instr.txt

Beginning "kriteriya_poiska" determine what the program should look find , be the first argument, beginning with "-", "(", ")"', "," Or "!". The arguments previous "kriteriyu_poiska" are treated as names directories, which should make a search. If no One way, search is made only in the current directory and its subdirectories.

Most of the search is conducted by file names, like shown in the previous example, ie, "Search_criterion" is given as "-name filename. Instead of option-name can be use the option-path, then the command will look for matches in full file name with the path. For example, the command

[user] $ find. -Path '. / Sr * sc'

Find in the current directory, a subdirectory '. / Src / misc'. Instead of the full name of the file or directory in this example is used so-called "template name". Since wildcards Files can be used not only team find , but also with many other teams (including the already discussed the team chmod, chown, chgrp, cp, rm, cat, mv ), then the rules for Templates should be given some attention.

Most common wildcards are constructed using special character "*" and "?". Button "*" used to replace arbitrary strings of characters. In Linux

  • "*" - Meets all the files for Except hidden;

  • ".*" - Meets all the hidden files (But also the current directory ". " and directory level above " .. ": do not forget about it!)

  • "*.*" - Corresponds to only those files and directories that have ". " in the middle of the name or terminate at the point;

  • "P * r" - corresponds to the "peter" and the "piper";

  • "* C *" - corresponds to the "picked" and "peck".

Icon ? replaces an arbitrary character, so the index?. htm will match the names index0.htm, index5.htm and indexa.htm.

In "*" and "?" In the Linux job template names you can use brackets [] in which gives a list of possible characters, or the interval in who should get the possible characters. For example, [abc] * meets all the file names beginning with a, b, c; * [I-N1-3] corresponds to the files whose names end in I, J, K, L, M, N, 1, 2, 3.

Now back to the team find and tell more about what the search criteria are possible. Some examples of simple search criteria are listed in Table. 4.4.

Table 4.4. Search Criteria for team find.

Option

value

-name Template

Searches files whose names match the pattern

-group name

Searches files belonging to the specified group

-size Number [c]

Searches files, the size of number 512-byte blocks. If the number is the symbol c, then the size specified in bytes (characters)

-mtime Number

Searches files that were last changed specified number days ago

-newer Sample

Searches Files which were changed after changing the file specified in sample

-type Filetype

Searches Files of this type. Type specified by one of the symbols b (Block-oriented devices), c (Byte-oriented devices), d (File directory), f (Regular file), p (Named pipe) or l (Symlink)

Other simple criteria you can find out if check man-page on the team find . Here, just have to say that the simple criteria can be built more complex with the help of logical operations and , or or a negation operation, a sign which serves as an exclamation point. For example, if you want to find all files whose names end with at. txt and. doc, then the criterion can be written as (-name *. Txt-or-name *. doc) . You can combine these read any number of criteria (and not just simple!). If the operation not explicitly specified, it is assumed -and , ie instead (-name *. Txt-and-name *. doc) can be written simply (- name *. Txt-name *. doc) . If you use only one operation -and or ! , then you can usually omit the brackets, but with the operation -or and in complex expressions, parentheses are required. Before parentheses put a backslash, and after the brackets - a space. For example, if you want to find a directory on his behalf, it can be a team

[user] $ find / usr-name doc-type d

Or (subject to the rules of constructing complex criteria)

[user] $ find / usr \ (-name doc-and-type d \)

In the following example we are looking for files on such criteria: either the file name ends in *. tmp, or the file size more than 100 KB.

[user] $ find / home / kos \ (\ (-name *. tmp \)-or \ (-size +200 \) \)

In the last example is worth paying attention to the fact that before the value of size is "+" sign. Such sign can be used with any numeric parameter in the search criteria Team find . It means that you need to search for files that have value more defined. Accordingly, the sign " - " means, what to look for files that have less than a given value. If the signs + or - is missing, searched the files that are Data value is given.

To complete the review team find , I must say more about that after a search criterion in this team can immediately ask the operation to be applied to all files found on your criteria. Simplest example the use of such opportunities is an indication of the team -print .

[user] $ find / home / kos-name *. tmp-print

On which display a list of names of all found files showing the full path to the file. This operation the default, ie, when no operations no pointed out (as it was in all the examples above). Another example of the operation that applies to all found files, can serve the operation -exec cmd () \; , where cmd - arbitrary shell command shell. That is, in this case, all found the files (their names are replaced alternately braces) apply command cmd . For cmd () in this case should be followed by a semicolon, Shielded by a backslash.

For example, if you want to delete this catalog all the files to which users have accessed within 30 days, give the command:

[root] # find. -Type f-atime +30-exec rm () \;

Instead

-exec can be put-ok, then before implementation of the command cmd for each file will asked for confirmation.

In general, the team find is very powerful, useful and highly adaptable tool search the file system. All its features are not lists, study relevant man-page. And be very careful with the use of such opportunities the team as a challenge to other commands that apply to all found files. Remember that changes often irreversible!

4.6.9. Team split - split file into several parts

Sometimes it is necessary to split one big file several smaller files. For example, consider the situation When you want to move to your home computer file song.mp3 format of "MP3", the size of 4,894,425 bytes. Capabilities to pump this file on a network you do not, and the only possible transfer method - use floppies. But since on a disk file does not fit, you want to split it into several small files, and then "build" again. For solve this problem you can use the command split .

Team split copies the file, breaking it into separate files of a given length. In As the arguments she has to specify the source file name and prefix names of output files. The names of output files will be compiled from the prefix and two additional letters `aa ' , `ab ' , `ac ' etc. (no spaces or dots between the prefix and letters). If the prefix file name is not specified, the default used 'x', so the output files are named ` xaa ' , ` xab ' , etc. .

In arguments, you can specify the option-b, determining the size of the output file in bytes. After-b must be a number, and then - the letter k (showing that output file is specified in kilobytes) or m (mean size specified in megabytes). If this option is not specified, the default size output file is assumed to be 1 MB. Thus, to transfer to diskettes file song.mp3 must first give the command

[user] $ split-b1400k song.mp3 song.

Copy the files song.aa, song.ab, song.ac, song.ad, song.ae on separate floppy disks, to transfer them to home computer, copied to some directory and restore source file with the command

[user] $ cat song .*> song.mp3

Then you can delete temporary files song.xx.

4.6.10. Compare files and the command patch

You ever noticed that the task of comparing the contents two different files occurs when you use your computer surprising often? Of course, it is so easy to copy the file, and then forget which of the newer versions or better quality (according to one author wingman criteria). So the tools to compare files just necessary and Linux tools such offers.

Simplest of them - the team cmp . This team simply compares the contents of two files-byte:

[user] $ cmp file1 file2

If the files are completely identical, it silently finishes its work (there is a return to the command line without any additional messages), but if the files differ, given line number and the number of bytes in a row, where there is a first difference.

Course, the information issued by the team cmp , not much to take, for example, the decision on which of two files to be more valuable. Therefore, should use the command diff for a full report about what are the differences in interest our files. To receive a report sufficient to indicate the team, which namely to compare files:

[user] $ diff paper.old paper.new

Report identified differences will be issued on standard output. Naturally, it is better to redirect to a file:

[user] $ diff paper.old paper.new> paper.diff

To estimate versions of the same file more convenient it may be the team sdiff , which gives the result of comparison in the form of two columns, separated by spaces. If the strings with the same numbers in the files differ, then in the output sdiff they are separated by a vertical bar | . If the line is only the first file, she was awarded with < . Accordingly, the string, which is found only in the second file, as designated > .

There is also a team diff3 , which allows you to compare 3 files at once.

But still the most commonly used traditional for UNIX-based systems team diff . This demand for it because it creates a report about differences between two files can be used by a team patch . Most of these features are used in the propagation software updates. Suppose that a software application was sent to users as a file program.c, containing the source code in C language. After This developer has made some corrections in the program and kept text in a file program.c.new. Required to bring the revised program text to users. Obviously, users enough to give only corrections, ie, report changes generated by the command

[user] $ diff program.c program.c.new> program.c.diff

Naturally, file size program.c.diff substantially less than the file program.c.new, so that could be obtain substantial savings on the transfer of files when sending users only file program.c.diff (because the volume of modern software applications are tens of megabytes). But users should be able to make these corrections in available to them version. This task allows you to decide the team patch . With files program.c and program.c.diff , user can give the command

[user] $ patch program.c program.c.diff> program.c.new

As a result of which he will file program.c.new.

Read more:

Copyright 2004-2010 Sydney-Linux.com