Loop that lists folders with spaces

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
Approach
I have a directory with named folders, randomly, with blank spaces and periods.
I created a small loop (in a script) with the intention of renaming these folders.
On the basis that these are my directories (test and empty):
$ ls -la
./
../
35._JK_io/
'43A. io kl_ -'/
'Mi.Nombre.es Adios'/
Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.
Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:
for archives in `sh -c 'ls -q */' sh`
do
echo "$archives"
done
Issue
The problem is that when I run the script I get:
35._JK_io/:
43A.
io
kl_
-/:
Mi.Nombre.es
Adios/:
Question
How do I prevent this from happening to me and to appear with spaces and as only three files?
Observation
When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not
Thank you!!
bash shell quoting wildcards
add a comment |Â
up vote
1
down vote
favorite
Approach
I have a directory with named folders, randomly, with blank spaces and periods.
I created a small loop (in a script) with the intention of renaming these folders.
On the basis that these are my directories (test and empty):
$ ls -la
./
../
35._JK_io/
'43A. io kl_ -'/
'Mi.Nombre.es Adios'/
Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.
Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:
for archives in `sh -c 'ls -q */' sh`
do
echo "$archives"
done
Issue
The problem is that when I run the script I get:
35._JK_io/:
43A.
io
kl_
-/:
Mi.Nombre.es
Adios/:
Question
How do I prevent this from happening to me and to appear with spaces and as only three files?
Observation
When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not
Thank you!!
bash shell quoting wildcards
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Approach
I have a directory with named folders, randomly, with blank spaces and periods.
I created a small loop (in a script) with the intention of renaming these folders.
On the basis that these are my directories (test and empty):
$ ls -la
./
../
35._JK_io/
'43A. io kl_ -'/
'Mi.Nombre.es Adios'/
Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.
Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:
for archives in `sh -c 'ls -q */' sh`
do
echo "$archives"
done
Issue
The problem is that when I run the script I get:
35._JK_io/:
43A.
io
kl_
-/:
Mi.Nombre.es
Adios/:
Question
How do I prevent this from happening to me and to appear with spaces and as only three files?
Observation
When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not
Thank you!!
bash shell quoting wildcards
Approach
I have a directory with named folders, randomly, with blank spaces and periods.
I created a small loop (in a script) with the intention of renaming these folders.
On the basis that these are my directories (test and empty):
$ ls -la
./
../
35._JK_io/
'43A. io kl_ -'/
'Mi.Nombre.es Adios'/
Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.
Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:
for archives in `sh -c 'ls -q */' sh`
do
echo "$archives"
done
Issue
The problem is that when I run the script I get:
35._JK_io/:
43A.
io
kl_
-/:
Mi.Nombre.es
Adios/:
Question
How do I prevent this from happening to me and to appear with spaces and as only three files?
Observation
When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not
Thank you!!
bash shell quoting wildcards
edited Jul 20 at 14:36
Gilles
502k1169881514
502k1169881514
asked Jul 20 at 11:03
Nicolás Alarcón R.
1165
1165
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().
Instead
for dirname in ./*/; do
printf 'Directory name is "%s"n' "$dirname"
done
The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.
It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.
Related:
- Why *not* parse `ls`?
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().
Instead
for dirname in ./*/; do
printf 'Directory name is "%s"n' "$dirname"
done
The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.
It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.
Related:
- Why *not* parse `ls`?
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
add a comment |Â
up vote
5
down vote
accepted
When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().
Instead
for dirname in ./*/; do
printf 'Directory name is "%s"n' "$dirname"
done
The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.
It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.
Related:
- Why *not* parse `ls`?
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().
Instead
for dirname in ./*/; do
printf 'Directory name is "%s"n' "$dirname"
done
The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.
It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.
Related:
- Why *not* parse `ls`?
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
When looping over directories, especially when these have funky names, don't loop over the output of ls. In general, passing pathnames between programs needs to be done with great caution as Unix filenames may contain any characters apart from / and the nul character ().
Instead
for dirname in ./*/; do
printf 'Directory name is "%s"n' "$dirname"
done
The final / in the pattern ./*/ makes the pattern expand to directories only. The dirname variable will get values like ./some directory name/ when doing this. I included ./ at the start of the pattern, but you may remove this if you like. Just be aware that if you have directories that have a dash (-) as the first character in their names, then you would later have to use e.g. mv -- "$dirname" "$newname" (with the --) to stop mv from interpreting the dash in the name as a command line option. With ./ at the start of $dirname, the -- is not needed.
It is extra important to quote variable expansions as the shell would otherwise do word splitting and filename globbing on the values.
Related:
- Why *not* parse `ls`?
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
edited Jul 20 at 11:19
answered Jul 20 at 11:09
Kusalananda
101k13199311
101k13199311
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457416%2floop-that-lists-folders-with-spaces%23new-answer', 'question_page');
);
Post as a guest
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
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
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