Sign inSign up

marlabsdocker/lesson2

By marlabsdocker

•Updated almost 9 years ago

Introduction to Linux. Lesson 2. Unix Command Line. Part 2.

Image
0

10K+

marlabsdocker/lesson2 repository overview

+++++++++++++++++++++++++

⁠Change Ownership: chown.

⁠++++++++++++++++++++++++

The chown command allows you to change the ownership of a file to someone else. Only the root user can do this. (Normal users may not give away file ownership or steal ownership from another user.) The syntax of the command is as follows:

[root@fedora-serverA ~]# chown [-R] username filename

where username is the login of the user to whom you want to assign ownership, and filename is the name of the file in question. The filename may be a directory as well. The -R option applies when the specified filename is a directory name. This option tells the command to recursively descend through the directory tree and apply the new ownership, not only to the directory itself, but also to all of the files and directories within it.

NOTE The chown command supports a special syntax that allows you to also specify a group name to assign to a file. The format of the command becomes chown username.groupname filename.

⁠+++++++++++++++++++++++

⁠Change Group: chgrp

⁠+++++++++++++++++++++++

The chgrp command-line utility lets you change the group settings of a file. It works much like chown. Here is the format: [root@fedora-serverA ~]# chgrp [-R] groupname filename where groupname is the name of the group to which you want to assign filename ownership. The filename may be a directory as well. The -R option applies when the specified filename is a directory name. As with chown, the -R option tells the command to recursively descend through the directory tree and apply the new ownership, not only to the directory itself, but also to all of the files and directories within it.

⁠+++++++++++++++++++++++++

⁠Change Mode: chmod

⁠+++++++++++++++++++++++++

Directories and files within the Linux system have permissions associated with them. By default, permissions are set for the owner of the file, the group associated with the file, and everyone else who can access the file (also known as owner, group, and other, respectively). When you list files or directories, you see the permissions in the first column of the output. Permissions are divided into four parts. The first part is represented by the first character of the permission. Normal files have no special value and are represented with a hyphen (-) character. If the file has a special attribute, it is represented by a letter. The two special attributes we are most interested in here are directories (d) and symbolic links (l). The second, third, and fourth parts of a permission are represented in three-character chunks. The first part indicates the file owner’s permission. The second part indicates the group permission. The last part indicates the world permission. In the context of UNIX, “world” means all users in the system, regardless of their group settings. Following are the letters used to represent permissions and their corresponding values. When you combine attributes, you add their values. The chmod command is used to set permission values.

Letter Permission Value R Read 4 W Write 2 X Execute 1

Using the numeric command mode is typically known as the octal permissions, since the value can range from 0–7. To change permissions on a file, you simply add these values together for each permission you want to apply. For example, if you want to make it so that just the user (owner) can have full access (RWX) to a file called foo, you would type [yyang@fedora-serverA ~]$ chmod 700 foo What is important to note is that using the octal mode, you always replace any permissions that were set. So if there was a file in /usr/local that was SetUID and you ran the command chmod -R 700 /usr/local, that file will no longer be SetUID. If you want to change certain bits, you should use the symbolic mode of chmod. This mode turns out to be much easier to remember, and you can add, subtract, or overwrite permissions. The symbolic form of chmod allows you to set the bits of the owner, the group, or others. You can also set the bits for all. For example, if you want to change a file called foobar.sh so that it is executable for the owner, you can run the following command: [yyang@fedora-serverA ~]$ chmod u+x foobar.sh If you want to change the group’s bit to execute also, use the following: [yyang@fedora-serverA ~]$ chmod ug+x foobar.sh If you need to specify different permissions for others, just add a comma and its permission symbols, as here: [yyang@fedora-serverA ~]$ chmod ug+x,o-rwx foobar.sh If you do not want to add or subtract a permission bit, you can use the equal (=) sign instead of a plus (+) sign or minus (-) sign. This will write the specific bits to the file and erase any other bit for that permission. In the previous examples, we used + to add the execute bit to the User and Group fields. If you want only the execute bit, you would replace the + with =. There is also a fourth character you can use: a. This will apply the permission bits to all of the fields. The following list shows the most common combinations of the three permissions. Other combinations, such as -wx, do exist, but they are rarely used. Letter Permission Value --- No permissions 0 r-- Read only 4 Rw- Read and write 6 Rwx Read, write, and execute 7 r-x Read and execute 5 --x Execute only 1

For each file, three of these three-letter chunks are grouped together. The first chunk represents the permissions for the owner of the file, the second chunk represents the permissions for the file’s group, and the last chunk represents the permissions for all users on the system. Permission Numeric Equivalent Description -rw------- 600 Owner has read and write permissions. -rw-r--r-- 644 Owner has read and write permissions; group and world have read-only permission. -rw-rw-rw- 666 Everyone has read and write permissions. Not recommended;this combination allows the file to be accessed and changed by anyone. -rwx------ 700 Owner has read, write, and execute permissions. Best combination for programs or executables that the owner wishes to run. -rwxr-xr-x 755 Owner has read, write, and execute permissions. Everyone else has read and execute permissions. -rwxrwxrwx 777 Everyone has read, write, and execute permissions. Like the 666 setting, this combination should be avoided. -rwx--x--x 711 Owner has read, write, and execute permissions; everyone else has executeonly permissions. Useful for programs that you want to let others run but not copy.

⁠FILE MANAGEMENT AND MANIPULATION.

This section covers the basic command-line tools for managing files and directories. Most of this will be familiar to anyone who has used a command-line interface—same old functions, but new commands to execute.

⁠+++++++++++++++++++++++

⁠Copy Files: cp

⁠+++++++++++++++++++++++

The cp command is used to copy files. It has a substantial number of options. See its man page for additional details. By default, this command works silently, only displaying status information if an error condition occurs. Following are the most common options for cp: Option for cp Description -f Forces copy; does not ask for verification -I Interactive copy; before each file is copied, verifies with user the mkdir command. Only the owner can read and write to this directory.

First, let’s use the touch command to create an empty file called foo.txt in the user yyang’s home directory. Type [yyang@fedora-serverA ~]$ touch foo.txt To use the cp (copy) command to copy foo.txt to foo.txt.html, type [yyang@fedora-serverA ~]$ cp foo.txt foo.txt.html To interactively copy all files ending in .html to the /tmp directory, type this command: [yyang@fedora-serverA ~]$ cp -i *.html /tmp

⁠++++++++++++++++++++

⁠Move Files: mv

⁠++++++++++++++

The mv command is used to move files from one location to another. Files can be moved across partitions/file systems as well. Moving files across partitions involves a copy operation, and as a result, the move command may take longer. But you will find that moving files within the same file system is almost instantaneous. Following are the most common options for mv: Option for mv Description -f Forces move -I Interactive move To move a file named foo.txt.html from /tmp to your present working directory, use this command: [yyang@fedora-serverA ~]$ mv /tmp/foo.txt.html . NOTE That last dot (.) is not a typo—it literarily means “this directory.” There is no explicit rename tool, so you can use the mv command. To rename the file foo.txt.html to foo.txt.htm, type [yyang@fedora-serverA ~]$ mv foo.txt.html foo.txt.htm

⁠+++++++++++++++++

⁠+++++++++++++++++

The ln command lets you establish hard links and soft links (see “Files, File Types, File Ownership, and File Permissions” earlier in this chapter). The general format of ln is as follows: [yyang@fedora-serverA ~]$ ln original_file new_file Although ln has many options, you’ll rarely need to use most of them. The most common option, -s, creates a symbolic link instead of a hard link. To create a symbolic link called link-to-foo.txt that points to the original file called foo.txt, issue the command [yyang@fedora-serverA ~]$ ln -s foo.txt link-to-foo.txt

⁠+++++++++++++++++++

⁠Find a File: find

⁠+++++++++++++++++++

The find command lets you search for files according to various criteria. Like the tools we have already discussed, find has a large number of options that you can read about in its man page. Here is the general format of find: [yyang@fedora-serverA ~]$ find start_dir [options] where start_dir is the directory from which the search should start. To find all files in the current directory (i.e., the “.” directory) that have not been accessed in at least seven days, use the following command: [yyang@fedora-serverA ~]$ find . -atime 7 Type this command to find all files in your present working directory whose names are core and then delete them (i.e., automatically run the rm command): [yyang@fedora-serverA ~]$ find . -name core -exec rm {} ; TIP The syntax for the -exec option with the find command as used here can be hard to remember sometimes, and so you can also use the xargs method instead of the exec option used in this example. Using xargs, the command would then be written [yyang@fedora-serverA ~]$ find . -name 'core' | xargs rm To find all files in your PWD whose names end in .txt (i.e., files that have the .txt extension) and are also less than 100 kilobytes (K) in size, issue this command: [yyang@fedora-serverA ~]$ find . -name '.txt' -size -100k To find all files in your PWD whose names end in .txt (i.e., files that have the .txt extension) and are also greater than 100K in size, issue this command: [yyang@fedora-serverA ~]$ find . -name '.txt' -size 100k

⁠+++++++++++++++++++

⁠File Compression: gzip

⁠+++++++++++++++++++

In the original distributions of UNIX, the tool to compress files was appropriately called compress. Unfortunately, the algorithm was patented by someone hoping to make a great deal of money. Instead of paying out, most sites sought and found another compression tool with a patent-free algorithm: gzip. Even better, gzip consistently achieves better compression ratios than compress does. Another bonus: Recent changes have allowed gzip to uncompress files that were compressed using the compress command. NOTE The filename extension usually identifies a file compressed with gzip. These files typically end in .gz (files compressed with compress end in .z). Note that gzip compresses the file in place, meaning that after the compression process, the original file is removed, and the only thing left is the compressed file. To compress a file named foo.txt.htm in your PWD, type [yyang@fedora-serverA ~]$ gzip foo.txt.htm And then to decompress it, use gzip again with the -d option: [yyang@fedora-serverA ~]$ gzip -d foo.txt.htm.gz Issue this command to compress all files ending in .htm in your PWD using the best compression possible: [yyang@fedora-serverA ~]$ gzip -9 *.htm

⁠++++++++++++++

⁠bzip2 Command

⁠++++++++++++++

If you have noticed files with a .bz extension, these have been compressed with the bzip2 compression utility. The bzip2 tool uses a different compression algorithm that usually turns out smaller files than those compressed with the gzip utility, but it uses semantics that are similar to gzip; for more information, read the man page on bzip2.

⁠++++++++++++++++++++

⁠Create a Directory: mkdir

⁠++++++++++++++++++++

The mkdir command in Linux is identical to the same command in other flavors of UNIX, as well as in MS-DOS. An often-used option of the mkdir command is the -p option. This option will force mkdir to create parent directories if they don’t exist already. For example, if you need to create /tmp/bigdir/subdir/mydir and the only directory that exists is /tmp, using -p will cause bigdir and subdir to be automatically created along with mydir. Create a directory tree like bigdir/subdir/finaldir in your PWD. Type [yyang@fedora-serverA ~]$ mkdir -p bigdir/subdir/finaldir To create a single directory called mydir, use this command: [yyang@fedora-serverA ~]$ mkdir mydir

⁠++++++++++++++++++++

⁠Remove a Directory: rmdir

⁠++++++++++++++++++++

The rmdir command offers no surprises for those familiar with the DOS version of the command; it simply removes an existing directory. This command also accepts the -p parameter, which removes parent directories as well. For example, if you want to get rid of all the directories from bigdir to finaldir that were created earlier, you’d issue this command alone: [yyang@fedora-serverA ~]$ rmdir -p bigdir/subdir/finaldir To remove a directory called mydir, you’d type this: [yyang@fedora-serverA ~]$ rmdir mydir

⁠+++++++++++++++++++++++++++++++++

⁠Display a File One Screen at a Time: more

⁠+++++++++++++++++++++++++++++++++

The more command works in much the same way the DOS version of the program does. It takes an input file and displays it one screen at a time. The input file can come either from its stdin or from a command-line parameter. Additional command-line parameters, though rarely used, can be found in the man page. To view the /etc/passwd file one screen at a time, use this command: [yyang@fedora-serverA ~]$ more /etc/passwd To view the directory listing generated by the ls command one screen at a time, enter [yyang@fedora-serverA ~]$ ls | more

Tag summary

Content type

Image

Digest

Size

91 MB

Last updated

almost 9 years ago

docker pull marlabsdocker/lesson2