≡ Menu

file names

BASH Shell: For Loop File Names With Spaces

BASH for loop works nicely under UNIX / Linux / Windows and OS X while working on set of files. However, if you try to process a for loop on file name with spaces in them you are going to have some problem. For loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character. There are multiple solutions to this problem.
[click to continue…]

Debian PHP 5 Security Issues

Debian 5 php5 package has serious security issues as follows:

To prevent Denial of Service attacks by exhausting the number of available temporary file names, the max_file_uploads option introduced in PHP 5.3.1 has been backported.
[click to continue…]

Sometime you need to create a shell scripts with output filenames with date in it. For example instead of log file named "secure.log", you can create a filename called "secure-jan-02-06.log" or "secure-DATE-FORMAT.log". The DATE-FORMAT can be set as per requirements:
=> dd-mm-yyyy
=> mm-dd-yyyy
=> yyyy-mm-dd
=> Month-Day-Year
[click to continue…]

Use any one of the following command to create temporary empty file names. The first command is special as it use the redirection operator >, the redirection refers to the standard output. So you are creating a new file or destroying existing file:
[click to continue…]

Various methods exists to create a random temporary file name. This is useful if your application/shell scripting needs temporary unique file names.

Method #1: Use of $RANDOM bash shell variable

1) At shell prompt type command:
# echo $RANDOM

You will get random value every time. This variable can be use to create unique file name as demonstrated by createtempfiles.bash script.

1) Download/view createtempfiles.bash script

2) Save the script to your computer and execute it as follows:
$ chmod +x random1.bash
$ ./random1.bash

Output:
/tmp/0.24101.txt
/tmp/0.28872.txt
/tmp/0.8457.txt
/tmp/0.18006.txt
/tmp/0.29528.txt

Use this method if your script needs more than two temporary files.

Method # 2 Use of $$ variable

This is old and classic method. $$ shell variable returns the current running process this can be use to create unique temporary file as demonstrated in following script:
vi random2.bash

#!/bin/bash
#
TFILE="/tmp/$(basename $0).$$.tmp"
ls > $TFILE
echo "See diretory listing in $TFILE"

Save the script and execute as follows:
$ chmod +x random2.bash
$ ./ random2.bash

Use this method if your script needs only ONE temporary file.

Method # 3 Use of mktemp or tempfile utility

As name suggest both makes unique temporary filename. Just type mktemp at shell prompt to create it:
$ mktemp
Output:
/tmp/tmp.IAnO5O
OR
$ tempfile
Output:
/tmp/IAnO5O

Make a unique temporary directory instead of a file using –d option to both of them
$ mktemp –d
$ tempfile –d

Both mktemp or tempfile provides the shell scripts facility to use temporary files in safe manner hence it is highly recommended to use them.