2

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 ?

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

3 Answers 3

2
photorec='/pathto/your_photo_recovery_folder'
photorecorg='/pathto/your_photo_recorg_folder'

cd "$photorec" && \
   EXTDIR="$photorecorg" find . -type f -name '*.?*' -exec sh -c '
      for f
      do
         dest=${EXTDIR}/${f##*.}
         cp -p "$f" "$dest/."
      done
   ' x {} +
5
  • Got 1 error per file. This is one of them: cp: cannot create regular file 'mp3/.': File or directory not found Commented Apr 3, 2017 at 2:07
  • Did you properly initialize the photorecorg variable with full path? Coz this looks like $photorecorg is null.
    – user218374
    Commented Apr 3, 2017 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. Commented Apr 3, 2017 at 2:14
  • should be some scope issue Commented Apr 3, 2017 at 2:15
  • 1
    You are right just look at the fixed version in the answer section.My bad.
    – user218374
    Commented Apr 3, 2017 at 2:16
2

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.

1
  • I guess I have to take a look at zsh. Advanced stuff :) Commented Apr 3, 2017 at 23:54
1

additional to @user218374 with mkdir -p.

photorec='/media/TiloDataRecoveryRAW'
photorecorg='/media//TiloDataRecoveryOrg'

cd "$photorec" && \
   EXTDIR="$photorecorg" find . -type f -name '*.?*' -exec sh -c '
      for f
      do
         dest=${EXTDIR}/${f##*.}
         mkdir -p "$dest" && cp -p "$f" "$dest/."
      done
   ' x {} +

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .