GNU Coreutils Snippets
cat - concatenate
Section titled “cat - concatenate”# Send multiline text to a filecat <<EOF > test.mdThe quick brown fox jumped over the lazy dogEOF
# Show multiple filescat file1 file2 file3
# Show Linux distribution and release informationcat /etc/*releasechown, chmod - Change file permission
Section titled “chown, chmod - Change file permission”# Give current user read write execute on all files in currect directory and files and folder below itchmod -R u=rwx ./
# Change ownership to user tom, group admin of text.txt filesudo chown tom:admin test.txt
# Chage just ownersudo chown admin test.txt
# Change just groupsudo chgrp devops test.txtSee also DevOps Bootcamp Modifying Permissions file permissions section of - DevOps Bootcamp with Nana - DevOps Bootcamp Series with Nana Janashia
cut - remove selections from each line of files
Section titled “cut - remove selections from each line of files”# Print the fifth character on each line:command | cut [-c|--characters] 5
# Print the fifth to tenth character of each line of the specified file:cut [-c|--characters] 5-10 path/to/file
# Split each line in a file by a delimiter into fields and print fields two and six (default delimiter is `TAB`):cut [-f|--fields] 2,6 path/to/file
# Use space as a delimiter and print only the first 3 fields:command | cut [-d|--delimiter] " " [-f|--fields] -3
# Use : as delimiter and print first part before delimitercommand | cut -d ":" -f 1
# Only print lines that contain the delimiter:command | cut [-d|--delimiter] ":" [-f|--fields] 1 [-s|--only-delimited]
# Print specific fields of lines that use `NUL` to terminate lines instead of newlines:find . -print0 | cut [-z|--zero-terminated] [-d|--delimiter] "/" [-f|--fields] 2df - display free disk space
Section titled “df - display free disk space”Reports on disk space on mounted and mounted file systems
# display in human readable formatdf -hdf -ahk# -a all mount points# -h human readable# -k Use 1024 byte (1-Kbyte) blocksdu - disk usage
Section titled “du - disk usage”# Check disk usage of current directory up to 1 directory leveldu -h --max-depth=1ln - link files
Section titled “ln - link files”# Create a symbolic link to a fileln -s /path/to/file /path/to/symlink
# Create a symbolic link to a directoryln -s /path/to/directory /path/to/symlink
# Remove a symbolic linkrm /path/to/symlinkrm -r /path/to/symlink-directorylocate - find files from index
Section titled “locate - find files from index”See Locate Snippets - Locate Snippets
od (Octal Dump)
Section titled “od (Octal Dump)”Displays a file in octal (base 8) format by default. Used for seeing data that isn’t in a human readable format that control characters in files.
# Display file in character format and show control charactersod -c input.txt# Sort output of a command or text files, output to standard outsort file.txt
# Sort file and save back to new file or can be same filesort file.txt -o new_file.txt
# Call sort from uutilcoreutils sort [OPTION] [FILE]tee - Update a file’s contents and append items
Section titled “tee - Update a file’s contents and append items”# Update a file's contents and append items# Update /etc/environment for all usersecho "http_proxy=http://2.3.4.5:3128" | sudo tee -a /etc/environmentecho "https_proxy=http://2.3.4.5:3128" | sudo tee -a /etc/environmentecho "export no_proxy=localhost, 127.0.0.1" | sudo tee -a /etc/environmenttr - translate/delete
Section titled “tr - translate/delete”Use also sed command if use of tr becomes complex
# Replace Characters J for Zecho 'Call me Justin' | tr 'J' 'Z'
# Replace delimitersecho 'FirstName LastName Comment Age' | tr ' ' ','
# Replace character with newlines# Easier way to see a pathecho $PATH | tr ":" "\n"
# Combine techniques below to clean up textecho "Mangled FiLE-nAMe.txt" | tr -d '-' | tr -s ' ' | tr ' ' '_' | tr '[:upper:]' '[:lower:]'# and pipe output to tr repeatedly like a bad filename
# Replace lower case with upper caseecho 'Call me Justin' | tr '[:lower:]' '[:upper:]'# orecho 'Call me Justin' | tr 'a-z' 'A-Z'# Other tokens that can be used for matching and replacing# [:alnum:]: Letters and digits.# [:alpha:]: Letters only.# [:digit:]: Digits only.# [:blank:]: Tabs and spaces.# [:space:]: All whitespace, including newline characters.# [:graph:]: All characters including symbols, but not spaces.# [:print:]: All characters including symbols, including spaces.# [:punct:]: All punctuation characters.# [:lower:]: Lowercase letters.# [:upper:]: Uppercase letters.
# Invert matches with -c (complement)# Convert all spaces to dashes '-'echo 'A long file name nice to have as dashed name' | tr ' ' '-'
# Delete characters -d# Delete all spaces and letter iecho 'Characters and 12354 numbers' | tr -d ' i'
# Reduce repeated characters -s (squeeze repeats)# Reduce repeated spaces to a single spaceecho 'A spaced out sentence' | tr -s ' '
# Delete all blank charactersecho 'Call me Just in' | tr -d '[:blank:]'
# Delete all whitespace (tabs, newlines, spaces)echo 'bunch of words to merge' | tr -d '[:space:]'
# Delete everything except digits from a string# Deletion will include space, whitespace, newlines# -c and -d to complete (reverse match) and delete othersecho 'Call me 123 Justin 552' | tr -cd '[:digit:]'uniq - choose unique items
Section titled “uniq - choose unique items”# Show only uniq lines in a filecat file.txt | uniqyes - repeat string or y
Section titled “yes - repeat string or y”Useful for scripts requiring prompts
# Output Hello world repeatedlyyes "Hello World"
# Automatically confirm a prompt, for example in apt, alternative to apt -yyes | sudo apt install vim-nox
# Automatically decline a prompt by outputting n repeatedlyyes n | sudo apt install vim-nox# Note yes piping will only work when there is a prompt# In this example, if all vim-nox dependencies are installed, there won't be a prompt# and the package will be installed anyways
# Limit output of string output to first 5 lines using head limit of 5yes | head -n 5