Useful Shell Tricks

Useful shell tricks, shell scripts, shell commands and so on.

Photo by me

Search match lines from text files (csv, txt) and copy to new folder

1
2
3
grep -i -f file.csv -rl /path/to/search | xargs -I {} cp {} /path/to/newfolder/

grep 'ABC,*' data.csv | cut -d ',' -f 2 | xargs -I {} cp {} target_folder
  • grep: Searches for lines that match a pattern in a file. In this case, we’re using the -f option to read the search pattern from a file (file.csv), and the -r option to search recursively through directories.
  • -i: Makes the search case-insensitive.
  • -l: Prints only the names of files that contain the search pattern.
  • /path/to/search: Specifies the directory to search in.
  • xargs: Executes a command using arguments read from standard input. In this case, we’re using it to execute the cp command on each file that matches the search pattern.
  • -I {}: Replaces each occurrence of {} in the command with the name of the file being processed by xargs.
  • cp: Copies files from one location to another.
  • /path/to/newfolder/: Specifies the destination folder for the copied files.

Get md5 of files in folder and rename

1
2
3
4
5
6
7
8
9
#!/bin/bash

for file in $(find folder/ -type f -name "*.jpg")
do
echo $file
md5hash=$(md5 $file | cut -d ' ' -f 4)
cp $file ./new_folder/$md5hash.jpg
echo $md5hash
done

Go inside every subfolder and run command

1
for dir in ./*match_str/; do (cd "$dir" && echo "Updating $dir" && git pull origin main); done