1

I have a problem with listing through directories. I wanted to make script which will loop through dirs and files in them so I can move them elsewhere.

My problem is that I need to SORT all the files within the folders. There's like 2000 directories and I need to loop through them and then loop through all files within these dirs and sort them by extension in directory which will be created (or files will be moved) and it's name will be that particular extension

Could anyone help?

#!/bin/bash

tar_fol="$HOME/Desktop/try/"
to_fol="$HOME/Desktop/SortedFiles/"

for DIRE in "$(ls -d  $tar_fol"*")"
do

  echo "Checking dir : $DIRE"

  for FIL in "$(ls -p $tar_fol$DIRE)"
  do
    echo "Checking file : $FIL"

    if [ "$(find ~/Desktop/ -type d -name ${FIL##*.})" != "" ]
      then
          mv -f $tar_fol$FIL $to_fol${FIL##*.}
      else
        mkdir $to_fol${FIL##*.}
        mv $tar_fol$FIL $to_fol${FIL##*.}
      fi

  done
done
3
  • 1
    Apart from $FILE (line 17) which maybe changed to $FIL, can you describe better the problem you're having (if your script is not working)? Commented Jun 3, 2016 at 8:18
  • Thanks for the correction. My problem is that I need to SORT all the files within the folders. There's like 2000 directories and I need to loop through them and then loop through all files within these dirs and sort them by extension in directory which will be created (or files will be moved) and it's name will be that particular extension Problem is that '6 for DIRE in "$(ls -d $tar_fol"*")" giving me an error Commented Jun 3, 2016 at 8:21
  • Please provide a sample of the directory structure used as input and the expected output. Commented Jun 3, 2016 at 8:29

2 Answers 2

2

You are quoting the wildcard, so you are looking for files whose name literally ends with the character *.

Anyway, you should not use ls to drive loops, and also, you should avoid using uppercase for your private variables.

#!/bin/bash

tar_fol="$HOME/Desktop/try/"
to_fol="$HOME/Desktop/SortedFiles/"

for dire in "$tar_fol"/*/.
do
  echo "Checking dir: '$dire'"
  for fil in "$dire"/*
  do
    echo "Checking file: '$fil'"
    find ~/Desktop/ -type d -name "${fil##*.}" -exec sh -c 'mkdir -p "$0";
        mv "$@" "$0"' "$to_fol" "$fil" {} \+
  done
done

I'm not sure I understand exactly what the innermost loop is supposed to accomplish, so there is some guesswork there. The general idea is to pass the destination directory in $0 (obscure hack, but it's fairly common) and the found files to the -exec script.

Sign up to request clarification or add additional context in comments.

6 Comments

First loop is for looping through folders and second for looping through files within these folders. Anyway thanks for your reply. Btw: find: missing argument to -exec'
Sorry about that; added the {} which I guess is what it was complaining about. If your find doesn't support -exec ... {} \+ some additional refactoring is required. My question is about the innermost loop -- the one I replaced with just a find.
that loop is for listing through files in folder so if the file has extension for example .txt then, if directory with the same name as extension of the file exists in to_fol move that file there, if not then make new dir in to_fol with a name of it's extension (of file which was listed in for loop) and move that file to that folder
basically my problem is that i need to sort all of the files from my RECOVERY of SSD. It's about 1500 folders by 500 files. I need to sort every type of file by it's extension to separate folder
So then maybe find ~/Desktop -type f -name "*.${fil##*.}" -exec sh -c 'mkdir -p "$@"; mv "$@" "$0"' "$to_fol/${fil##*.}" {} \+ but I guess this is going to do the same extension many times, so you should optimize away those cases (pipe through sort -u? Skip if the directory in $to_fol already exists?)
|
0

You can make use of ls command

cd $HOME/Desktop/try/
ls -laRX

OR

ls -laRX $HOME/Desktop/try/

   -l     use a long listing format
   -a, --all do not ignore entries starting with .
   -R, --recursive
          list subdirectories recursively
   -X     sort alphabetically by entry extension

Refer man ls for more information

1 Comment

Neat, I had never heard of -X, there's really a tool for everything !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.