Skip to content

Instantly share code, notes, and snippets.

@vaibhaw
Last active June 17, 2019 16:18
Show Gist options
  • Save vaibhaw/ff480912e12daf02002d to your computer and use it in GitHub Desktop.
Save vaibhaw/ff480912e12daf02002d to your computer and use it in GitHub Desktop.
# search and replace strings
grep -rl search_string directory | xargs sed -i 's/string_to_replace/replace_string/g'
# bulk rename files in a folder
a=1
for i in *.jpg; do
new=$(printf "sofa_%04d.jpg" "$a") #04 pad to length of 4
mv -- "$i" "$new"
let a=a+1
done
# bulk rename files - single line
# Ref: http://www.howtogeek.com/howto/ubuntu/shell-geek-rename-multiple-files-at-once/
# It will replace test with prod in all the matched files
for f in *.jpg; do mv $f ${f/test/prod}; done
# location of an executable/binary
which <binary_name>
# which python results
# /usr/bin/python
# realpath of a symlink
readlink -f <path>
# readlink -f /usr/bin/python
# redirect output and error to log from an executable run from bash
# Ref: http://stackoverflow.com/questions/4699790/cmd-21-log-vs-cmd-log-21
cmd > log 2>&1
# copy folder excluding a specific sub-directory
# thefoldertoexclude is relative to sourcefolder
rsync -av --progress sourcefolder destinationfolder --exclude thefoldertoexclude
# exclude multiple folders/files written in exclude file.
# each entry should be on newline. all paths must be relative to source folder
rsync -av --progress sourcefolder destinationfolder --exclude-from 'exclude-list.txt'
# compare 2 folders
diff -rq folder-1 folder-2
# sync files across servers
rsync -avuzpr folder/ hercules:java_projects/folder/
a = archive mode
v = increase verbose
u = skip files that are newer on the receiver
z = compress file data during the transfer
Archive mode is the same as options -rlptgoD
r = recurse into directories
l = copy symlinks as symlinks
p = preserve permissions
t = preserve modification times
g = preserve group
o = preserve owner (super-user only)
D = preserve device & special files
# list only directories .*/ matches hidden folders too
# ref: http://stackoverflow.com/q/14352290/925216
ls -ld .*/ */
# To recursively give directories read&execute privileges:
find /path/to/base/dir -type d -exec chmod 755 {} +
# To recursively give files read privileges:
find /path/to/base/dir -type f -exec chmod 644 {} +
# Or, if there are many objects to process:
chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
# Or, to reduce chmod spawning:
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644
# delete files with specific extension and less than some size
find -name "*.tif" -size -160k -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment