Skip to content

Instantly share code, notes, and snippets.

@Subangkar
Last active October 5, 2020 15:41
Show Gist options
  • Save Subangkar/7264351ea4c7a2b0c138144ef4a4f5b8 to your computer and use it in GitHub Desktop.
Save Subangkar/7264351ea4c7a2b0c138144ef4a4f5b8 to your computer and use it in GitHub Desktop.
Useful GNU Bash Scripts
# Common Contents of two text files -------------
diff -y -w --strip-trailing-cr -i --minimal --color=always file1 file2 | grep -n -v'' "[|<>]"
diff --ignore-blank-lines --ignore-space-change --ignore-trailing-space --ignore-case --minimal --strip-trailing-cr --unchanged-group-format='@@ %dn, %df
%<' --old-group-format='' --new-group-format='' --changed-group-format='' file1 file2
# -------------------------------------------
# CSV like parsing
sed 's/,,/, ,/g;s/,,/, ,/g' data.csv | column -s, -t
# list all files with type without preceding ./
find -name '*.type' | sed 's/\.\///'
# find and delete files with size less than k
find . -type f -size -30k -maxdepth 1 -exec rm -r {} \;
# count #lines
wc -l
# grep recursively over all files in a directory
grep -nrwi 'pattern' `ls`
grep -nrwi 'pattern' `find . -name '*'`
# string tokenize
IFS=';' tokens=( $string )
echo ${tokens[*]}
# iterate over lines
IFS=$'\n' # make newlines the only separator
for line in $(cat filename)
do
echo "$line"
done
# has process substitution
while read i; do echo "$i"; done < filename
# has process substitution
cat filename | while read -r j
do
echo $j
done
# iterate without process substitution
while read -r line
do
printf '%s\n' "$line"
done < <(ioscan -m dsf)
# rename all files in a directory
rename 's/text/with\ text/g' *.jpg
find . -name '*jpg' -exec bash -c ' mv $0 ${0/text/with}' {} \;
find . -type f -name 'Lucky-*' | while read FILE ; do
newfile="$(echo ${FILE} |sed -e 's/text/with/')" ;
mv "${FILE}" "${newfile}" ;
done
# number of files in a directory
ls -1 `ls test/` | wc -l
# check if directory/file exists
if [ ! -d "path/to/file_or_dir" ]
then
echo "path/to/file_or_dir"
fi
# download if not exists
wget -nc https://url/
# copy folder contents to another folder
cp -a /source/. /dest/ # just contents
cp -R "$source" dest # also source folder
# unzip to a directory
unzip "path/to/zip" -d dest/
# ------- create symlink ------------
ln -s /mnt/my_drive/movies ~/my_movies # softlink
unlink symlink_to_remove
rm symlink_to_remove
# ------- user management -----------
# create new user
sudo useradd -m subangkar -p PASSWORD
sudo useradd subangkar
# set passwd of user
sudo passwd subangkar
# create new group
sudo groupadd newgroup
# add user to group
usermod -a -G grp1,grp2 username
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment