Jump to content

Truncate a file: Difference between revisions

(→‎{{header|Lasso}}: Added Lasso contribution)
Line 655:
<lang bash># Truncate a file named "myfile" to 1440 kilobytes.
truncate -s 1440k myfile</lang>
 
 
----
<lang bash>## Pure bourne-shell approach (cross-OS, no truncate(1) binary required)
 
# 1. simplest one-liner
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 afields afields 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
 
# Now a bad file exists
-bash-4.2$ echo 'abc'>bad
+ echo abc
-bash-4.2$ ls -l bad;cat<bad
+ 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<bad;echo "_EOF_"
+ 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() { 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_
# bonus: use a seek and append and add truncate(1) syntax w/ units - short one-liners please
 
=={{header|ZX Spectrum Basic}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.