Skip to content

Instantly share code, notes, and snippets.

@askedrelic
Last active August 15, 2017 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save askedrelic/4a0e35c10957b6794f0623faa9c36311 to your computer and use it in GitHub Desktop.
Save askedrelic/4a0e35c10957b6794f0623faa9c36311 to your computer and use it in GitHub Desktop.
Find examples #unix
# better mv
find . -name "*.symlink" -exec sh -c 'echo "$1" ".${1%.symlink}"' _ {} \;
Command Breakdown:
1. '.' => search path starting at current directory marked by ' . '
2. -name => set find match name (in this case all files that end with .gappedPeak)
3. -exec => execute the following command on every match
4. sh -c => 'exec' creates an independent shell environment for each match
5. mv "$1" "${1%.gappedPeak}.bed" => mv first variable (denoted by $1), which is the current file name, to new name. Here I do a substring match and delete; so take first var again, $1 and use % to delete .gappedPeak from the string. The .bed at the end just concatenates the remaining variable, which is now just test#, with .bed, creating the new test#.bed filename.
6. The underscore is a placeholder for $0
7. The {} is replaced by each (*.gappedPeak) filename found by the find command, and becomes $1 to the sh command.
8. \; marks the end of the -exec command.  You can also use ';' or ";".
find . -name "cover.1*" -exec sh -c 'echo "$1" ".${1%.1.jpg}.jpeg"' _ {} \;
# sort sizes of cover.jpg
find . -iname cover.* -print0 | xargs -0 -I {} ls -ash "{}" | sort -h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment