Notes

Bash - more notes

author: Paul Kim

categories: bash, linux, mac

tags: bash, linux, mac

Clear bash history

history -c

Create a folder with same name as file and move file into folder

for file in *; do
  if [[ -f "$file" ]]; then
    mkdir "${file%.*}"
    mv "$file" "${file%.*}"
  fi
done

Create folder with same name as files that end with ".md" and move file into that folder

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    mkdir "${f%.*}"
    mv "$f" "${f%.*}"
  fi
done

Create folder with same name as files that end with ".md" and move file into that folder (USE THIS!)

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    ff="${f#[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-}"
    newf="${ff%.*}"
    mkdir "$newf"
    mv "$f" "${newf}/index.md"
  fi
done

Print all files and directories

for f in *
do
    echo $f
done

Print all directories only

for f in *
do
  if [ -d "$f" ]
  then
    echo $f
  fi
done

Print all files only

for f in *
do
  if [ -f "$f" ]
  then
    echo $f
  fi
done

Print only files that end with ".md"

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    echo $f
  fi
done

strip YYYY-MM-DD

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    echo "${f#*-*-*-}"
  fi
done

strip .md

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    echo "${f%.*}"
  fi
done

strip YYYY-MM-DD and .md

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    f=${f#*-*-*-}
    echo ${f%.*}
  fi
done

strip YYYY-MM-DD and .md

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    echo $(f=${f#*-*-*-};echo ${f%.*})
  fi
done

strip YYYY-MM-DD and .md

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    f=${f#[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-}
    echo ${f%.*}
  fi
done

strip YYYY-MM-DD and .md (USE THIS!)

for f in *
do
  if [ -f "$f" ] && [ ${f: -3} == ".md" ]
  then
    f=${f#[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-}
    echo ${f%.*}
  fi
done

what is the difference between if [ ] and if [[ ]] ?

[] specifies equality. [[]] specifies inequality. however, someone said they will personally never uses [[]] because it is not portable to different shells

what is the purpose of using double-quotes vs single-quotes in bash commands?

var1=QQ

# this will not work
sed -i 's/$var1/ZZ/g' '$file'

# this will work
sed -i "s/$var1/ZZ/g" "$file"
  • Use double quotes to make the shell expand variables while preserving whitespace
  • use single quotes for strings to be treated literally

The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally


How to insert text after a certain string in a file?

I have a file called foo.md with the following content:

---
title: Blank
author: Paul Kim
---

I want to insert the text "type: post" after ---. We can do this with sed:

# append "type: post" after every occurrence of `---`
sed -i '/---/a type: post' foo.md

# insert "type: post" after every occurrence of `---`
sed -i '/---/i type: post' foo.md

But it will insert the text for both occurrences of ---. What if I only want to insert the text on the first occurrence only? We can do this with GNU sed by prepending 0, to the above sed command:

# append "type: post" on first occurrence only of `---`
sed -i '0,/---/a type: post' foo.md

# insert "type: post" on first occurrence only of `---`
sed -i '0,/---/i type: post' foo.md

find command

# change directory to ~/dev/kimbaudi.github.io/_posts/
cd ~/dev/kimbaudi.github.io/_posts/

# recursively find all files whose name ends in .md and print the total
find . -name "*.md" | wc -l

# find all files up to maxdepth 1 whose name ends in .md and print the total
find . -maxdepth 1 -name "*.md" | wc -l

# redirect .md files to text file
# strip "./" from the beginning of output files
cd ~/dev/kimbaudi.github.io/_posts
find . -maxdepth 1 -name "*.md" -printf '%f\n' > ~/Desktop/found.txt

Source

ChromeEdgeFirefoxOpera

© 2021 paulkode.com. All rights reserved.