Recursive shell script to list files












3















I'm trying to write a shell script that lists certain types of files in a Directory (and sub-directories). I'm struggling with the recursive part.



Here's what I have:



#!/bin/sh

#download dir
DOWNLOADING_DIR=/Users/richard/Downloads

echo "Starting Script..."

for FILE in $DOWNLOADING_DIR/*
do
if [ -d "$FILE" ]
then
echo "...Checking Directory "$FILE
for DFILE in $FILE/*
do
echo "Found file ... $DFILE"
done
else
echo "Found file ... $FILE"
echo ""
fi
done


The problem is when it finds the directory, it doesn't find any names of files in the directory. It just lists the sub-directory name and not the files in the sub. It works for the files in the first directory.



I need this script to search out .txt or .doc files and move them to another directory.










share|improve this question









New contributor




Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    3















    I'm trying to write a shell script that lists certain types of files in a Directory (and sub-directories). I'm struggling with the recursive part.



    Here's what I have:



    #!/bin/sh

    #download dir
    DOWNLOADING_DIR=/Users/richard/Downloads

    echo "Starting Script..."

    for FILE in $DOWNLOADING_DIR/*
    do
    if [ -d "$FILE" ]
    then
    echo "...Checking Directory "$FILE
    for DFILE in $FILE/*
    do
    echo "Found file ... $DFILE"
    done
    else
    echo "Found file ... $FILE"
    echo ""
    fi
    done


    The problem is when it finds the directory, it doesn't find any names of files in the directory. It just lists the sub-directory name and not the files in the sub. It works for the files in the first directory.



    I need this script to search out .txt or .doc files and move them to another directory.










    share|improve this question









    New contributor




    Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      3












      3








      3


      1






      I'm trying to write a shell script that lists certain types of files in a Directory (and sub-directories). I'm struggling with the recursive part.



      Here's what I have:



      #!/bin/sh

      #download dir
      DOWNLOADING_DIR=/Users/richard/Downloads

      echo "Starting Script..."

      for FILE in $DOWNLOADING_DIR/*
      do
      if [ -d "$FILE" ]
      then
      echo "...Checking Directory "$FILE
      for DFILE in $FILE/*
      do
      echo "Found file ... $DFILE"
      done
      else
      echo "Found file ... $FILE"
      echo ""
      fi
      done


      The problem is when it finds the directory, it doesn't find any names of files in the directory. It just lists the sub-directory name and not the files in the sub. It works for the files in the first directory.



      I need this script to search out .txt or .doc files and move them to another directory.










      share|improve this question









      New contributor




      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm trying to write a shell script that lists certain types of files in a Directory (and sub-directories). I'm struggling with the recursive part.



      Here's what I have:



      #!/bin/sh

      #download dir
      DOWNLOADING_DIR=/Users/richard/Downloads

      echo "Starting Script..."

      for FILE in $DOWNLOADING_DIR/*
      do
      if [ -d "$FILE" ]
      then
      echo "...Checking Directory "$FILE
      for DFILE in $FILE/*
      do
      echo "Found file ... $DFILE"
      done
      else
      echo "Found file ... $FILE"
      echo ""
      fi
      done


      The problem is when it finds the directory, it doesn't find any names of files in the directory. It just lists the sub-directory name and not the files in the sub. It works for the files in the first directory.



      I need this script to search out .txt or .doc files and move them to another directory.







      shell directory






      share|improve this question









      New contributor




      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 4 hours ago









      wjandrea

      472413




      472413






      New contributor




      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 7 hours ago









      Borg357Borg357

      183




      183




      New contributor




      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Borg357 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          7














          Your script is not recursive, as it does not call itself.



          Here is a variation that implements something like what you have recursively:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          printf '%sn' "$pathname"
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          The function walk_dir takes a directory pathname as its only argument and iterates over its content. If a directory is found, it calls itself recursively to traverse that sub-directory.



          Modifying this to find the files whose filename suffix is either .txt or .doc:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          Note that by "file" above we really mean anything that is not a directory or a symbolic link to a directory, which may not be the same as a regular file. By setting the dotglob and nullglob shell options in bash, we are able to find hidden pathnames and will not have to test specially for possibly empty directories.



          A variation for /bin/sh that does not care about hidden names:



          #!/bin/sh

          walk_dir () {
          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          elif [ -e "$pathname" ]; then
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          With the globstar and exglob shell options in bash, one could even do the following (with no recursion) to move the files:



          shopt -s globstar extglob

          mv "$DOWNLOADING_DIR"/**/*.@(txt|doc) "$destdir"


          ... unless the resulting file list turned out to be too long. The ** matches across slashes in pathnames (enabled by globstar) and *.@(txt|doc) matches any filename that ends with either .txt or .doc (enabled by extglob).





          A far more efficient and portable way to find regular files with a filename suffix of either .txt or .doc in or under some top-level directory $topdir, and to move them to some other directory $destdir:



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv {} "$destdir" ;


          With GNU mv you can make it a bit more efficient,



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv -t "$destdir" {} +


          This variation would move batches of files instead of one file at a time. Use mv with -v to see what gets moved, or add -print before -exec to get a listing of the pathnames that mv is called with.






          share|improve this answer





















          • 1





            I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

            – P_Yadav
            7 hours ago











          • @P_Yadav Yes, I'm currently writing the answer to that first question.

            – Kusalananda
            7 hours ago











          • I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

            – Borg357
            3 hours ago











          • 'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

            – Borg357
            2 hours ago






          • 1





            @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

            – Kusalananda
            2 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Borg357 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494143%2frecursive-shell-script-to-list-files%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          Your script is not recursive, as it does not call itself.



          Here is a variation that implements something like what you have recursively:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          printf '%sn' "$pathname"
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          The function walk_dir takes a directory pathname as its only argument and iterates over its content. If a directory is found, it calls itself recursively to traverse that sub-directory.



          Modifying this to find the files whose filename suffix is either .txt or .doc:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          Note that by "file" above we really mean anything that is not a directory or a symbolic link to a directory, which may not be the same as a regular file. By setting the dotglob and nullglob shell options in bash, we are able to find hidden pathnames and will not have to test specially for possibly empty directories.



          A variation for /bin/sh that does not care about hidden names:



          #!/bin/sh

          walk_dir () {
          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          elif [ -e "$pathname" ]; then
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          With the globstar and exglob shell options in bash, one could even do the following (with no recursion) to move the files:



          shopt -s globstar extglob

          mv "$DOWNLOADING_DIR"/**/*.@(txt|doc) "$destdir"


          ... unless the resulting file list turned out to be too long. The ** matches across slashes in pathnames (enabled by globstar) and *.@(txt|doc) matches any filename that ends with either .txt or .doc (enabled by extglob).





          A far more efficient and portable way to find regular files with a filename suffix of either .txt or .doc in or under some top-level directory $topdir, and to move them to some other directory $destdir:



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv {} "$destdir" ;


          With GNU mv you can make it a bit more efficient,



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv -t "$destdir" {} +


          This variation would move batches of files instead of one file at a time. Use mv with -v to see what gets moved, or add -print before -exec to get a listing of the pathnames that mv is called with.






          share|improve this answer





















          • 1





            I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

            – P_Yadav
            7 hours ago











          • @P_Yadav Yes, I'm currently writing the answer to that first question.

            – Kusalananda
            7 hours ago











          • I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

            – Borg357
            3 hours ago











          • 'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

            – Borg357
            2 hours ago






          • 1





            @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

            – Kusalananda
            2 hours ago
















          7














          Your script is not recursive, as it does not call itself.



          Here is a variation that implements something like what you have recursively:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          printf '%sn' "$pathname"
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          The function walk_dir takes a directory pathname as its only argument and iterates over its content. If a directory is found, it calls itself recursively to traverse that sub-directory.



          Modifying this to find the files whose filename suffix is either .txt or .doc:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          Note that by "file" above we really mean anything that is not a directory or a symbolic link to a directory, which may not be the same as a regular file. By setting the dotglob and nullglob shell options in bash, we are able to find hidden pathnames and will not have to test specially for possibly empty directories.



          A variation for /bin/sh that does not care about hidden names:



          #!/bin/sh

          walk_dir () {
          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          elif [ -e "$pathname" ]; then
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          With the globstar and exglob shell options in bash, one could even do the following (with no recursion) to move the files:



          shopt -s globstar extglob

          mv "$DOWNLOADING_DIR"/**/*.@(txt|doc) "$destdir"


          ... unless the resulting file list turned out to be too long. The ** matches across slashes in pathnames (enabled by globstar) and *.@(txt|doc) matches any filename that ends with either .txt or .doc (enabled by extglob).





          A far more efficient and portable way to find regular files with a filename suffix of either .txt or .doc in or under some top-level directory $topdir, and to move them to some other directory $destdir:



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv {} "$destdir" ;


          With GNU mv you can make it a bit more efficient,



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv -t "$destdir" {} +


          This variation would move batches of files instead of one file at a time. Use mv with -v to see what gets moved, or add -print before -exec to get a listing of the pathnames that mv is called with.






          share|improve this answer





















          • 1





            I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

            – P_Yadav
            7 hours ago











          • @P_Yadav Yes, I'm currently writing the answer to that first question.

            – Kusalananda
            7 hours ago











          • I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

            – Borg357
            3 hours ago











          • 'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

            – Borg357
            2 hours ago






          • 1





            @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

            – Kusalananda
            2 hours ago














          7












          7








          7







          Your script is not recursive, as it does not call itself.



          Here is a variation that implements something like what you have recursively:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          printf '%sn' "$pathname"
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          The function walk_dir takes a directory pathname as its only argument and iterates over its content. If a directory is found, it calls itself recursively to traverse that sub-directory.



          Modifying this to find the files whose filename suffix is either .txt or .doc:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          Note that by "file" above we really mean anything that is not a directory or a symbolic link to a directory, which may not be the same as a regular file. By setting the dotglob and nullglob shell options in bash, we are able to find hidden pathnames and will not have to test specially for possibly empty directories.



          A variation for /bin/sh that does not care about hidden names:



          #!/bin/sh

          walk_dir () {
          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          elif [ -e "$pathname" ]; then
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          With the globstar and exglob shell options in bash, one could even do the following (with no recursion) to move the files:



          shopt -s globstar extglob

          mv "$DOWNLOADING_DIR"/**/*.@(txt|doc) "$destdir"


          ... unless the resulting file list turned out to be too long. The ** matches across slashes in pathnames (enabled by globstar) and *.@(txt|doc) matches any filename that ends with either .txt or .doc (enabled by extglob).





          A far more efficient and portable way to find regular files with a filename suffix of either .txt or .doc in or under some top-level directory $topdir, and to move them to some other directory $destdir:



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv {} "$destdir" ;


          With GNU mv you can make it a bit more efficient,



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv -t "$destdir" {} +


          This variation would move batches of files instead of one file at a time. Use mv with -v to see what gets moved, or add -print before -exec to get a listing of the pathnames that mv is called with.






          share|improve this answer















          Your script is not recursive, as it does not call itself.



          Here is a variation that implements something like what you have recursively:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          printf '%sn' "$pathname"
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          The function walk_dir takes a directory pathname as its only argument and iterates over its content. If a directory is found, it calls itself recursively to traverse that sub-directory.



          Modifying this to find the files whose filename suffix is either .txt or .doc:



          #!/bin/bash

          walk_dir () {
          shopt -s nullglob dotglob

          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          else
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          Note that by "file" above we really mean anything that is not a directory or a symbolic link to a directory, which may not be the same as a regular file. By setting the dotglob and nullglob shell options in bash, we are able to find hidden pathnames and will not have to test specially for possibly empty directories.



          A variation for /bin/sh that does not care about hidden names:



          #!/bin/sh

          walk_dir () {
          for pathname in "$1"/*; do
          if [ -d "$pathname" ]; then
          walk_dir "$pathname"
          elif [ -e "$pathname" ]; then
          case "$pathname" in
          *.txt|*.doc)
          printf '%sn' "$pathname"
          esac
          fi
          done
          }

          DOWNLOADING_DIR=/Users/richard/Downloads

          walk_dir "$DOWNLOADING_DIR"


          With the globstar and exglob shell options in bash, one could even do the following (with no recursion) to move the files:



          shopt -s globstar extglob

          mv "$DOWNLOADING_DIR"/**/*.@(txt|doc) "$destdir"


          ... unless the resulting file list turned out to be too long. The ** matches across slashes in pathnames (enabled by globstar) and *.@(txt|doc) matches any filename that ends with either .txt or .doc (enabled by extglob).





          A far more efficient and portable way to find regular files with a filename suffix of either .txt or .doc in or under some top-level directory $topdir, and to move them to some other directory $destdir:



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv {} "$destdir" ;


          With GNU mv you can make it a bit more efficient,



          find "$topdir" -type f ( -name '*.txt' -o -name '*.doc' ) 
          -exec mv -t "$destdir" {} +


          This variation would move batches of files instead of one file at a time. Use mv with -v to see what gets moved, or add -print before -exec to get a listing of the pathnames that mv is called with.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered 7 hours ago









          KusalanandaKusalananda

          123k16232382




          123k16232382








          • 1





            I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

            – P_Yadav
            7 hours ago











          • @P_Yadav Yes, I'm currently writing the answer to that first question.

            – Kusalananda
            7 hours ago











          • I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

            – Borg357
            3 hours ago











          • 'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

            – Borg357
            2 hours ago






          • 1





            @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

            – Kusalananda
            2 hours ago














          • 1





            I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

            – P_Yadav
            7 hours ago











          • @P_Yadav Yes, I'm currently writing the answer to that first question.

            – Kusalananda
            7 hours ago











          • I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

            – Borg357
            3 hours ago











          • 'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

            – Borg357
            2 hours ago






          • 1





            @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

            – Kusalananda
            2 hours ago








          1




          1





          I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

          – P_Yadav
          7 hours ago





          I think the there are two questions OP asked 1) What is the error in the code of OP? 2) How to move files to another directory?

          – P_Yadav
          7 hours ago













          @P_Yadav Yes, I'm currently writing the answer to that first question.

          – Kusalananda
          7 hours ago





          @P_Yadav Yes, I'm currently writing the answer to that first question.

          – Kusalananda
          7 hours ago













          I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

          – Borg357
          3 hours ago





          I think I'll go toward the efficient route as you said.. and look into find instead of the loops.

          – Borg357
          3 hours ago













          'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

          – Borg357
          2 hours ago





          'find "$DOWNLOADING_DIR" -type f ( -name '.tex' -o -name '.doc' -o -name '*.txt' ) -exec mv -v {} "$COMPLETED_DIR" ;' Is what I went with. It works well. @Kusalananda, But another question, if I may. How does one skip moving a file.. Like if the file contains with file name "sample", is it possible to skip moving that file to the new directory?

          – Borg357
          2 hours ago




          1




          1





          @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

          – Kusalananda
          2 hours ago





          @Borg357 Also note that the parentheses need to be escaped (they weren't in your comment).

          – Kusalananda
          2 hours ago










          Borg357 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Borg357 is a new contributor. Be nice, and check out our Code of Conduct.













          Borg357 is a new contributor. Be nice, and check out our Code of Conduct.












          Borg357 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494143%2frecursive-shell-script-to-list-files%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          第一次世界大戦

          Touch on Surface Book