I have a photorec folder from a data recovery process that has 2481 folders with the recovered files.

The folder names are in the sequence recup_dir.1, recup_dir.2 ... recup_dir.2481

I got a list of all extension in these folders with this command:

find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u

So I created another dir called photorecorg with a list of folders corresponding to each extension I got with the above command:

mkdir 7z arj asf avi bat blend bmp bz2 cdr DOC docx flac flv gif gz html jpg mobi mov mp3 mp4 mpg ods odt ogg pdf php png ppt ps psd rar rtf svg swf tar tif ttf wav wmf xcf xls xlsx xz zip

Now I need to copy the files in the photorec folder to the extension folders in photorecorg:

I thought in something like find . -print | xargs cp $0 ../photorecorg/<extension>

What would be the command that can find the files and copy to each extension dir ?

  • 1
    are the files in those immediate "recup" directories, or are there subdirectories within them? – Jeff Schaller Apr 3 '17 at 0:39
  • the files are in the immediate recup dirs – Nelson Teixeira Apr 3 '17 at 1:35

cd "$photorec" && \
   EXTDIR="$photorecorg" find . -type f -name '*.?*' -exec sh -c '
      for f
         cp -p "$f" "$dest/."
   ' x {} +
  • Got 1 error per file. This is one of them: cp: cannot create regular file 'mp3/.': File or directory not found – Nelson Teixeira Apr 3 '17 at 2:07
  • Did you properly initialize the photorecorg variable with full path? Coz this looks like $photorecorg is null. – user218374 Apr 3 '17 at 2:09
  • it's null and I'm debugging it and seeing that all should be ok. I echoed it before the loop and it echos correctly. when I echoed it in the loop it's empty. – Nelson Teixeira Apr 3 '17 at 2:14
  • should be some scope issue – Nelson Teixeira Apr 3 '17 at 2:15
  • 1
    You are right just look at the fixed version in the answer section.My bad. – user218374 Apr 3 '17 at 2:16

Run zsh. Put autoload zmv in your ~/.zshrc (or, to try it out, type this on the zsh command line). Then you can use the zmv function to move or copy files.

mv -Q '**/*(.)' '$f:e/$f:t'

Explanation: the first operand is a pattern of files to move, the second is the replacement pattern. **/ matches files in subdirectories recursively. (.) is a glob qualifier, it restricts the matches to regular files (so we don't risk matching directories). In the replacement pattern, $f designates the original path, the modifier e retains only the extension, and t retains the full base name.

  • I guess I have to take a look at zsh. Advanced stuff :) – Nelson Teixeira Apr 3 '17 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.