Home Dokumentacje GNU Coreutils - manual - 18. File name manipulation
06 | 12 | 2019
GNU Coreutils - manual - 18. File name manipulation Drukuj

[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

18. File name manipulation

This section describes commands that manipulate file names.


[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

18.1 basename: Strip directory and suffix from a file name

basename removes any leading directory components from name. Synopsis:

basename name [suffix]

If suffix is specified and is identical to the end of name, it is removed from name as well. Note that since trailing slashes are removed prior to suffix matching, suffix will do nothing if it contains slashes. basename prints the result on standard output.

Together, basename and dirname are designed such that if `ls "$name"' succeeds, then the command sequence `cd "$(dirname "$name")"; ls "$(basename "$name")"' will, too. This works for everything except file names containing a trailing newline.

POSIX allows the implementation to define the results if name is empty or `//'. In the former case, GNU basename returns the empty string. In the latter case, the result is `//' on platforms where // is distinct from /, and `/' on platforms where there is no difference.

The only options are `--help' and `--version'. See section Common options. Options must precede operands.

An exit status of zero indicates success, and a nonzero value indicates failure.

Examples:

# Output "sort".
basename /usr/bin/sort

# Output "stdio".
basename include/stdio.h .h

[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

18.2 dirname: Strip last file name component

dirname prints all but the final slash-delimited component of name. Slashes on either side of the final component are also removed. If the string contains no slash, dirname prints `.' (meaning the current directory). Synopsis:

dirname name

name need not be a file name, but if it is, this operation effectively lists the directory that contains the final component, including the case when the final component is itself a directory.

Together, basename and dirname are designed such that if `ls "$name"' succeeds, then the command sequence `cd "$(dirname "$name")"; ls "$(basename "$name")"' will, too. This works for everything except file names containing a trailing newline.

POSIX allows the implementation to define the results if name is `//'. With GNU dirname, the result is `//' on platforms where // is distinct from /, and `/' on platforms where there is no difference.

The only options are `--help' and `--version'. See section Common options.

An exit status of zero indicates success, and a nonzero value indicates failure.

Examples:

# Output "/usr/bin".
dirname /usr/bin/sort
dirname /usr/bin//.//

# Output ".".
dirname stdio.h

[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

18.3 pathchk: Check file name validity and portability

pathchk checks validity and portability of file names. Synopsis:

pathchk [option]… name

For each name, pathchk prints an error message if any of these conditions is true:

  1. One of the existing directories in name does not have search (execute) permission,
  2. The length of name is larger than the maximum supported by the operating system.
  3. The length of one component of name is longer than its file system's maximum.

A nonexistent name is not an error, so long a file with that name could be created under the above conditions.

The program accepts the following options. Also see Common options. Options must precede operands.

`-p'

Instead of performing checks based on the underlying file system, print an error message if any of these conditions is true:

  1. A file name is empty.
  2. A file name contains a character outside the POSIX portable file name character set, namely, the ASCII letters and digits, `.', `_', `-', and `/'.
  3. The length of a file name or one of its components exceeds the POSIX minimum limits for portability.
`-P'

Print an error message if a file name is empty, or if it contains a component that begins with `-'.

`--portability'

Print an error message if a file name is not portable to all POSIX hosts. This option is equivalent to `-p -P'.

Exit status:

0 if all specified file names passed all checks,
1 otherwise.

[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

18.4 mktemp: Create temporary file or directory

mktemp manages the creation of temporary files and directories. Synopsis:

mktemp [option]… [template]

Safely create a temporary file or directory based on template, and print its name. If given, template must include at least three consecutive `X's in the last component. If omitted, the template `tmp.XXXXXXXXXX' is used, and option `--tmpdir' is implied. The final run of `X's in the template will be replaced by alpha-numeric characters; thus, on a case-sensitive file system, and with a template including a run of n instances of `X', there are `62**n' potential file names.

Older scripts used to create temporary files by simply joining the name of the program with the process id (`$$') as a suffix. However, that naming scheme is easily predictable, and suffers from a race condition where the attacker can create an appropriately named symbolic link, such that when the script then opens a handle to what it thought was an unused file, it is instead modifying an existing file. Using the same scheme to create a directory is slightly safer, since the mkdir will fail if the target already exists, but it is still inferior because it allows for denial of service attacks. Therefore, modern scripts should use the mktemp command to guarantee that the generated name will be unpredictable, and that knowledge of the temporary file name implies that the file was created by the current script and cannot be modified by other users.

When creating a file, the resulting file has read and write permissions for the current user, but no permissions for the group or others; these permissions are reduced if the current umask is more restrictive.

Here are some examples (although note that if you repeat them, you will most likely get different file names):

  • Create a temporary file in the current directory.
    $ mktemp file.XXXX
    file.H47c
    
  • Create a temporary file with a known suffix.
    $ mktemp --suffix=.txt file-XXXX
    file-H08W.txt
    $ mktemp file-XXXX-XXXX.txt
    file-XXXX-eI9L.txt
    
  • Create a secure fifo relative to the user's choice of TMPDIR, but falling back to the current directory rather than `/tmp'. Note that mktemp does not create fifos, but can create a secure directory in which the fifo can live. Exit the shell if the directory or fifo could not be created.
    $ dir=$(mktemp -p "${TMPDIR:-.}" -d dir-XXXX) || exit 1
    $ fifo=$dir/fifo
    $ mkfifo "$fifo" || { rmdir "$dir"; exit 1; }
    
    
  • Create and use a temporary file if possible, but ignore failure. The file will reside in the directory named by TMPDIR, if specified, or else in `/tmp'.
    $ file=$(mktemp -q) && {
    >   # Safe to use $file only within this block.  Use quotes,
    >   # since $TMPDIR, and thus $file, may contain whitespace.
    >   echo ... > "$file"
    
    >   rm "$file"
    > }
    
  • Act as a semi-random character generator (it is not fully random, since it is impacted by the contents of the current directory). To avoid security holes, do not use the resulting names to create a file.
    $ mktemp -u XXX
    Gb9
    $ mktemp -u XXX
    nzC
    

The program accepts the following options. Also see Common options.

`-d'
`--directory'

Create a directory rather than a file. The directory will have read, write, and search permissions for the current user, but no permissions for the group or others; these permissions are reduced if the current umask is more restrictive.

`-q'
`--quiet'

Suppress diagnostics about failure to create a file or directory. The exit status will still reflect whether a file was created.

`-u'
`--dry-run'

Generate a temporary name that does not name an existing file, without changing the file system contents. Using the output of this command to create a new file is inherently unsafe, as there is a window of time between generating the name and using it where another process can create an object by the same name.

`-p dir'
`--tmpdir[=dir]'

Treat template relative to the directory dir. If dir is not specified (only possible with the long option `--tmpdir') or is the empty string, use the value of TMPDIR if available, otherwise use `/tmp'. If this is specified, template must not be absolute. However, template can still contain slashes, although intermediate directories must already exist.

`--suffix=suffix'

Append suffix to the template. suffix must not contain slash. If `--suffix' is specified, template must end in `X'; if it is not specified, then an appropriate `--suffix' is inferred by finding the last `X' in template. This option exists for use with the default template and for the creation of a suffix that starts with `X'.

`-t'

Treat template as a single file relative to the value of TMPDIR if available, or to the directory specified by `-p', otherwise to `/tmp'. template must not contain slashes. This option is deprecated; the use of `-p' without `-t' offers better defaults (by favoring the command line over TMPDIR) and more flexibility (by allowing intermediate directories).

Exit status:

0 if the file was created,
1 otherwise.

[ << ] [ >> ] [Top] [Contents] [Index] [ ? ]

This document was generated by root on May, 18 2011 using texi2html 1.76.

 
Linki sponsorowane

W celu realizacji usług i funkcji na witrynach internetowych ZUI "ELPRO" stosujemy pliki cookies. Korzystanie z witryny bez zmiany ustawień dotyczących plików cookies oznacza, że będą one zapisywane w urządzeniu wyświetlającym stronę internetową. Więcej szczegółów w Polityce plików cookies.

Akceptuję pliki cookies z tej witryny.