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

























  • I would suggest that you can probably do this with "find" and "dirname" and maybe "cut" depending on what exactly your goal is.

    – Perkins
    Jan 13 at 4:23
















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

























  • I would suggest that you can probably do this with "find" and "dirname" and maybe "cut" depending on what exactly your goal is.

    – Perkins
    Jan 13 at 4:23














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
















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















share|improve this question













share|improve this question




share|improve this question








edited Jan 12 at 21:11









wjandrea

496413




496413










asked Jan 12 at 18:32









Borg357Borg357

183




183













  • I would suggest that you can probably do this with "find" and "dirname" and maybe "cut" depending on what exactly your goal is.

    – Perkins
    Jan 13 at 4:23



















  • I would suggest that you can probably do this with "find" and "dirname" and maybe "cut" depending on what exactly your goal is.

    – Perkins
    Jan 13 at 4:23

















I would suggest that you can probably do this with "find" and "dirname" and maybe "cut" depending on what exactly your goal is.

– Perkins
Jan 13 at 4:23





I would suggest that you can probably do this with "find" and "dirname" and maybe "cut" depending on what exactly your goal is.

– Perkins
Jan 13 at 4:23










1 Answer
1






active

oldest

votes


















8














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





















  • 2





    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?

    – PRY
    Jan 12 at 18:47











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

    – Kusalananda
    Jan 12 at 18:49











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

    – Borg357
    Jan 12 at 22:51











  • '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
    Jan 12 at 23:25






  • 1





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

    – Kusalananda
    Jan 12 at 23:42











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
});


}
});














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









8














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





















  • 2





    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?

    – PRY
    Jan 12 at 18:47











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

    – Kusalananda
    Jan 12 at 18:49











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

    – Borg357
    Jan 12 at 22:51











  • '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
    Jan 12 at 23:25






  • 1





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

    – Kusalananda
    Jan 12 at 23:42
















8














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





















  • 2





    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?

    – PRY
    Jan 12 at 18:47











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

    – Kusalananda
    Jan 12 at 18:49











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

    – Borg357
    Jan 12 at 22:51











  • '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
    Jan 12 at 23:25






  • 1





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

    – Kusalananda
    Jan 12 at 23:42














8












8








8







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 Jan 12 at 19:59

























answered Jan 12 at 18:45









KusalanandaKusalananda

129k16243401




129k16243401








  • 2





    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?

    – PRY
    Jan 12 at 18:47











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

    – Kusalananda
    Jan 12 at 18:49











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

    – Borg357
    Jan 12 at 22:51











  • '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
    Jan 12 at 23:25






  • 1





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

    – Kusalananda
    Jan 12 at 23:42














  • 2





    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?

    – PRY
    Jan 12 at 18:47











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

    – Kusalananda
    Jan 12 at 18:49











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

    – Borg357
    Jan 12 at 22:51











  • '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
    Jan 12 at 23:25






  • 1





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

    – Kusalananda
    Jan 12 at 23:42








2




2





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?

– PRY
Jan 12 at 18:47





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?

– PRY
Jan 12 at 18:47













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

– Kusalananda
Jan 12 at 18:49





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

– Kusalananda
Jan 12 at 18:49













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

– Borg357
Jan 12 at 22:51





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

– Borg357
Jan 12 at 22:51













'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
Jan 12 at 23:25





'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
Jan 12 at 23:25




1




1





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

– Kusalananda
Jan 12 at 23:42





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

– Kusalananda
Jan 12 at 23:42


















draft saved

draft discarded




















































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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory