How to resolve the algorithm Truncate a file step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Truncate a file step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Truncate a file to a specific length.   This should be implemented as a routine that takes two parameters: the filename and the required file length (in bytes).

Truncation can be achieved using system or library calls intended for such a task, if such methods exist, or by creating a temporary file of a reduced size and renaming it, after first deleting the original file, if no other method is available.   The file may contain non human readable binary data in an unspecified format, so the routine should be "binary safe", leaving the contents of the untruncated part of the file unchanged. If the specified filename does not exist, or the provided length is not less than the current file length, then the routine should raise an appropriate error condition. On some systems, the provided file truncation facilities might not change the file or may extend the file, if the specified length is greater than the current length of the file. This task permits the use of such facilities.   However, such behaviour should be noted, or optionally a warning message relating to an non change or increase in file size may be implemented.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Truncate a file step by step in the UNIX Shell programming language

Source code in the unix programming language

# Truncate a file named "myfile" to 1440 kilobytes.
ls myfile >/dev/null &&
  dd if=/dev/null of=myfile bs=1 seek=1440k


# Truncate a file named "myfile" to 1440 kilobytes.
truncate -s 1440k myfile


# 1. simplest one-liner
-bash$ >file

# or the more clear/verbose:
echo -n>file

# 2. checks for file as input prior to truncate:
f="file";echo -n<$f>$f

# double-check file size
ls -1 file # it's trunc

# 3. multiple files (glob)
glob=$(ls -1 file[0-9]); for f in $glob; do echo -n<$f>$f; done


## OUTPUT:
# 1. one-liner
-bash-4.2$ echo -n>bad
+ echo -n
-bash-4.2$ ls -l bad; cat bad
+ ls -l bad
-rw-r--r--  1 fieldsa  fieldsa  0 Dec  4 20:07 bad
+ cat bad
-bash-4.2$ 

# 2. checks
-bash-4.2$ set -x; f="bad"; echo -n<$f>$f
+ f=bad
+ echo -n
-bash: bad: No such file or directory

# Try again with the shortest version: f="bad"<$f>$f
+ ls -l bad
-rw-r--r--  1 fieldsa  fieldsa  8192 Dec  5 01:04 bad
-bash-4.2$ f="bad" <$f>$f
+ f=bad
-bash-4.2$ ls -l
+ ls -l
-rw-r--r--  1 fieldsa  fieldsa       0 Dec  5 01:04 bad
-bash-4.2$ rm bad
+ rm bad
-bash-4.2$ f="bad" <$f>$f
+ f=bad
-bash: bad: No such file or directory

# Now a bad file exists
-bash-4.2$ echo 'abc'>bad
+ echo abc
-bash-4.2$ ls -l bad;cat
+ ls -l bad
-rw-r--r--  1 fieldsa  fieldsa  4 Dec  4 19:21 bad
+ cat
abc

-bash-4.2$ f="bad"; echo -n<$f>$f
+ f=bad
+ echo -n
-bash-4.2$ ls -l bad;cat
+ ls -l bad
-rw-r--r--  1 fieldsa  fieldsa  0 Dec  4 19:23 bad
+ cat
+ echo _EOF_
_EOF_

-bash-4.2$ rm bad
+ rm bad
-bash-4.2$ f="bad" <$f&&echo -n>$f
+ f=bad
-bash: bad: No such file or directory

# 4. trunc(): trunc file size # truncate at length
-bash-4.2$ trunc() { IFS= read -N $2 b<"$1"; echo -n "$b">"$1"; }
-bash-4.2$ echo "abcdef">bad; ls -l bad; cat bad; trunc bad 3; ls -l bad; cat bad; echo "_EOF_"
+ echo abcdef
+ ls -l bad
-rw-r--r--  1 fieldsa  fieldsa  7 Dec  4 19:59 bad
+ cat bad
abcdef
+ trunc bad 3
+ read -N 3 b
+ echo -n abc
+ ls -l bad
-rw-r--r--  1 fieldsa  fieldsa  3 Dec  4 19:59 bad
+ cat bad
abc+ echo _EOF_
_EOF_


  

You may also check:How to resolve the algorithm Greatest element of a list step by step in the RapidQ programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Classes step by step in the Raven programming language
You may also check:How to resolve the algorithm Vector products step by step in the Clojure programming language
You may also check:How to resolve the algorithm MD4 step by step in the Java programming language