Bash script to loop through folders and list files to text Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionSecurity implications of forgetting to quote a variable in bash/POSIX shellsUsing globbing/wildcards when Opening Files from TerminalMove files to multiple foldersUsing find and sed to copy 20th line of many files into one fileLoop in text files shell scriptFolder research taskGo through every subfolder, check for a folder and run scriptBash script to monitor file change and execute commandMoving files into folders using bash scriptCopying and pasting specific file types in folders and subfoldersHow to recursively copy only the files from folders and subfolders?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Why is black pepper both grey and black?
Why was the term "discrete" used in discrete logarithm?
Is the address of a local variable a constexpr?
If Jon Snow became King of the Seven Kingdoms what would his regnal number be?
What does the "x" in "x86" represent?
Why is "Consequences inflicted." not a sentence?
What is this single-engine low-wing propeller plane?
How can I make names more distinctive without making them longer?
"Seemed to had" is it correct?
How do I keep my slimes from escaping their pens?
What is a Meta algorithm?
Why is "Captain Marvel" translated as male in Portugal?
Super Attribute Position on Product Page Magento 1
Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?
List *all* the tuples!
What is the correct way to use the pinch test for dehydration?
Why does Python start at index -1 when indexing a list from the end?
Right-skewed distribution with mean equals to mode?
What's the difference between `auto x = vector<int>()` and `vector<int> x`?
Did Xerox really develop the first LAN?
Is the Standard Deduction better than Itemized when both are the same amount?
Models of set theory where not every set can be linearly ordered
What do you call a phrase that's not an idiom yet?
Bash script to loop through folders and list files to text
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionSecurity implications of forgetting to quote a variable in bash/POSIX shellsUsing globbing/wildcards when Opening Files from TerminalMove files to multiple foldersUsing find and sed to copy 20th line of many files into one fileLoop in text files shell scriptFolder research taskGo through every subfolder, check for a folder and run scriptBash script to monitor file change and execute commandMoving files into folders using bash scriptCopying and pasting specific file types in folders and subfoldersHow to recursively copy only the files from folders and subfolders?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I can do this in Windows CMD scripting but now I am getting into Debian via Raspberry Pi.
What I'd like to do is...
(In current folder)
- For each subfolder...
- Create a file called original_filenames.txt
- Echo the name of the folder into this original_filenames.txt
- List all files (including any subfolders) to original_filenames.txt
bash shell-script
add a comment |
I can do this in Windows CMD scripting but now I am getting into Debian via Raspberry Pi.
What I'd like to do is...
(In current folder)
- For each subfolder...
- Create a file called original_filenames.txt
- Echo the name of the folder into this original_filenames.txt
- List all files (including any subfolders) to original_filenames.txt
bash shell-script
1
What are you having an issue doing? Please include the script you are attempting to use in your question and any errors you are encountering.
– kemotep
May 30 '18 at 18:28
Do you want to descend into subfolders and list those files?
– glenn jackman
May 30 '18 at 18:57
Yes I want to recurse through subfolders. I think I've got something working now, I was getting an error until I added quotes to deal with spaces in folder names:#!/bin/bash; for dir in */ ; do ; tree -s "$dir">"$dir/original_filenames.txt"; done
– Murgatroyd
May 30 '18 at 19:24
That's a good lesson. You'll want to read Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
May 30 '18 at 19:27
add a comment |
I can do this in Windows CMD scripting but now I am getting into Debian via Raspberry Pi.
What I'd like to do is...
(In current folder)
- For each subfolder...
- Create a file called original_filenames.txt
- Echo the name of the folder into this original_filenames.txt
- List all files (including any subfolders) to original_filenames.txt
bash shell-script
I can do this in Windows CMD scripting but now I am getting into Debian via Raspberry Pi.
What I'd like to do is...
(In current folder)
- For each subfolder...
- Create a file called original_filenames.txt
- Echo the name of the folder into this original_filenames.txt
- List all files (including any subfolders) to original_filenames.txt
bash shell-script
bash shell-script
edited 11 hours ago
Rui F Ribeiro
42.1k1483142
42.1k1483142
asked May 30 '18 at 18:23
MurgatroydMurgatroyd
62
62
1
What are you having an issue doing? Please include the script you are attempting to use in your question and any errors you are encountering.
– kemotep
May 30 '18 at 18:28
Do you want to descend into subfolders and list those files?
– glenn jackman
May 30 '18 at 18:57
Yes I want to recurse through subfolders. I think I've got something working now, I was getting an error until I added quotes to deal with spaces in folder names:#!/bin/bash; for dir in */ ; do ; tree -s "$dir">"$dir/original_filenames.txt"; done
– Murgatroyd
May 30 '18 at 19:24
That's a good lesson. You'll want to read Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
May 30 '18 at 19:27
add a comment |
1
What are you having an issue doing? Please include the script you are attempting to use in your question and any errors you are encountering.
– kemotep
May 30 '18 at 18:28
Do you want to descend into subfolders and list those files?
– glenn jackman
May 30 '18 at 18:57
Yes I want to recurse through subfolders. I think I've got something working now, I was getting an error until I added quotes to deal with spaces in folder names:#!/bin/bash; for dir in */ ; do ; tree -s "$dir">"$dir/original_filenames.txt"; done
– Murgatroyd
May 30 '18 at 19:24
That's a good lesson. You'll want to read Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
May 30 '18 at 19:27
1
1
What are you having an issue doing? Please include the script you are attempting to use in your question and any errors you are encountering.
– kemotep
May 30 '18 at 18:28
What are you having an issue doing? Please include the script you are attempting to use in your question and any errors you are encountering.
– kemotep
May 30 '18 at 18:28
Do you want to descend into subfolders and list those files?
– glenn jackman
May 30 '18 at 18:57
Do you want to descend into subfolders and list those files?
– glenn jackman
May 30 '18 at 18:57
Yes I want to recurse through subfolders. I think I've got something working now, I was getting an error until I added quotes to deal with spaces in folder names:
#!/bin/bash; for dir in */ ; do ; tree -s "$dir">"$dir/original_filenames.txt"; done
– Murgatroyd
May 30 '18 at 19:24
Yes I want to recurse through subfolders. I think I've got something working now, I was getting an error until I added quotes to deal with spaces in folder names:
#!/bin/bash; for dir in */ ; do ; tree -s "$dir">"$dir/original_filenames.txt"; done
– Murgatroyd
May 30 '18 at 19:24
That's a good lesson. You'll want to read Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
May 30 '18 at 19:27
That's a good lesson. You'll want to read Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
May 30 '18 at 19:27
add a comment |
2 Answers
2
active
oldest
votes
Using a shell loop:
for dir in */; do
printf '%sn' "$dir"
( cd "$dir" && find . )
>"$dir/original_filenames.txt"
done
This iterates over all subdirectories in the current directory. The body of the loop outputs the name of the directory, and then a list of all files and directories, recursively, in that directory. This output goes to the original_filenames.txt
file in the directory.
Shorter:
for dir in */; do
find "$dir" >"$dir/original_filenames.txt"
done
This gives slightly different output in that the pathnames that are written to the file all start with the name of the subfolder.
If you're uninterested in directory names and just want pathnames of the regular files, use find
with -type f
after the directory name.
Note that if you're planning on using the generate output files for anything, then this will fail (or at least be very problematic) if any of the found pathnames contain newlines (which is entirely possible on a Unix system).
add a comment |
You could do this with the find
command and the exec
option - something like this:
find . -type d ( ! -name . ) -exec bash -c '
dirname=$(basename "") &&
cd "" &&
echo "" > original_filenames.txt &&
ls | grep -Fv original_filenames.txt >> original_filenames.txt
' ;
Here's a StackOverflow post you might find useful:
- How to go to each directory and execute a command?
1
For readability, you can have newlines in a single quoted string, and&&
at the end of a line does not require a line continuation.
– glenn jackman
May 30 '18 at 19:02
1
Instead of puttingin the middle of the shell script, you may want to pass it as an argument and use
"$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see whatdirname
is used for here, and you don't need( )
around! -name .
.
– ilkkachu
May 30 '18 at 19:15
@ilkkachu Thanks for the comment. The unuseddirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.
– igal
May 30 '18 at 19:21
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
1
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446964%2fbash-script-to-loop-through-folders-and-list-files-to-text%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using a shell loop:
for dir in */; do
printf '%sn' "$dir"
( cd "$dir" && find . )
>"$dir/original_filenames.txt"
done
This iterates over all subdirectories in the current directory. The body of the loop outputs the name of the directory, and then a list of all files and directories, recursively, in that directory. This output goes to the original_filenames.txt
file in the directory.
Shorter:
for dir in */; do
find "$dir" >"$dir/original_filenames.txt"
done
This gives slightly different output in that the pathnames that are written to the file all start with the name of the subfolder.
If you're uninterested in directory names and just want pathnames of the regular files, use find
with -type f
after the directory name.
Note that if you're planning on using the generate output files for anything, then this will fail (or at least be very problematic) if any of the found pathnames contain newlines (which is entirely possible on a Unix system).
add a comment |
Using a shell loop:
for dir in */; do
printf '%sn' "$dir"
( cd "$dir" && find . )
>"$dir/original_filenames.txt"
done
This iterates over all subdirectories in the current directory. The body of the loop outputs the name of the directory, and then a list of all files and directories, recursively, in that directory. This output goes to the original_filenames.txt
file in the directory.
Shorter:
for dir in */; do
find "$dir" >"$dir/original_filenames.txt"
done
This gives slightly different output in that the pathnames that are written to the file all start with the name of the subfolder.
If you're uninterested in directory names and just want pathnames of the regular files, use find
with -type f
after the directory name.
Note that if you're planning on using the generate output files for anything, then this will fail (or at least be very problematic) if any of the found pathnames contain newlines (which is entirely possible on a Unix system).
add a comment |
Using a shell loop:
for dir in */; do
printf '%sn' "$dir"
( cd "$dir" && find . )
>"$dir/original_filenames.txt"
done
This iterates over all subdirectories in the current directory. The body of the loop outputs the name of the directory, and then a list of all files and directories, recursively, in that directory. This output goes to the original_filenames.txt
file in the directory.
Shorter:
for dir in */; do
find "$dir" >"$dir/original_filenames.txt"
done
This gives slightly different output in that the pathnames that are written to the file all start with the name of the subfolder.
If you're uninterested in directory names and just want pathnames of the regular files, use find
with -type f
after the directory name.
Note that if you're planning on using the generate output files for anything, then this will fail (or at least be very problematic) if any of the found pathnames contain newlines (which is entirely possible on a Unix system).
Using a shell loop:
for dir in */; do
printf '%sn' "$dir"
( cd "$dir" && find . )
>"$dir/original_filenames.txt"
done
This iterates over all subdirectories in the current directory. The body of the loop outputs the name of the directory, and then a list of all files and directories, recursively, in that directory. This output goes to the original_filenames.txt
file in the directory.
Shorter:
for dir in */; do
find "$dir" >"$dir/original_filenames.txt"
done
This gives slightly different output in that the pathnames that are written to the file all start with the name of the subfolder.
If you're uninterested in directory names and just want pathnames of the regular files, use find
with -type f
after the directory name.
Note that if you're planning on using the generate output files for anything, then this will fail (or at least be very problematic) if any of the found pathnames contain newlines (which is entirely possible on a Unix system).
edited May 30 '18 at 19:32
answered May 30 '18 at 19:27
Kusalananda♦Kusalananda
142k18265440
142k18265440
add a comment |
add a comment |
You could do this with the find
command and the exec
option - something like this:
find . -type d ( ! -name . ) -exec bash -c '
dirname=$(basename "") &&
cd "" &&
echo "" > original_filenames.txt &&
ls | grep -Fv original_filenames.txt >> original_filenames.txt
' ;
Here's a StackOverflow post you might find useful:
- How to go to each directory and execute a command?
1
For readability, you can have newlines in a single quoted string, and&&
at the end of a line does not require a line continuation.
– glenn jackman
May 30 '18 at 19:02
1
Instead of puttingin the middle of the shell script, you may want to pass it as an argument and use
"$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see whatdirname
is used for here, and you don't need( )
around! -name .
.
– ilkkachu
May 30 '18 at 19:15
@ilkkachu Thanks for the comment. The unuseddirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.
– igal
May 30 '18 at 19:21
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
1
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
add a comment |
You could do this with the find
command and the exec
option - something like this:
find . -type d ( ! -name . ) -exec bash -c '
dirname=$(basename "") &&
cd "" &&
echo "" > original_filenames.txt &&
ls | grep -Fv original_filenames.txt >> original_filenames.txt
' ;
Here's a StackOverflow post you might find useful:
- How to go to each directory and execute a command?
1
For readability, you can have newlines in a single quoted string, and&&
at the end of a line does not require a line continuation.
– glenn jackman
May 30 '18 at 19:02
1
Instead of puttingin the middle of the shell script, you may want to pass it as an argument and use
"$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see whatdirname
is used for here, and you don't need( )
around! -name .
.
– ilkkachu
May 30 '18 at 19:15
@ilkkachu Thanks for the comment. The unuseddirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.
– igal
May 30 '18 at 19:21
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
1
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
add a comment |
You could do this with the find
command and the exec
option - something like this:
find . -type d ( ! -name . ) -exec bash -c '
dirname=$(basename "") &&
cd "" &&
echo "" > original_filenames.txt &&
ls | grep -Fv original_filenames.txt >> original_filenames.txt
' ;
Here's a StackOverflow post you might find useful:
- How to go to each directory and execute a command?
You could do this with the find
command and the exec
option - something like this:
find . -type d ( ! -name . ) -exec bash -c '
dirname=$(basename "") &&
cd "" &&
echo "" > original_filenames.txt &&
ls | grep -Fv original_filenames.txt >> original_filenames.txt
' ;
Here's a StackOverflow post you might find useful:
- How to go to each directory and execute a command?
edited May 30 '18 at 19:41
answered May 30 '18 at 18:54
igaligal
6,1511638
6,1511638
1
For readability, you can have newlines in a single quoted string, and&&
at the end of a line does not require a line continuation.
– glenn jackman
May 30 '18 at 19:02
1
Instead of puttingin the middle of the shell script, you may want to pass it as an argument and use
"$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see whatdirname
is used for here, and you don't need( )
around! -name .
.
– ilkkachu
May 30 '18 at 19:15
@ilkkachu Thanks for the comment. The unuseddirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.
– igal
May 30 '18 at 19:21
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
1
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
add a comment |
1
For readability, you can have newlines in a single quoted string, and&&
at the end of a line does not require a line continuation.
– glenn jackman
May 30 '18 at 19:02
1
Instead of puttingin the middle of the shell script, you may want to pass it as an argument and use
"$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see whatdirname
is used for here, and you don't need( )
around! -name .
.
– ilkkachu
May 30 '18 at 19:15
@ilkkachu Thanks for the comment. The unuseddirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.
– igal
May 30 '18 at 19:21
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
1
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
1
1
For readability, you can have newlines in a single quoted string, and
&&
at the end of a line does not require a line continuation.– glenn jackman
May 30 '18 at 19:02
For readability, you can have newlines in a single quoted string, and
&&
at the end of a line does not require a line continuation.– glenn jackman
May 30 '18 at 19:02
1
1
Instead of putting
in the middle of the shell script, you may want to pass it as an argument and use "$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see what dirname
is used for here, and you don't need ( )
around ! -name .
.– ilkkachu
May 30 '18 at 19:15
Instead of putting
in the middle of the shell script, you may want to pass it as an argument and use "$1"
inside the shell script, to avoid issues with strange filenames. Also, I can't see what dirname
is used for here, and you don't need ( )
around ! -name .
.– ilkkachu
May 30 '18 at 19:15
@ilkkachu Thanks for the comment. The unused
dirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The ! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.– igal
May 30 '18 at 19:21
@ilkkachu Thanks for the comment. The unused
dirname
was a typo/mistake on my part. I'm now using it to add just the filename to the output file. The ! -name .
was to ignore the current directory (i.e. to only apply the commands to proper subdirectories) - I removed it.– igal
May 30 '18 at 19:21
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
@glennjackman Thanks for the comment. I've update my post as you suggested.
– igal
May 30 '18 at 19:22
1
1
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
@igal, I added a version I think is readable. If you don't like, please edit it out.
– glenn jackman
May 30 '18 at 19:24
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446964%2fbash-script-to-loop-through-folders-and-list-files-to-text%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
-bash, shell-script
1
What are you having an issue doing? Please include the script you are attempting to use in your question and any errors you are encountering.
– kemotep
May 30 '18 at 18:28
Do you want to descend into subfolders and list those files?
– glenn jackman
May 30 '18 at 18:57
Yes I want to recurse through subfolders. I think I've got something working now, I was getting an error until I added quotes to deal with spaces in folder names:
#!/bin/bash; for dir in */ ; do ; tree -s "$dir">"$dir/original_filenames.txt"; done
– Murgatroyd
May 30 '18 at 19:24
That's a good lesson. You'll want to read Security implications of forgetting to quote a variable in bash/POSIX shells
– glenn jackman
May 30 '18 at 19:27