### Create Directory and Change to It Source: https://www.gnu.org/software/coreutils/manual/html_node/csplit-invocation.html Example setup for demonstrating csplit usage by creating a directory and navigating into it. ```bash $ mkdir d && cd d ``` -------------------------------- ### Install command synopses Source: https://www.gnu.org/software/coreutils/manual/coreutils.html These are the basic synopses for the `install` command, showing different ways to copy files and create directories with specified attributes. ```shell install [option]... [-T] source dest ``` ```shell install [option]... source... directory ``` ```shell install [option]... -t directory source... ``` ```shell install [option]... -d directory... ``` -------------------------------- ### Example input file 1 Source: https://www.gnu.org/software/coreutils/manual/html_node/Paired-and-unpaired-lines.html Content of the first input file used in `join` examples. ```text a 1 b 2 ``` -------------------------------- ### Install command with backup option Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Use the `--backup` option with `install` to create backups of files that would otherwise be overwritten or removed. This is useful for preserving previous versions during installation. ```shell install -b [option]... [-T] source dest ``` -------------------------------- ### Example input file 2 Source: https://www.gnu.org/software/coreutils/manual/html_node/Paired-and-unpaired-lines.html Content of the second input file used in `join` examples. ```text a A c C ``` -------------------------------- ### Example Debian Version Strings Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Provides examples of valid Debian version strings with different combinations of epoch and debian_revision. ```text 60.7.2esr-1~deb9u1 52.9.0esr-1~deb9u1 1:2.3.4-1+b2 327-2 1:1.0.13-3 2:1.19.2-1+deb9u5 ``` -------------------------------- ### cat Command Examples Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Examples demonstrating how to use the cat command to output file contents and standard input, or to copy standard input to standard output. ```bash # Output f's contents, then standard input, then g's contents. cat f - g ``` ```bash # Copy standard input to standard output. cat ``` -------------------------------- ### Examples of Debian Version Strings Source: https://www.gnu.org/software/coreutils/manual/html_node/Hyphen_002dminus-and-colon.html Provides concrete examples of version strings adhering to Debian's syntax, showcasing the use of epoch and revision parts. ```plaintext 60.7.2esr-1~deb9u1 52.9.0esr-1~deb9u1 1:2.3.4-1+b2 327-2 1:1.0.13-3 2:1.19.2-1+deb9u5 ``` -------------------------------- ### Install command with compare option Source: https://www.gnu.org/software/coreutils/manual/coreutils.html The `--compare` option for `install` checks if the content and attributes of source and destination files are identical. If no changes are needed, the destination is not modified, which can prevent redundant operations. ```shell install -C [option]... [-T] source dest ``` -------------------------------- ### Install command with directory creation option Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Use the `-D` option with `install` to automatically create any missing parent directories for the destination file before copying. This ensures the target directory structure exists. ```shell install -D [option]... [-T] source dest ``` -------------------------------- ### Create symbolic link to a directory (Bad Example) Source: https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html This 'Bad Example' shows creating a symbolic link to a directory using a relative path that points to itself, which is generally not useful. ```bash # Create link ../a pointing to a in that directory. # Not really useful because it points to itself. ln -s a .. ``` -------------------------------- ### tac Command Example Source: https://www.gnu.org/software/coreutils/manual/coreutils.html An example demonstrating how to use the tac command with the -r and -s options to reverse a file character by character. ```bash # Reverse a file character by character. tac -r -s 'x\|[^x]' ``` -------------------------------- ### Join command usage example Source: https://www.gnu.org/software/coreutils/manual/html_node/join-invocation.html Demonstrates joining two files based on the first field. ```bash $ cat file1 a 1 b 2 e 5 $ cat file2 a X e Y f Z $ join file1 file2 a 1 X e 5 Y ``` -------------------------------- ### Change ownership using --from option Source: https://www.gnu.org/software/coreutils/manual/html_node/chown-invocation.html Example of using the --from option to safely change ownership recursively. ```shell chown -h -R --from=OLDUSER NEWUSER / ``` -------------------------------- ### Debian Version String Syntax Example Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Illustrates the syntax of Debian version strings, including optional epoch and debian_revision parts. ```text [epoch:]upstream_version[-debian_revision] ``` -------------------------------- ### Configure unit separators Source: https://www.gnu.org/software/coreutils/manual/html_node/numfmt-invocation.html Examples for setting unit separators for input and output scaling. ```bash Add a space on output: --unit-separator=' ' Disable blanks on input: --unit-separator='' Support blanks on input: --delimiter='' Ditto and output non-breaking space: -d '' --unit-separator=$'\u00A0' ``` -------------------------------- ### Example of ls --dired Output Source: https://www.gnu.org/software/coreutils/manual/html_node/What-information-is-listed.html Demonstrates the output structure of ls with the --dired flag enabled. ```bash $ mkdir -p a/sub/deeper a/sub2 $ touch a/f1 a/f2 $ touch a/sub/deeper/file $ ls -gloRF --dired a a: total 8 -rw-r--r-- 1 0 Jun 10 12:27 f1 -rw-r--r-- 1 0 Jun 10 12:27 f2 drwxr-xr-x 3 4096 Jun 10 12:27 sub/ drwxr-xr-x 2 4096 Jun 10 12:27 sub2/ a/sub: total 4 drwxr-xr-x 2 4096 Jun 10 12:27 deeper/ a/sub/deeper: total 0 -rw-r--r-- 1 0 Jun 10 12:27 file a/sub2: total 0 //DIRED// 48 50 84 86 120 123 158 162 217 223 282 286 //SUBDIRED// 2 3 167 172 228 240 290 296 //DIRED-OPTIONS// --quoting-style=literal ``` -------------------------------- ### Backup options for cp, mv, ln, install Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Specifies backup behavior for file operations. ```bash --backup ``` -------------------------------- ### Create symbolic link with absolute path (Bad Example) Source: https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html This 'Bad Example' illustrates creating a symbolic link using hard-coded absolute file names, which makes the link fragile and difficult to move. ```bash # Hard coded file names don't move well. ln -s $(pwd)/a /some/dir/ ``` -------------------------------- ### tsort Example with Simple Input Source: https://www.gnu.org/software/coreutils/manual/html_node/tsort-invocation.html Demonstrates tsort's ability to produce a total ordering from a simple partial ordering. ```bash tsort < a; ln -s a b); cp -aL b c; ls -i1 c/b 74163295 a 74163295 b ``` -------------------------------- ### Running commands with modified environments Source: https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html Examples of running commands with specific environment modifications. ```bash env - PATH="$PATH" foo ``` ```bash env foo ``` ```bash env DISPLAY=gnu:0 LOGNAME=foo nemacs ``` ```bash env -u EDITOR PATH=/energy -- e=mc2 bar baz ``` -------------------------------- ### Create a sparse file Source: https://www.gnu.org/software/coreutils/manual/html_node/du-invocation.html Example command to create a sparse file with a specific apparent size. ```shell dd bs=1 seek=2GiB if=/dev/null of=big ``` -------------------------------- ### Version sort breakdown example Source: https://www.gnu.org/software/coreutils/manual/html_node/Version_002dsort-ordering-rules.html A visual representation of how the version sort algorithm decomposes two filenames into segments for comparison. ```text foo vs foo (rule 2, non-digits) 07 vs 7 (rule 3, digits) . vs a. (rule 2) 7 vs 7 (rule 3) z vs z (rule 2) ``` -------------------------------- ### tsort Example: Simple List Sorting Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Demonstrates sorting a simple list of items with dependencies using tsort with a here-document. ```bash tsort <(cksum -a sha2 -l 256 > dvd.sha256) > dvd.iso ``` -------------------------------- ### pr: Specify starting page Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Starts `pr` output from a specific page number. This option can also specify a range of pages to print. ```bash +first_page[:last_page] ``` -------------------------------- ### ISO 8601 Combined Date and Time Examples Source: https://www.gnu.org/software/coreutils/manual/html_node/Combined-date-and-time-of-day-items.html Examples of valid ISO 8601 date and time strings using both 'T' and space separators. ```text 2022-09-24T20:02:00.052-05:00 2022-12-31T23:59:59,999999999+11:00 1970-01-01 00:00Z ``` -------------------------------- ### Using numfmt with output padding Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example shows how to pad output numbers to a specific width with spaces. Positive numbers right-align, negative numbers left-align. ```shell numfmt --padding=n ``` -------------------------------- ### Example of Preserving Links with cp -aH Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example demonstrates preserving links using `cp -aH` when copying files and symbolic links, showing the resulting inode numbers. ```bash $ mkdir c; : > a; ln -s a b; cp -aH a b c; ls -i1 c 74161745 a 74161745 b ``` -------------------------------- ### Efficient method for downloading and checksumming using tee Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example demonstrates an efficient approach using tee to download a file and compute its checksum simultaneously, parallelizing the process. ```bash wget -O - https://example.com/some.iso | tee some.iso | cksum -a sha2 -l 256 > some.iso.sha256 ``` -------------------------------- ### Start with an empty environment using env -i Source: https://www.gnu.org/software/coreutils/manual/coreutils.html The `-i` or `--ignore-environment` option starts the command execution with a completely empty environment, discarding all inherited environment variables. ```bash env -i command ``` -------------------------------- ### Efficient checksumming using tee and process substitution Source: https://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html This example demonstrates an efficient method to checksum a downloaded file by interleaving download and checksum computation using tee and process substitution. It requires a shell that supports process substitution like bash or zsh. ```bash # slightly contrived, to demonstrate process substitution wget -O - https://example.com/dvd.iso \ | tee >(cksum -a sha2 -l 256 > dvd.sha256) > dvd.iso ``` -------------------------------- ### date output format specifiers Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Arguments starting with '+' define the output format, similar to strftime. Conversion specifiers, starting with '%', are used for specific date and time elements. ```bash +%a %b %e %H:%M:%S %Z %Y ``` -------------------------------- ### Using numfmt with a specific output unit size Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example specifies the output unit size. It's used when output numbers represent other units, for example, converting bytes to kilobytes. ```shell numfmt --to-unit=n ``` -------------------------------- ### Install command for directory creation Source: https://www.gnu.org/software/coreutils/manual/coreutils.html The `--directory` option creates specified directories and any necessary parent directories. It sets owner, group, and mode according to command-line arguments or defaults. ```shell install -d [option]... directory... ``` -------------------------------- ### Install command with file mode Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Control the file mode bits of installed files or directories with the `--mode` option. You can specify octal numbers or symbolic modes, with a default of 'u=rwx,go=rx,a-s'. ```shell install -m mode [option]... [-T] source dest ``` -------------------------------- ### Padding examples for date output Source: https://www.gnu.org/software/coreutils/manual/html_node/Padding-and-other-flags.html Demonstrates different padding behaviors for numeric fields in `date` output. Use '-' for no padding, '_' for space padding. ```shell date +%d/%m -d "Feb 1" ⇒ 01/02 ``` ```shell date +%-d/%-m -d "Feb 1" ⇒ 1/2 ``` ```shell date +%_d/%_m -d "Feb 1" ⇒ 1/ 2 ``` -------------------------------- ### Install command with group ownership Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Specify the group ownership for installed files or directories using the `--group` option. This allows setting the group to a name or numeric ID, defaulting to the current process's group. ```shell install -g group [option]... [-T] source dest ``` -------------------------------- ### File extension handling: Debian vs. Coreutils Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Illustrates the difference in handling file extensions between Debian's algorithm and `sort -V`. The examples show distinct sorting outcomes. ```shell $ compver hello-8.txt hello-8.2.txt 2>/dev/null hello-8.2.txt hello-8.txt $ printf '%s\n' hello-8.txt hello-8.2.txt | sort -V hello-8.txt hello-8.2.txt ``` -------------------------------- ### Install command with owner ownership Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Set the owner of installed files or directories using the `--owner` option, provided the process has sufficient privileges (e.g., run as root). The owner can be a name or numeric ID, defaulting to 'root'. ```shell install -o owner [option]... [-T] source dest ``` -------------------------------- ### cp --parents example Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Illustrates the use of `cp --parents` to create destination files with a full path structure relative to the target directory. ```bash cp --parents a/b/c existing_dir ``` -------------------------------- ### GET /uname Source: https://www.gnu.org/software/coreutils/manual/html_node/uname-invocation.html Retrieves system information based on the provided options. ```APIDOC ## GET /uname ### Description Prints information about the machine and operating system it is run on. If no options are given, it defaults to the -s option. ### Method GET ### Endpoint uname ### Parameters #### Query Parameters - **-a, --all** (flag) - Optional - Print all information. - **-i, --hardware-platform** (flag) - Optional - Print the hardware platform name. - **-m, --machine** (flag) - Optional - Print the machine hardware name. - **-n, --nodename** (flag) - Optional - Print the network node hostname. - **-p, --processor** (flag) - Optional - Print the processor type. - **-o, --operating-system** (flag) - Optional - Print the name of the operating system. - **-r, --kernel-release** (flag) - Optional - Print the kernel release. - **-s, --kernel-name** (flag) - Optional - Print the kernel name. - **-v, --kernel-version** (flag) - Optional - Print the kernel version. ### Response #### Success Response (0) - **Output** (string) - The requested system information string. #### Error Response (non-zero) - **Status** (integer) - Indicates failure. ``` -------------------------------- ### Time of day format examples Source: https://www.gnu.org/software/coreutils/manual/html_node/Time-of-day-items.html Various valid representations of the same time of day. ```text 20:02:00.000000 20:02 8:02pm 20:02-0500 # In EST (U.S. Eastern Standard Time). ``` -------------------------------- ### Create GNU-style backups with cp Source: https://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html Iterates through a list of files and creates backups using the --backup and --force flags. ```bash fail=0 for i; do cp --backup --force --preserve=all -- "$i" "$i" || fail=1 done exit $fail ``` -------------------------------- ### Synopsis of fmt command Source: https://www.gnu.org/software/coreutils/manual/html_node/fmt-invocation.html This shows the basic syntax for using the fmt command with its options and file arguments. ```shell fmt [option]... [file]... ``` -------------------------------- ### Substring Extraction Source: https://www.gnu.org/software/coreutils/manual/html_node/String-expressions.html Extracts a portion of a string based on a starting position and length. ```APIDOC ## substr string position length ### Description Returns the substring of `string` beginning at `position` with a maximum length of `length`. If `position` or `length` is negative, zero, or non-numeric, the null string is returned. ### Method `expr` (string expression) ### Parameters #### String Argument - **string** (string) - The original string. #### Position Argument - **position** (integer) - The starting position (1-based index). #### Length Argument - **length** (integer) - The maximum length of the substring. ### Request Example ```bash expr substr "hello world" 7 5 ``` ### Response #### Success Response (string) - **substring** (string) - The extracted substring. #### Response Example ``` world ``` ``` -------------------------------- ### Efficient Compression of du Output with tee and Process Substitution Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example uses `tee` and process substitution to compress `du` output efficiently, allowing immediate processing of the data while creating a compressed copy. ```bash du -ak | tee >(gzip -9 > /tmp/du.gz) | checkspace -a ``` -------------------------------- ### Example dd output format Source: https://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html The standard output format for dd I/O statistics. ```text 3441325+0 records in 3441325+0 records out 3441325000 bytes (3.4 GB, 3.2 GiB) copied, 1.00036 s, 3.4 GB/s 5000000+0 records in 5000000+0 records out 5000000000 bytes (5.0 GB, 4.7 GiB) copied, 1.44433 s, 3.5 GB/s ``` -------------------------------- ### Display RFC 5322 email format Source: https://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html Example output for the --rfc-email option. ```text Mon, 09 Jul 2020 17:00:00 -0400 ``` -------------------------------- ### Create Directory and Change to It Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This command sequence is used to set up an empty directory for performing file operations. ```bash $ mkdir d && cd d ``` -------------------------------- ### Example: Current full month name and day Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Illustrates formatting the current date to show the full month name and day of the month. Note the potential for leading zeros on single-digit days. ```bash date '+%B %d' ``` -------------------------------- ### Efficiently Compress Output with tee and Process Substitution Source: https://www.gnu.org/software/coreutils/manual/html_node/tee-invocation.html This approach uses `tee` with process substitution to start the GUI immediately and avoid decompression. The output of `du -ak` is piped to `tee`, which sends it to `gzip` for compression and simultaneously to `checkspace`. ```bash du -ak | tee >(gzip -9 > /tmp/du.gz) | checkspace -a ``` -------------------------------- ### Recursively Change Group Ownership Source: https://www.gnu.org/software/coreutils/manual/html_node/chgrp-invocation.html Example of recursively changing the group ownership of a directory and its contents. ```bash # Change the group of /u and subfiles to "staff". chgrp -hR staff /u ``` -------------------------------- ### File Extension Examples for Version Sort Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Illustrates how GNU Coreutils identifies file extensions based on specific patterns. The longest matching suffix is used, with exceptions for certain patterns. ```text ‘hello-8.txt’: the suffix is ‘.txt’ ‘hello-8.2.txt’: the suffix is ‘.txt’ (‘.2’ is not included because the dot is not followed by a letter) ‘hello-8.0.12.tar.gz’: the suffix is ‘.tar.gz’ (‘.0.12’ is not included) ‘hello-8.2’: no suffix (suffix is an empty string) ‘hello.foobar65’: the suffix is ‘.foobar65’ ‘gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2’: the suffix is ‘.fc9.tar.bz2’ (‘.7rc2’ is not included as it begins with a digit) ‘.autom4te.cfg’: the suffix is the entire string. ``` -------------------------------- ### Change Group Ownership Source: https://www.gnu.org/software/coreutils/manual/html_node/chgrp-invocation.html Example of changing the group ownership of a file or directory to a specific group. ```bash # Change the group of /u to "staff". chgrp staff /u ``` -------------------------------- ### Inefficient method for downloading and checksumming Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example shows a less efficient way to download a file and then compute its checksum, requiring two passes. ```bash wget https://example.com/some.iso && cksum -a sha2 -l 256 some.iso ``` -------------------------------- ### Failed multi-argument shebang Source: https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html Example of a failed attempt to pass multiple arguments to an interpreter in a shebang line. ```bash #!/usr/bin/env perl -T -w print "hello\n"; ``` ```bash /usr/bin/env: 'perl -T -w': No such file or directory ``` -------------------------------- ### Create Directories with Default Permissions Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Creates directories A, B, and C, preserving their default permissions. Sets specific permissions for A, B, and C using octal and symbolic modes. ```bash mkdir A B C chmod 755 A chmod 0755 B chmod u=rwx,go=rx C ``` -------------------------------- ### Z85 Encoding Example Source: https://www.gnu.org/software/coreutils/manual/html_node/basenc-invocation.html Encodes binary data to Z85 format. Input is provided via printf. ```bash printf '\376\117\202\000' | basenc --z85 ``` -------------------------------- ### Print last system boot time Source: https://www.gnu.org/software/coreutils/manual/coreutils.html Use the --boot option to display the date and time of the last system boot. ```bash who --boot ``` -------------------------------- ### Factorize large numbers Source: https://www.gnu.org/software/coreutils/manual/html_node/factor-invocation.html Example demonstrating the performance of factor on large numbers generated via bc. ```bash $ M8=$(echo 2^31-1 | bc) $ M9=$(echo 2^61-1 | bc) $ n=$(echo "$M8 * $M9" | bc) $ bash -c "time factor $n" 4951760154835678088235319297: 2147483647 2305843009213693951 real 0m0.006s user 0m0.004s sys 0m0.002s ``` -------------------------------- ### Find and execute group change per file Source: https://www.gnu.org/software/coreutils/manual/coreutils.html This example shows a safer, though slower, method of changing group ownership by executing the command for each found file individually. ```shell find / -owner OLDUSER -exec chgrp -h NEWUSER {} \; ``` -------------------------------- ### Base32hex Encoding Example Source: https://www.gnu.org/software/coreutils/manual/html_node/basenc-invocation.html Encodes binary data to Base32hex format. Input is provided via printf. ```bash printf '\376\117\202' | basenc --base32hex ```