From fec416c7638ce033134b4652989ad2e11dab90b7 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Wed, 6 Nov 2019 00:11:43 +0000 Subject: [PATCH 1/6] Show using awk without data via STDIN This is useful for those times you want to just do something in awk, - on-the-fly, without having to rely on its standard input (STDIN). It's especially handy when you want to perform floating-point arithmetic, if you're not a shell like ZSH which does support it. --- sheets/awk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheets/awk b/sheets/awk index 67f6a07..e42a739 100644 --- a/sheets/awk +++ b/sheets/awk @@ -15,3 +15,6 @@ awk -v RS='' '/42B/' file # display only first column from multi-column text echo "first-column second-column third-column" | awk '{print $1}' + +# Use awk solo; without the need for something via STDIN. +awk BEGIN'{printf("Example text.\n")}' From ff4af9148508d8d416437e144f77fd2cb5328389 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Wed, 6 Nov 2019 00:18:59 +0000 Subject: [PATCH 2/6] Access environment variables from within awk. ENVIRON is a special associative array variable, and LS_COLORS is an index therein. If you want to see the available variables you could use, you could execute the trusty `env` command. --- sheets/awk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheets/awk b/sheets/awk index e42a739..9610a85 100644 --- a/sheets/awk +++ b/sheets/awk @@ -18,3 +18,6 @@ echo "first-column second-column third-column" | awk '{print $1}' # Use awk solo; without the need for something via STDIN. awk BEGIN'{printf("Example text.\n")}' + +# Accessing environment variables from within awk. +awk 'BEGIN{print ENVIRON["LS_COLORS"]}' From c12d8e2df27e95e8470fae15d4b2ba39a7f93b48 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Wed, 6 Nov 2019 00:28:10 +0000 Subject: [PATCH 3/6] Show counting lines awk reads from STDIN This is useful, but not if this is _all_ you're after; in those cases, you're better off just using `free | wc -l` or similar. However, there may be times you need to count the number of lines in order to achieve _other_ things programmed in awk, which would then be crucial! For example, you may wish to output text informing the user of how many log entries were discovered in one or more files given to awk. --- sheets/awk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheets/awk b/sheets/awk index 9610a85..74b237a 100644 --- a/sheets/awk +++ b/sheets/awk @@ -21,3 +21,6 @@ awk BEGIN'{printf("Example text.\n")}' # Accessing environment variables from within awk. awk 'BEGIN{print ENVIRON["LS_COLORS"]}' + +# One method to count the number of lines; in this case, read from STDIN. +free | awk '{i++} END{print i}' From d69a3c8e3a8f527d096aedb5b2443573928974e3 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Wed, 6 Nov 2019 00:36:07 +0000 Subject: [PATCH 4/6] Very useful when you need to test for certain permissions. I imagine this would be most useful when dealing with more advanced permission schemes. --- sheets/stat | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheets/stat b/sheets/stat index 13d2dac..2d1fc94 100644 --- a/sheets/stat +++ b/sheets/stat @@ -1,2 +1,5 @@ # display numerical values for file permissions stat -c '%a %n' * + +# Display only the octal permissions for the given directory. Great for tests. +stat --format='%a' /boot From 8e5e9dd0a49c23961d1939016dbc61f2d824bb31 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Wed, 6 Nov 2019 00:45:05 +0000 Subject: [PATCH 5/6] Minor comment cleanup, to keep it consistent --- sheets/find | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/sheets/find b/sheets/find index 1723835..8b4a956 100644 --- a/sheets/find +++ b/sheets/find @@ -1,54 +1,55 @@ -# To find files by case-insensitive extension (ex: .jpg, .JPG, .jpG): +# Find files by case-insensitive extension (ex: .jpg, .JPG, .jpG): find . -iname '*.jpg' -# To find directories: +# Find directories: find . -type d -# To find files: +# Find files: find . -type f -# To find files by octal permission: +# Find files by octal permission: find . -type f -perm 777 -# To find files with setuid bit set: +# Find files with setuid bit set: find . -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l # To find files with extension '.txt' and remove them: find ./path/ -name '*.txt' -exec rm '{}' \; -# To find files with extension '.txt' and look for a string into them: +# Find files with extension '.txt' and look for a string into them: find ./path/ -name '*.txt' | xargs grep 'string' -# To find files with size bigger than 5 Mb and sort them by size: +# Find files with size bigger than 5 Mb and sort them by size: find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z -# To find files bigger thank 2 MB and list them: +# Find files bigger thank 2 MB and list them: find . -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' -# To find files modified more than 7 days ago and list file information +# Find files modified more than 7 days ago and list file information find . -type f -mtime +7d -ls -# To find symlinks owned by a user and list file information +# Find symlinks owned by a user and list file information find . -type l --user=username -ls -# To search for and delete empty directories +# Search for and delete empty directories find . -type d -empty -exec rmdir {} \; -# To search for directories named build at a max depth of 2 directories +# Search for directories named build at a max depth of 2 directories find . -maxdepth 2 -name build -type d -# To search all files who are not in .git directory +# Search all files who are not in .git directory find . ! -iwholename '*.git*' -type f -# To find all files that have the same node (hard link) as MY_FILE_HERE +# Find all files that have the same node (hard link) as MY_FILE_HERE find . -type f -samefile MY_FILE_HERE 2>/dev/null -# To find all files in the current directory and modify their permissions +# Find all files in the current directory and modify their permissions find . -type f -exec chmod 644 {} \; -# To find files with extension '.txt.' and edit all of them with vim +# Find files with extension '.txt.' and edit all of them with vim # vim is started only once for all files find . -iname '*.txt' -exec vim {} \+ -# To find all files with extension '.png' and rename them by changing extension to '.jpg' (base name is preserved) +# Find all files with extension '.png' and rename them by changing extension to +# '.jpg' (base name is preserved) find . -type f -iname '*.png' -exec bash -c 'mv "$0" "${0%.*}.jpg"' {} \; From 86007dd5db431099e44d044d879bc5e3c6221d0d Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Wed, 6 Nov 2019 00:50:46 +0000 Subject: [PATCH 6/6] Delete extension-specific files using logic This was taken from my own notes amassed over several years. Commands like these are a real time-saver. A lot of people, I imagine, will probably use `-exec rm {} \+` which is fine and all, but it's another process you'd have to launch, which isn't necessary at all, unless you need certain `rm` functionality. --- sheets/find | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheets/find b/sheets/find index 8b4a956..abe454d 100644 --- a/sheets/find +++ b/sheets/find @@ -53,3 +53,6 @@ find . -iname '*.txt' -exec vim {} \+ # Find all files with extension '.png' and rename them by changing extension to # '.jpg' (base name is preserved) find . -type f -iname '*.png' -exec bash -c 'mv "$0" "${0%.*}.jpg"' {} \; + +# Use logic and grouping to delete extension-specific files. +find \( -iname "*.jpg" -or -iname "*.sfv" -or -iname "*.xspf" \) -type f -delete