Here's an example of using Powershell to restore a group of files from a backup.
Again, I use typeset -b byte to declare byte to be a binary type which, by the way, can hold up to 64KB of either binary or text data. Again, I use the ksh93 I/O mechanism to open the input file, image.gpj using file descriptor 3. Again, I read byte by byte but this time backwards from the last byte of the file to the byte at offset 0. File does not have write permission File does not have execute permission This is sepcial file This is not a directory File size is not zero File does not exist The following points need to be considered while using file test operators − There must be spaces between the operators and the expressions.
This was used to patch up one of our file servers after some lovely 'enterprise' software decided to start randomly truncating files.
We suddenly found ourselves with thousands of empty (zero byte) files split across different directories with unpredictable file dates.
Simply restoring a backup of everything was not an option, as 90% of the files were OK and we don't want to overwrite the non-zero files with old copies.
We suddenly found ourselves with thousands of empty (zero byte) files split across different directories with unpredictable file dates.
Simply restoring a backup of everything was not an option, as 90% of the files were OK and we don't want to overwrite the non-zero files with old copies.
First we restored a tape backup of everything to a separate folder.
Then the script below was used to find each zero byte file, retrieve the matching file from the backup folder and copy it to a third, temporary destination.
(recreating directories in the new destination as needed.)
Then the script below was used to find each zero byte file, retrieve the matching file from the backup folder and copy it to a third, temporary destination.
(recreating directories in the new destination as needed.)
With this done we could check the right files had been copied and then robocopy them all back onto the live server.
# This script will pass through the directory structure once, copying files as it goes.
# Each fully qualified file name must be less than 260 characters,
# Each directory name must be less than 248 characters.
# Each fully qualified file name must be less than 260 characters,
# Each directory name must be less than 248 characters.
note
My first attempt at this was to try Robocopy with /max:0 /L to list all the zero byte files, unfortunately /max:0 did not return any files, so it was powershell to the rescue.
My first attempt at this was to try Robocopy with /max:0 /L to list all the zero byte files, unfortunately /max:0 did not return any files, so it was powershell to the rescue.
- Unix / Linux for Beginners
- Unix / Linux Shell Programming
- Advanced Unix / Linux
- Unix / Linux Useful Resources
- Selected Reading
We have a few operators that can be used to test various properties associated with a Unix file.
Assume a variable file holds an existing file name 'test' the size of which is 100 bytes and has read, write and execute permission on −
Operator | Description | Example |
---|---|---|
-b file | Checks if file is a block special file; if yes, then the condition becomes true. | [ -b $file ] is false. |
-c file | Checks if file is a character special file; if yes, then the condition becomes true. | [ -c $file ] is false. |
-d file | Checks if file is a directory; if yes, then the condition becomes true. | [ -d $file ] is not true. |
-f file | Checks if file is an ordinary file as opposed to a directory or special file; if yes, then the condition becomes true. | [ -f $file ] is true. |
-g file | Checks if file has its set group ID (SGID) bit set; if yes, then the condition becomes true. | [ -g $file ] is false. |
-k file | Checks if file has its sticky bit set; if yes, then the condition becomes true. | [ -k $file ] is false. |
-p file | Checks if file is a named pipe; if yes, then the condition becomes true. | [ -p $file ] is false. |
-t file | Checks if file descriptor is open and associated with a terminal; if yes, then the condition becomes true. | [ -t $file ] is false. |
-u file | Checks if file has its Set User ID (SUID) bit set; if yes, then the condition becomes true. | [ -u $file ] is false. |
-r file | Checks if file is readable; if yes, then the condition becomes true. | [ -r $file ] is true. |
-w file | Checks if file is writable; if yes, then the condition becomes true. | [ -w $file ] is true. |
-x file | Checks if file is executable; if yes, then the condition becomes true. | [ -x $file ] is true. |
-s file | Checks if file has size greater than 0; if yes, then condition becomes true. | [ -s $file ] is true. |
-e file | Checks if file exists; is true even if file is a directory but exists. | [ -e $file ] is true. |
Example
The following example uses all the file test operators −
Assume a variable file holds an existing file name '/var/www/tutorialspoint/unix/test.sh' the size of which is 100 bytes and has read, write and execute permission −
The above script will produce the following result −
The following points need to be considered while using file test operators −
- There must be spaces between the operators and the expressions. For example, 2+2 is not correct; it should be written as 2 + 2.
- if...then...else...fi statement is a decision-making statement which has been explained in the next chapter.
unix-basic-operators.htm