OsGate.org Logo

Linux basic commands, part 2: files and directories - linux commands files directories

System System

Date 18.02.2011

Visits 2654

"The first time that we use a linux system it's worth to know some basic commands in order to begin to use the command line. These commands can include for example operations on files and directories and in this article we will cover this topic."

Introduction

In this second article (the first is here) we will take a look on others commands useful when working with file and directories.

File and directories

chattr - change file attributes on a Linux file system

 

You can use "chattr" to change the attributes on a given file. Attributes are special system flags that can be applied to files in order to obtain a particular result. For example the "i" flag makes a file immutable. Read below for others attributes.

chattr [options] (mode)(attributes) file1 [file2] [...]

The "mode" are:

  • "+" for add attributes
  • "-" for delete attributes
  • "=" for set the attribute we want and delete the others that are already set up.

The attributes are (description taken from the man page of chattr):

"The letters 'acdeijstuADST'select the new attributes for the files:
append only (a), compressed (c), no dump (d), extent format (e),
immutable (i), data journalling (j), secure deletion (s), no tail-merg-
ing (t), undeletable (u), no atime updates (A), synchronous directory
updates (D), synchronous updates (S), and top of directory hierarchy
(T).
The following attributes are read-only, and may be listed by lsattr(1)
but not modified by chattr: huge file (h), compression error (E),
indexed directory (I), compression raw access (X), and compressed dirty
file (Z)."

Read the man page if you want to know more about the attributes.

 

Some examples:

#add the undeletable attribute
chattr +u song.mp3

#remove the undeletable attribute
chattr -u song.mp3

#reset the attributes to ucj
chattr =ucj file.txt

Note that some attributes can only be set by root user, like the immutable "i".

 

Useful options:

  • -R, Recursively change attributes of directories and their contents
  • -V, Be verbose with chattr's output and print the program version.
  • -f, Suppress most error messages.

chgrp - change group ownership

For change the group ownership of files you can use ¿chgrp¿.

chgrp [options] group file1 [file2] [...]

A small example can be:

chgrp -R www /var/www/htdocs

This will change recursively the group to "www" in the folder "/var/www/htdocs".

 

Useful options:

  • -h, affect each symbolic link instead of any referenced file (useful only on systems that can change the ownership of a symlink)
  • -R, operate on files and directories recursively

chmod - change file mode bits

To change the permissions on a file use "chmod".

chmod [options] [mode] file1 [file2] [...]

The "[mode]" field can be used in two ways, using the symbolic or the octal notation.

If you use the first you must specify permissions using letters and symbols.

 

Usually this is the way to change permissions with symbolic notation:

chmod ug+rw file

You can see a first set of parameters on the left side of the "+" sing, these represent the permissions owners. The second parameter is the mode and on the right side you have the permissions the file will have.

These are the possible parameters:

Permissions owners Modes Permissions
u, user +, adds permissions r, read
g, group -, deletes permissions x, execute
o, others =, sets only the specified permissions and deletes the others w, write
a, all   X, execute only if the file is a directory
    s, set user or group id on execution
    t, restricted deletion flag or sticky

Example with the symbolic mode:

bash-4.1# chmod ugo+r file
bash-4.1# chmod g+rw file
bash-4.1# chmod o+t file

The other way to set permission is the "octal mode". In this mode the permissions are represented using the octal notation, so using the powers 20, 21 and 22.

 

Each permission has a value:

Power Value Permission
22 4 read
21 2 write
20 1 execute

 

Now you can sum the values in order to set the right permissions.

So for example for a read and execute you will have 5 and for read, execute and write the sum is 7.

 

Octal notation example:

bash-4.1# chmod 777 file
bash-4.1# chmod 501 file
bash-4.1# chmod 644 file

If we look at the first example, you can see the 777, this means set all the permissions(rwx) for the user (1st seven), for the group (2nd seven) and for the rest of the world (3rd seven).

Permissions on directories

The normal permissions applied to a file don't work in the same manner if applied to directories:

  • read: if the read attribute is set, it's possible to obtain the list of file of a directory.
  • write: It's possible to write inside the directory
  • execute: It's possible to change in the directory and operate with its contents

If you set only the read or execute right, the user cannot operate with the directory. For example if you only set the execute right, the user can work with the directories (create file, cd, ecc..) but is still forbidden for he to obtain the list of file. Instead, if you set only the read attribute, the user is able to obtain the list of file but cannot work with the directory. So it's important to set both read and execute permissions on a folder if you want a normal behavior.

 

Useful options:

  • -R, change files and directories recursively

chown - change file owner and group

We've seen how to change group to a file, now we're going to see how to change user. Note that with ¿chown¿ you can also change the group.

The behavior of this command is quite the same of "chgrp":

chown [options] owner[.group] file1 [file2] [...]

Example:

chown -R foo.users /home/foo/documents/

You can see that "chown" is able to change the username (foo) along with the group (users) if a point is placed between the two parameters (foo.users).

 

Useful options:

  • -R, operate on files and directories recursively

du - estimate file space usage

"Du" it's a command used for check the space occupied by a file or a folder.

du [options] file1 [file2] [...]

Without options, "du" will visualize the file size in bytes along the file name. If you want for example know the size of a directory the best way is to add the "-h" (human readable) and "-s" (summarize) options. These will print the total size of the directory along its name.

 

Useful options:

  • -a, write counts for all files, not just directories
  • -c, produce a grand total
  • -h, print sizes in human readable format (e.g., 1K 234M 2G)
  • -s, display only a total for each argument
  • --exclude=PATTERN, exclude files that match PATTERN

lsattr - list file attributes on a Linux second extended file system

When we set the attributes on a file, there is also the need to see what kind of attributes are already set on that file. To do this you can use "lsattr" that acts like "ls" but it shows only the attribute set.

lsattr [options] file1 [file2] [...]

Example:

lsattr test
-u------------- test

Useful options:

  • -R, Recursively list attributes of directories and their contents.
  • -a, List all files in directories, including files that start with '.'.
  • -d, List directories like other files, rather than listing their contents.

stat - display file or file system status

"Stat" can be used to obtain useful information about a file stored in the file system.

stat [options] file1 [file2] [...]

It can give you some useful informations, like the modification, access and change time. Others information can be the inode or the various permissions.

 

Example:

bash-4.1$ stat test
File: `test'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 772365 Links: 2
Access: (0003/d-------wx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2011-02-14 20:24:00.000000000 +0100
Modify: 2011-02-14 17:24:34.000000000 +0100
Change: 2011-02-14 17:24:34.000000000 +0100
Birth: -

The option "-f" change the information you will obtain from the normal "stat", in facts you'll get the file system status instead of the file status. So you can obtain the information about the file system the file is stored on.

 

Useful options:

  • -f, display file system status instead of file status
  • -c FORMAT, use the specified FORMAT instead of the default; output a new-line after each use of FORMAT (see the man page of stat for a list of possible formats)

touch - change file timestamps

The "touch" command is often used when there is the need to change the timestamps on a given file.

The timestamps can be visualized thanks to the "stat" command and these are the modification, access and change time. Touch will change only the modification and access time, updating them to the current time.

touch [options] file1 [file2] [...]

If "touch" is not able to find the file, this will be created automatically unless the "-c" is specified.

 

Useful options:

  • -a, change only the access time
  • -c, do not create any files
  • -d STRING, parse STRING and use it instead of current time
  • -m, change only the modification time